1890a9d84c5306f8e17dc887455a2a1d5758d7cf
[yaffs-website] / 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     private $loaders;
31
32     /**
33      * Accepts a list of LoaderInterface instances.
34      *
35      * @param LoaderInterface[] $loaders An array of LoaderInterface instances
36      *
37      * @throws MappingException If any of the loaders does not implement LoaderInterface
38      */
39     public function __construct(array $loaders)
40     {
41         foreach ($loaders as $loader) {
42             if (!$loader instanceof LoaderInterface) {
43                 throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', \get_class($loader)));
44             }
45         }
46
47         $this->loaders = $loaders;
48     }
49
50     /**
51      * {@inheritdoc}
52      */
53     public function loadClassMetadata(ClassMetadataInterface $metadata)
54     {
55         $success = false;
56
57         foreach ($this->loaders as $loader) {
58             $success = $loader->loadClassMetadata($metadata) || $success;
59         }
60
61         return $success;
62     }
63
64     /**
65      * @return LoaderInterface[]
66      */
67     public function getLoaders()
68     {
69         return $this->loaders;
70     }
71 }