8bf1c17da9d92ed98992657cc759caaa79e5fc87
[yaffs-website] / vendor / symfony / serializer / Mapping / Loader / LoaderChain.php
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
12 namespace Symfony\Component\Serializer\Mapping\Loader;
13
14 use Symfony\Component\Serializer\Exception\MappingException;
15 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
16
17 /**
18  * Calls multiple {@link LoaderInterface} instances in a chain.
19  *
20  * This class accepts multiple instances of LoaderInterface to be passed to the
21  * constructor. When {@link loadClassMetadata()} is called, the same method is called
22  * in <em>all</em> of these loaders, regardless of whether any of them was
23  * successful or not.
24  *
25  * @author Bernhard Schussek <bschussek@gmail.com>
26  * @author Kévin Dunglas <dunglas@gmail.com>
27  */
28 class LoaderChain implements LoaderInterface
29 {
30     /**
31      * @var LoaderInterface[]
32      */
33     private $loaders;
34
35     /**
36      * Accepts a list of LoaderInterface instances.
37      *
38      * @param LoaderInterface[] $loaders An array of LoaderInterface instances
39      *
40      * @throws MappingException If any of the loaders does not implement LoaderInterface
41      */
42     public function __construct(array $loaders)
43     {
44         foreach ($loaders as $loader) {
45             if (!$loader instanceof LoaderInterface) {
46                 throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', get_class($loader)));
47             }
48         }
49
50         $this->loaders = $loaders;
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     public function loadClassMetadata(ClassMetadataInterface $metadata)
57     {
58         $success = false;
59
60         foreach ($this->loaders as $loader) {
61             $success = $loader->loadClassMetadata($metadata) || $success;
62         }
63
64         return $success;
65     }
66 }