1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-validator for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-validator/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-validator/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\Validator;
10
11use Interop\Container\ContainerInterface;
12use Laminas\ServiceManager\Config;
13use Laminas\ServiceManager\FactoryInterface;
14use Laminas\ServiceManager\ServiceLocatorInterface;
15
16class ValidatorPluginManagerFactory implements FactoryInterface
17{
18    /**
19     * laminas-servicemanager v2 support for invocation options.
20     *
21     * @param array
22     */
23    protected $creationOptions;
24
25    /**
26     * {@inheritDoc}
27     *
28     * @return ValidatorPluginManager
29     */
30    public function __invoke(ContainerInterface $container, $name, array $options = null)
31    {
32        $pluginManager = new ValidatorPluginManager($container, $options ?: []);
33
34        // If this is in a laminas-mvc application, the ServiceListener will inject
35        // merged configuration during bootstrap.
36        if ($container->has('ServiceListener')) {
37            return $pluginManager;
38        }
39
40        // If we do not have a config service, nothing more to do
41        if (! $container->has('config')) {
42            return $pluginManager;
43        }
44
45        $config = $container->get('config');
46
47        // If we do not have validators configuration, nothing more to do
48        if (! isset($config['validators']) || ! is_array($config['validators'])) {
49            return $pluginManager;
50        }
51
52        // Wire service configuration for validators
53        (new Config($config['validators']))->configureServiceManager($pluginManager);
54
55        return $pluginManager;
56    }
57
58    /**
59     * {@inheritDoc}
60     *
61     * @return ValidatorPluginManager
62     */
63    public function createService(ServiceLocatorInterface $container, $name = null, $requestedName = null)
64    {
65        return $this($container, $requestedName ?: ValidatorPluginManager::class, $this->creationOptions);
66    }
67
68    /**
69     * laminas-servicemanager v2 support for invocation options.
70     *
71     * @param array $options
72     * @return void
73     */
74    public function setCreationOptions(array $options)
75    {
76        $this->creationOptions = $options;
77    }
78}
79