@@ -8,219 +8,219 @@ |
||
| 8 | 8 | |
| 9 | 9 | class MorphTo extends BelongsTo |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The type of the polymorphic relation. |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $morphType; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The entities whose relations are being eager loaded. |
|
| 20 | - * |
|
| 21 | - * @var EntityCollection |
|
| 22 | - */ |
|
| 23 | - protected $entities; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * All of the models keyed by ID. |
|
| 27 | - * |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $dictionary = []; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Indicates if soft-deleted model instances should be fetched. |
|
| 34 | - * |
|
| 35 | - * @var bool |
|
| 36 | - */ |
|
| 37 | - protected $withTrashed = false; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Indicate if the parent entity hold the key for the relation. |
|
| 41 | - * |
|
| 42 | - * @var boolean |
|
| 43 | - */ |
|
| 44 | - protected static $ownForeignKey = true; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Create a new belongs to relationship instance. |
|
| 48 | - * |
|
| 49 | - * @param Mapper $mapper |
|
| 50 | - * @param \Analogue\ORM\Mappable $parent |
|
| 51 | - * @param string $foreignKey |
|
| 52 | - * @param string $otherKey |
|
| 53 | - * @param string $type |
|
| 54 | - * @param string $relation |
|
| 55 | - */ |
|
| 56 | - public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation) |
|
| 57 | - { |
|
| 58 | - $this->morphType = $type; |
|
| 59 | - |
|
| 60 | - parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Set the constraints for an eager load of the relation. |
|
| 65 | - * |
|
| 66 | - * @param array $entities |
|
| 67 | - * @return void |
|
| 68 | - */ |
|
| 69 | - public function addEagerConstraints(array $entities) |
|
| 70 | - { |
|
| 71 | - $this->buildDictionary($this->entities = EntityCollection::make($entities)); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Build a dictionary with the entities |
|
| 76 | - * |
|
| 77 | - * @param EntityCollection $entities |
|
| 78 | - * @return void |
|
| 79 | - */ |
|
| 80 | - protected function buildDictionary(EntityCollection $entities) |
|
| 81 | - { |
|
| 82 | - foreach ($entities as $entity) { |
|
| 83 | - if ($entity->getEntityAttribute($this->morphType)) { |
|
| 84 | - $this->dictionary[$entity->getEntityAttribute($this->morphType)][$entity->getEntityAttribute($this->foreignKey)][] = $entity; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Match the eagerly loaded results to their parents. |
|
| 91 | - * |
|
| 92 | - * @param array $entities |
|
| 93 | - * @param EntityCollection $results |
|
| 94 | - * @param string $relation |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - public function match(array $entities, EntityCollection $results, $relation) |
|
| 98 | - { |
|
| 99 | - return $entities; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get the results of the relationship. |
|
| 104 | - * |
|
| 105 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 106 | - * @return EntityCollection |
|
| 107 | - */ |
|
| 108 | - public function getEager() |
|
| 109 | - { |
|
| 110 | - foreach (array_keys($this->dictionary) as $type) { |
|
| 111 | - $this->matchToMorphParents($type, $this->getResultsByType($type)); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return $this->entities; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Match the results for a given type to their parents. |
|
| 119 | - * |
|
| 120 | - * @param string $type |
|
| 121 | - * @param EntityCollection $results |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - protected function matchToMorphParents($type, EntityCollection $results) |
|
| 125 | - { |
|
| 126 | - $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 127 | - $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 128 | - |
|
| 129 | - foreach ($results as $result) { |
|
| 130 | - $key = $result->{$keyName}; |
|
| 131 | - |
|
| 132 | - if (isset($this->dictionary[$type][$key])) { |
|
| 133 | - foreach ($this->dictionary[$type][$key] as $entity) { |
|
| 134 | - $entity->setEntityAttribute($this->relation, $result); |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Get all of the relation results for a type. |
|
| 142 | - * |
|
| 143 | - * @param string $type |
|
| 144 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 145 | - * @return EntityCollection |
|
| 146 | - */ |
|
| 147 | - protected function getResultsByType($type) |
|
| 148 | - { |
|
| 149 | - $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 150 | - |
|
| 151 | - $key = $mapper->getEntityMap()->getKeyName(); |
|
| 152 | - |
|
| 153 | - $query = $mapper->getQuery(); |
|
| 154 | - |
|
| 155 | - return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Gather all of the foreign keys for a given type. |
|
| 160 | - * |
|
| 161 | - * @param string $type |
|
| 162 | - * @return BaseCollection |
|
| 163 | - */ |
|
| 164 | - protected function gatherKeysByType($type) |
|
| 165 | - { |
|
| 166 | - $foreign = $this->foreignKey; |
|
| 167 | - |
|
| 168 | - return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) { |
|
| 169 | - return head($entities)->{$foreign}; |
|
| 170 | - |
|
| 171 | - })->unique(); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Associate the model instance to the given parent. |
|
| 176 | - * |
|
| 177 | - * @param mixed $entity |
|
| 178 | - * @return void |
|
| 179 | - */ |
|
| 180 | - public function associate($entity) |
|
| 181 | - { |
|
| 182 | - // The Mapper will retrieve this association within the object model, we won't be using |
|
| 183 | - // the foreign key attribute inside the parent Entity. |
|
| 184 | - // |
|
| 185 | - //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
| 186 | - // |
|
| 187 | - // Instead, we'll just add the object to the Entity's attribute |
|
| 188 | - |
|
| 189 | - $this->parent->setEntityAttribute($this->relation, $entity->getEntityObject()); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Get the foreign key value pair for a related object |
|
| 194 | - * |
|
| 195 | - * @var mixed $related |
|
| 196 | - * |
|
| 197 | - * @return array |
|
| 198 | - */ |
|
| 199 | - public function getForeignKeyValuePair($related) |
|
| 200 | - { |
|
| 201 | - $foreignKey = $this->getForeignKey(); |
|
| 202 | - |
|
| 203 | - if ($related) { |
|
| 204 | - $wrapper = $this->factory->make($related); |
|
| 205 | - |
|
| 206 | - $relatedKey = $this->relatedMap->getKeyName(); |
|
| 207 | - |
|
| 208 | - return [ |
|
| 209 | - $foreignKey => $wrapper->getEntityAttribute($relatedKey), |
|
| 210 | - $this->morphType => $wrapper->getMap()->getMorphClass(), |
|
| 211 | - ]; |
|
| 212 | - } else { |
|
| 213 | - return [$foreignKey => null]; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Get the dictionary used by the relationship. |
|
| 219 | - * |
|
| 220 | - * @return array |
|
| 221 | - */ |
|
| 222 | - public function getDictionary() |
|
| 223 | - { |
|
| 224 | - return $this->dictionary; |
|
| 225 | - } |
|
| 11 | + /** |
|
| 12 | + * The type of the polymorphic relation. |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $morphType; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The entities whose relations are being eager loaded. |
|
| 20 | + * |
|
| 21 | + * @var EntityCollection |
|
| 22 | + */ |
|
| 23 | + protected $entities; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * All of the models keyed by ID. |
|
| 27 | + * |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $dictionary = []; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Indicates if soft-deleted model instances should be fetched. |
|
| 34 | + * |
|
| 35 | + * @var bool |
|
| 36 | + */ |
|
| 37 | + protected $withTrashed = false; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Indicate if the parent entity hold the key for the relation. |
|
| 41 | + * |
|
| 42 | + * @var boolean |
|
| 43 | + */ |
|
| 44 | + protected static $ownForeignKey = true; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Create a new belongs to relationship instance. |
|
| 48 | + * |
|
| 49 | + * @param Mapper $mapper |
|
| 50 | + * @param \Analogue\ORM\Mappable $parent |
|
| 51 | + * @param string $foreignKey |
|
| 52 | + * @param string $otherKey |
|
| 53 | + * @param string $type |
|
| 54 | + * @param string $relation |
|
| 55 | + */ |
|
| 56 | + public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation) |
|
| 57 | + { |
|
| 58 | + $this->morphType = $type; |
|
| 59 | + |
|
| 60 | + parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Set the constraints for an eager load of the relation. |
|
| 65 | + * |
|
| 66 | + * @param array $entities |
|
| 67 | + * @return void |
|
| 68 | + */ |
|
| 69 | + public function addEagerConstraints(array $entities) |
|
| 70 | + { |
|
| 71 | + $this->buildDictionary($this->entities = EntityCollection::make($entities)); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Build a dictionary with the entities |
|
| 76 | + * |
|
| 77 | + * @param EntityCollection $entities |
|
| 78 | + * @return void |
|
| 79 | + */ |
|
| 80 | + protected function buildDictionary(EntityCollection $entities) |
|
| 81 | + { |
|
| 82 | + foreach ($entities as $entity) { |
|
| 83 | + if ($entity->getEntityAttribute($this->morphType)) { |
|
| 84 | + $this->dictionary[$entity->getEntityAttribute($this->morphType)][$entity->getEntityAttribute($this->foreignKey)][] = $entity; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Match the eagerly loaded results to their parents. |
|
| 91 | + * |
|
| 92 | + * @param array $entities |
|
| 93 | + * @param EntityCollection $results |
|
| 94 | + * @param string $relation |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + public function match(array $entities, EntityCollection $results, $relation) |
|
| 98 | + { |
|
| 99 | + return $entities; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get the results of the relationship. |
|
| 104 | + * |
|
| 105 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 106 | + * @return EntityCollection |
|
| 107 | + */ |
|
| 108 | + public function getEager() |
|
| 109 | + { |
|
| 110 | + foreach (array_keys($this->dictionary) as $type) { |
|
| 111 | + $this->matchToMorphParents($type, $this->getResultsByType($type)); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return $this->entities; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Match the results for a given type to their parents. |
|
| 119 | + * |
|
| 120 | + * @param string $type |
|
| 121 | + * @param EntityCollection $results |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + protected function matchToMorphParents($type, EntityCollection $results) |
|
| 125 | + { |
|
| 126 | + $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 127 | + $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 128 | + |
|
| 129 | + foreach ($results as $result) { |
|
| 130 | + $key = $result->{$keyName}; |
|
| 131 | + |
|
| 132 | + if (isset($this->dictionary[$type][$key])) { |
|
| 133 | + foreach ($this->dictionary[$type][$key] as $entity) { |
|
| 134 | + $entity->setEntityAttribute($this->relation, $result); |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Get all of the relation results for a type. |
|
| 142 | + * |
|
| 143 | + * @param string $type |
|
| 144 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 145 | + * @return EntityCollection |
|
| 146 | + */ |
|
| 147 | + protected function getResultsByType($type) |
|
| 148 | + { |
|
| 149 | + $mapper = $this->relatedMapper->getManager()->mapper($type); |
|
| 150 | + |
|
| 151 | + $key = $mapper->getEntityMap()->getKeyName(); |
|
| 152 | + |
|
| 153 | + $query = $mapper->getQuery(); |
|
| 154 | + |
|
| 155 | + return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Gather all of the foreign keys for a given type. |
|
| 160 | + * |
|
| 161 | + * @param string $type |
|
| 162 | + * @return BaseCollection |
|
| 163 | + */ |
|
| 164 | + protected function gatherKeysByType($type) |
|
| 165 | + { |
|
| 166 | + $foreign = $this->foreignKey; |
|
| 167 | + |
|
| 168 | + return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) { |
|
| 169 | + return head($entities)->{$foreign}; |
|
| 170 | + |
|
| 171 | + })->unique(); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Associate the model instance to the given parent. |
|
| 176 | + * |
|
| 177 | + * @param mixed $entity |
|
| 178 | + * @return void |
|
| 179 | + */ |
|
| 180 | + public function associate($entity) |
|
| 181 | + { |
|
| 182 | + // The Mapper will retrieve this association within the object model, we won't be using |
|
| 183 | + // the foreign key attribute inside the parent Entity. |
|
| 184 | + // |
|
| 185 | + //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
| 186 | + // |
|
| 187 | + // Instead, we'll just add the object to the Entity's attribute |
|
| 188 | + |
|
| 189 | + $this->parent->setEntityAttribute($this->relation, $entity->getEntityObject()); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Get the foreign key value pair for a related object |
|
| 194 | + * |
|
| 195 | + * @var mixed $related |
|
| 196 | + * |
|
| 197 | + * @return array |
|
| 198 | + */ |
|
| 199 | + public function getForeignKeyValuePair($related) |
|
| 200 | + { |
|
| 201 | + $foreignKey = $this->getForeignKey(); |
|
| 202 | + |
|
| 203 | + if ($related) { |
|
| 204 | + $wrapper = $this->factory->make($related); |
|
| 205 | + |
|
| 206 | + $relatedKey = $this->relatedMap->getKeyName(); |
|
| 207 | + |
|
| 208 | + return [ |
|
| 209 | + $foreignKey => $wrapper->getEntityAttribute($relatedKey), |
|
| 210 | + $this->morphType => $wrapper->getMap()->getMorphClass(), |
|
| 211 | + ]; |
|
| 212 | + } else { |
|
| 213 | + return [$foreignKey => null]; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Get the dictionary used by the relationship. |
|
| 219 | + * |
|
| 220 | + * @return array |
|
| 221 | + */ |
|
| 222 | + public function getDictionary() |
|
| 223 | + { |
|
| 224 | + return $this->dictionary; |
|
| 225 | + } |
|
| 226 | 226 | } |