| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace AlgoWeb\PODataLaravel\Models\ObjectMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\Association; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationMonomorphic; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityGubbins; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Map | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var EntityGubbins[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $entities = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var Association[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $associations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param EntityGubbins $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function addEntity(EntityGubbins $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->entities[$entity->getClassName()] = $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return EntityGubbins[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getEntities() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $this->entities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function resolveEntity($entityClassName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return array_key_exists($entityClassName, $this->entities) ? $this->entities[$entityClassName] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param EntityGubbins[] $entities | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function setEntities(array $entities) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->entities = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         foreach ($entities as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             if (!$entity instanceof EntityGubbins) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 throw new \InvalidArgumentException('Entities array must contain only EntityGubbins objects'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach ($entities as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->entities[$entity->getClassName()] = $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param Association[] $associations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function setAssociations(array $associations) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         foreach ($associations as $association) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $this->addAssociation($association); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param Association $association | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function addAssociation(Association $association) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (!is_array($this->associations)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->associations = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($association instanceof AssociationMonomorphic) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $this->addAssociationMonomorphic($association); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             throw new \InvalidArgumentException('Association type not yet handled'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->associations[] = $association; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return Association[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function getAssociations() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $this->associations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function isOK() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         foreach ($this->entities as $entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             if (!$entity->isOK()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param AssociationMonomorphic $association | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     private function addAssociationMonomorphic(AssociationMonomorphic $association) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $firstClass = $this->entities[$association->getFirst()->getBaseType()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $secondClass = $this->entities[$association->getLast()->getBaseType()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $firstClass->addAssociation($association); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $secondClass->addAssociation($association, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  |