AcMailer\Options\Factory\MailOptionsAbstractFactory::__invoke PHP Method

__invoke() public method

Create an object
public __invoke ( Interop\Container\ContainerInterface $container, string $requestedName, array $options = null ) : object
$container Interop\Container\ContainerInterface
$requestedName string
$options array
return object
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $specificServiceName = explode('.', $requestedName)[2];
        $config = $this->getConfig($container);
        $specificConfig = $config[$specificServiceName];
        if (!is_array($specificConfig)) {
            $specificConfig = [];
        }
        do {
            // Get extends
            $extendsConfigKey = isset($specificConfig['extends']) && is_string($specificConfig['extends']) ? trim($specificConfig['extends']) : null;
            // Always unset the extends, in case it had a value null, to prevent the MailOptions object to throw an
            // exception
            unset($specificConfig['extends']);
            // Try to extend from another configuration if defined and exists
            if (!is_null($extendsConfigKey) && array_key_exists($extendsConfigKey, $config) && is_array($config[$extendsConfigKey])) {
                $specificConfig = ArrayUtils::merge($config[$extendsConfigKey], $specificConfig);
            }
        } while ($extendsConfigKey != null);
        return new MailOptions($specificConfig);
    }

Usage Example

 public function testExtendsDoubleChaining()
 {
     $this->serviceLocator = new ServiceManagerMock(['Config' => ['acmailer_options' => ['default' => ['extends' => null, 'message_options' => ['to' => '*****@*****.**']], 'foo' => ['extends' => 'default', 'message_options' => ['from' => '*****@*****.**']], 'bar' => ['extends' => 'foo', 'message_options' => ['to' => '*****@*****.**', 'subject' => 'Foobar subject']]]]]);
     /** @var MailOptions $mailOptions */
     $mailOptions = $this->mailOptionsFactory->__invoke($this->serviceLocator, 'acmailer.mailoptions.bar');
     $this->assertInstanceOf('AcMailer\\Options\\MailOptions', $mailOptions);
     $this->assertEquals(['to' => [['*****@*****.**']], 'from' => '*****@*****.**', 'subject' => 'Foobar subject'], ['to' => [$mailOptions->getMessageOptions()->getTo()], 'from' => $mailOptions->getMessageOptions()->getFrom(), 'subject' => $mailOptions->getMessageOptions()->getSubject()]);
 }
MailOptionsAbstractFactory