Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / ResolveTargetEntityListener.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\ORM\Tools;
21
22 use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
23 use Doctrine\ORM\Mapping\ClassMetadata;
24
25 /**
26  * ResolveTargetEntityListener
27  *
28  * Mechanism to overwrite interfaces or classes specified as association
29  * targets.
30  *
31  * @author Benjamin Eberlei <kontakt@beberlei.de>
32  * @since 2.2
33  */
34 class ResolveTargetEntityListener
35 {
36     /**
37      * @var array
38      */
39     private $resolveTargetEntities = array();
40
41     /**
42      * Add a target-entity class name to resolve to a new class name.
43      *
44      * @param string $originalEntity
45      * @param string $newEntity
46      * @param array $mapping
47      * @return void
48      */
49     public function addResolveTargetEntity($originalEntity, $newEntity, array $mapping)
50     {
51         $mapping['targetEntity'] = ltrim($newEntity, "\\");
52         $this->resolveTargetEntities[ltrim($originalEntity, "\\")] = $mapping;
53     }
54
55     /**
56      * Process event and resolve new target entity names.
57      *
58      * @param LoadClassMetadataEventArgs $args
59      * @return void
60      */
61     public function loadClassMetadata(LoadClassMetadataEventArgs $args)
62     {
63         $cm = $args->getClassMetadata();
64         foreach ($cm->associationMappings as $mapping) {
65             if (isset($this->resolveTargetEntities[$mapping['targetEntity']])) {
66                 $this->remapAssociation($cm, $mapping);
67             }
68         }
69     }
70
71     private function remapAssociation($classMetadata, $mapping)
72     {
73         $newMapping = $this->resolveTargetEntities[$mapping['targetEntity']];
74         $newMapping = array_replace_recursive($mapping, $newMapping);
75         $newMapping['fieldName'] = $mapping['fieldName'];
76         unset($classMetadata->associationMappings[$mapping['fieldName']]);
77
78         switch ($mapping['type']) {
79             case ClassMetadata::MANY_TO_MANY:
80                 $classMetadata->mapManyToMany($newMapping);
81                 break;
82             case ClassMetadata::MANY_TO_ONE:
83                 $classMetadata->mapManyToOne($newMapping);
84                 break;
85             case ClassMetadata::ONE_TO_MANY:
86                 $classMetadata->mapOneToMany($newMapping);
87                 break;
88             case ClassMetadata::ONE_TO_ONE:
89                 $classMetadata->mapOneToOne($newMapping);
90                 break;
91         }
92     }
93 }
94