1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Validator\Mapping\Loader;
13
14use Symfony\Component\Validator\Exception\MappingException;
15use Symfony\Component\Validator\Mapping\ClassMetadata;
16
17/**
18 * Loads validation metadata from multiple {@link LoaderInterface} instances.
19 *
20 * Pass the loaders when constructing the chain. Once
21 * {@link loadClassMetadata()} is called, that method will be called on all
22 * loaders in the chain.
23 *
24 * @author Bernhard Schussek <bschussek@gmail.com>
25 */
26class LoaderChain implements LoaderInterface
27{
28    protected $loaders;
29
30    /**
31     * @param LoaderInterface[] $loaders The metadata loaders to use
32     *
33     * @throws MappingException If any of the loaders has an invalid type
34     */
35    public function __construct(array $loaders)
36    {
37        foreach ($loaders as $loader) {
38            if (!$loader instanceof LoaderInterface) {
39                throw new MappingException(sprintf('Class "%s" is expected to implement LoaderInterface.', \get_class($loader)));
40            }
41        }
42
43        $this->loaders = $loaders;
44    }
45
46    /**
47     * {@inheritdoc}
48     */
49    public function loadClassMetadata(ClassMetadata $metadata)
50    {
51        $success = false;
52
53        foreach ($this->loaders as $loader) {
54            $success = $loader->loadClassMetadata($metadata) || $success;
55        }
56
57        return $success;
58    }
59
60    /**
61     * @return LoaderInterface[]
62     */
63    public function getLoaders()
64    {
65        return $this->loaders;
66    }
67}
68