PHP code example of ucarsolutions / recipient-resolver

1. Go to this page and download the library: Download ucarsolutions/recipient-resolver library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ucarsolutions / recipient-resolver example snippets



use Ucarsolutions\RecipientResolver\Provider\YamlRecipientProvider;
use Ucarsolutions\RecipientResolver\Service\RecipientResolver;
use Ucarsolutions\RecipientResolver\Entity\Recipient;
use Ucarsolutions\RecipientResolver\Entity\NullRecipient;

$provider = new YamlRecipientProvider(__DIR__ . '/recipients.yaml');
$resolver = new RecipientResolver($provider);

// Get the whole map
/** @var array<string, Recipient> $all */
$all = $resolver->all();

// Resolve a single list
$alerts = $resolver->resolve('alerts'); // Recipient

$to  = $alerts->getReceiver();        // ['[email protected]']
$cc  = $alerts->getCarbonCopy();      // ['[email protected]']
$bcc = $alerts->getBlindCarbonCopy(); // []

// Unknown list → NullRecipient (safe no-op)
$unknown = $resolver->resolve('does-not-exist'); // NullRecipient
$unknown->getReceiver(); // []

// JSON (e.g., for logging)
$json = json_encode($alerts, JSON_THROW_ON_ERROR);
// {"recipient":["[email protected]"],"carbonCopy":["[email protected]"],"blindCarbonCopy":[]}


use Ucarsolutions\RecipientResolver\Entity\Recipient;
use Ucarsolutions\RecipientResolver\Provider\RecipientProviderInterface;

/**
 * @return array<string, Recipient>
 */
final class MyDbRecipientProvider implements RecipientProviderInterface
{
    public function provide(): array
    {
        // Build Recipient objects from your data source
        return [
            'alerts' => new Recipient(
                receiver: ['[email protected]'],
                carbonCopy: [],
                blindCarbonCopy: [],
            ),
        ];
    }
}


use Ucarsolutions\RecipientResolver\Service\RecipientResolver;

$resolver = new RecipientResolver(new MyDbRecipientProvider());