@@ -7,79 +7,79 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class EntityWrapper extends Wrapper |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Method used by the mapper to set the object |
|
| 12 | - * attribute raw values (hydration) |
|
| 13 | - * |
|
| 14 | - * @param array $attributes |
|
| 15 | - * |
|
| 16 | - * @return void |
|
| 17 | - */ |
|
| 18 | - public function setEntityAttributes(array $attributes) |
|
| 19 | - { |
|
| 20 | - $this->entity->setEntityAttributes($attributes); |
|
| 21 | - } |
|
| 10 | + /** |
|
| 11 | + * Method used by the mapper to set the object |
|
| 12 | + * attribute raw values (hydration) |
|
| 13 | + * |
|
| 14 | + * @param array $attributes |
|
| 15 | + * |
|
| 16 | + * @return void |
|
| 17 | + */ |
|
| 18 | + public function setEntityAttributes(array $attributes) |
|
| 19 | + { |
|
| 20 | + $this->entity->setEntityAttributes($attributes); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Method used by the mapper to get the |
|
| 25 | - * raw object's values. |
|
| 26 | - * |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - public function getEntityAttributes() |
|
| 30 | - { |
|
| 31 | - return $this->entity->getEntityAttributes(); |
|
| 32 | - } |
|
| 23 | + /** |
|
| 24 | + * Method used by the mapper to get the |
|
| 25 | + * raw object's values. |
|
| 26 | + * |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + public function getEntityAttributes() |
|
| 30 | + { |
|
| 31 | + return $this->entity->getEntityAttributes(); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Method used by the mapper to set raw |
|
| 36 | - * key-value pair |
|
| 37 | - * |
|
| 38 | - * @param string $key |
|
| 39 | - * @param string $value |
|
| 40 | - * |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - public function setEntityAttribute($key, $value) |
|
| 44 | - { |
|
| 45 | - $attributes = $this->entity->getEntityAttributes(); |
|
| 34 | + /** |
|
| 35 | + * Method used by the mapper to set raw |
|
| 36 | + * key-value pair |
|
| 37 | + * |
|
| 38 | + * @param string $key |
|
| 39 | + * @param string $value |
|
| 40 | + * |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + public function setEntityAttribute($key, $value) |
|
| 44 | + { |
|
| 45 | + $attributes = $this->entity->getEntityAttributes(); |
|
| 46 | 46 | |
| 47 | - $attributes[$key] = $value; |
|
| 47 | + $attributes[$key] = $value; |
|
| 48 | 48 | |
| 49 | - $this->entity->setEntityAttributes($attributes); |
|
| 50 | - } |
|
| 49 | + $this->entity->setEntityAttributes($attributes); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Method used by the mapper to get single |
|
| 54 | - * key-value pair |
|
| 55 | - * |
|
| 56 | - * @param string $key |
|
| 57 | - * @return mixed|null |
|
| 58 | - */ |
|
| 59 | - public function getEntityAttribute($key) |
|
| 60 | - { |
|
| 61 | - if ($this->hasAttribute($key)) { |
|
| 62 | - $attributes = $this->entity->getEntityAttributes(); |
|
| 63 | - return $attributes[$key]; |
|
| 64 | - } else { |
|
| 65 | - return null; |
|
| 66 | - } |
|
| 67 | - } |
|
| 52 | + /** |
|
| 53 | + * Method used by the mapper to get single |
|
| 54 | + * key-value pair |
|
| 55 | + * |
|
| 56 | + * @param string $key |
|
| 57 | + * @return mixed|null |
|
| 58 | + */ |
|
| 59 | + public function getEntityAttribute($key) |
|
| 60 | + { |
|
| 61 | + if ($this->hasAttribute($key)) { |
|
| 62 | + $attributes = $this->entity->getEntityAttributes(); |
|
| 63 | + return $attributes[$key]; |
|
| 64 | + } else { |
|
| 65 | + return null; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Test if a given attribute exists |
|
| 71 | - * |
|
| 72 | - * @param string $key |
|
| 73 | - * @return boolean |
|
| 74 | - */ |
|
| 75 | - public function hasAttribute($key) |
|
| 76 | - { |
|
| 77 | - $attributes = $this->entity->getEntityAttributes(); |
|
| 69 | + /** |
|
| 70 | + * Test if a given attribute exists |
|
| 71 | + * |
|
| 72 | + * @param string $key |
|
| 73 | + * @return boolean |
|
| 74 | + */ |
|
| 75 | + public function hasAttribute($key) |
|
| 76 | + { |
|
| 77 | + $attributes = $this->entity->getEntityAttributes(); |
|
| 78 | 78 | |
| 79 | - if (array_key_exists($key, $attributes)) { |
|
| 80 | - return true; |
|
| 81 | - } else { |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - } |
|
| 79 | + if (array_key_exists($key, $attributes)) { |
|
| 80 | + return true; |
|
| 81 | + } else { |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -7,27 +7,27 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Factory |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Build the wrapper corresponding to the object's type |
|
| 12 | - * |
|
| 13 | - * @param mixed $object |
|
| 14 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 15 | - * @return Wrapper |
|
| 16 | - */ |
|
| 17 | - public function make($object) |
|
| 18 | - { |
|
| 19 | - $manager = Manager::getInstance(); |
|
| 10 | + /** |
|
| 11 | + * Build the wrapper corresponding to the object's type |
|
| 12 | + * |
|
| 13 | + * @param mixed $object |
|
| 14 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 15 | + * @return Wrapper |
|
| 16 | + */ |
|
| 17 | + public function make($object) |
|
| 18 | + { |
|
| 19 | + $manager = Manager::getInstance(); |
|
| 20 | 20 | |
| 21 | - if ($manager->isValueObject($object)) { |
|
| 22 | - $entityMap = $manager->getValueMap($object); |
|
| 23 | - } else { |
|
| 24 | - $entityMap = $manager->mapper($object)->getEntityMap(); |
|
| 25 | - } |
|
| 21 | + if ($manager->isValueObject($object)) { |
|
| 22 | + $entityMap = $manager->getValueMap($object); |
|
| 23 | + } else { |
|
| 24 | + $entityMap = $manager->mapper($object)->getEntityMap(); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - if ($object instanceof Mappable) { |
|
| 28 | - return new EntityWrapper($object, $entityMap); |
|
| 29 | - } else { |
|
| 30 | - return new PlainObjectWrapper($object, $entityMap); |
|
| 31 | - } |
|
| 32 | - } |
|
| 27 | + if ($object instanceof Mappable) { |
|
| 28 | + return new EntityWrapper($object, $entityMap); |
|
| 29 | + } else { |
|
| 30 | + return new PlainObjectWrapper($object, $entityMap); |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -81,7 +81,7 @@ |
||
| 81 | 81 | |
| 82 | 82 | // We need to filter out properties that could belong to the object |
| 83 | 83 | // and which are not intended to be handled by the ORM |
| 84 | - return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
| 84 | + return array_filter($objectProperties, function(\ReflectionProperty $item) use ($attributeList) { |
|
| 85 | 85 | if (in_array($item->getName(), $attributeList)) { |
| 86 | 86 | return true; |
| 87 | 87 | } |
@@ -6,203 +6,203 @@ |
||
| 6 | 6 | |
| 7 | 7 | class PlainObjectWrapper extends Wrapper |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The list of attributes for the managed entity |
|
| 11 | - * |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - protected $attributeList; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * The reflection class for the managed entity |
|
| 18 | - * |
|
| 19 | - * @var ReflectionClass |
|
| 20 | - */ |
|
| 21 | - protected $reflection; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * PlainObjectWrapper constructor. |
|
| 25 | - * @param $popoEntity |
|
| 26 | - * @param $entityMap |
|
| 27 | - */ |
|
| 28 | - public function __construct($popoEntity, $entityMap) |
|
| 29 | - { |
|
| 30 | - $this->reflection = new ReflectionClass($popoEntity); |
|
| 31 | - |
|
| 32 | - parent::__construct($popoEntity, $entityMap); |
|
| 33 | - |
|
| 34 | - $this->attributeList = $this->getAttributeList(); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Get Compiled Attributes (key, attributes, embed, relations) |
|
| 39 | - * |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - protected function getAttributeList() |
|
| 43 | - { |
|
| 44 | - return $this->entityMap->getCompiledAttributes(); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Extract Attributes from a Plain Php Object |
|
| 49 | - * |
|
| 50 | - * @return array $attributes |
|
| 51 | - */ |
|
| 52 | - protected function extract() |
|
| 53 | - { |
|
| 54 | - $properties = $this->getMappedProperties(); |
|
| 55 | - |
|
| 56 | - $attributes = []; |
|
| 57 | - |
|
| 58 | - foreach ($properties as $property) { |
|
| 59 | - $name = $property->getName(); |
|
| 60 | - |
|
| 61 | - if ($property->isPublic()) { |
|
| 62 | - $attributes[$name] = $this->entity->$name; |
|
| 63 | - } else { |
|
| 64 | - $property->setAccessible(true); |
|
| 9 | + /** |
|
| 10 | + * The list of attributes for the managed entity |
|
| 11 | + * |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + protected $attributeList; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * The reflection class for the managed entity |
|
| 18 | + * |
|
| 19 | + * @var ReflectionClass |
|
| 20 | + */ |
|
| 21 | + protected $reflection; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * PlainObjectWrapper constructor. |
|
| 25 | + * @param $popoEntity |
|
| 26 | + * @param $entityMap |
|
| 27 | + */ |
|
| 28 | + public function __construct($popoEntity, $entityMap) |
|
| 29 | + { |
|
| 30 | + $this->reflection = new ReflectionClass($popoEntity); |
|
| 31 | + |
|
| 32 | + parent::__construct($popoEntity, $entityMap); |
|
| 33 | + |
|
| 34 | + $this->attributeList = $this->getAttributeList(); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Get Compiled Attributes (key, attributes, embed, relations) |
|
| 39 | + * |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + protected function getAttributeList() |
|
| 43 | + { |
|
| 44 | + return $this->entityMap->getCompiledAttributes(); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Extract Attributes from a Plain Php Object |
|
| 49 | + * |
|
| 50 | + * @return array $attributes |
|
| 51 | + */ |
|
| 52 | + protected function extract() |
|
| 53 | + { |
|
| 54 | + $properties = $this->getMappedProperties(); |
|
| 55 | + |
|
| 56 | + $attributes = []; |
|
| 57 | + |
|
| 58 | + foreach ($properties as $property) { |
|
| 59 | + $name = $property->getName(); |
|
| 60 | + |
|
| 61 | + if ($property->isPublic()) { |
|
| 62 | + $attributes[$name] = $this->entity->$name; |
|
| 63 | + } else { |
|
| 64 | + $property->setAccessible(true); |
|
| 65 | 65 | |
| 66 | - $attributes[$name] = $property->getValue($this->entity); |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return $attributes; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @return \ReflectionProperty[] |
|
| 75 | - */ |
|
| 76 | - protected function getMappedProperties() |
|
| 77 | - { |
|
| 78 | - $objectProperties = $this->reflection->getProperties(); |
|
| 79 | - |
|
| 80 | - $attributeList = $this->getAttributeList(); |
|
| 81 | - |
|
| 82 | - // We need to filter out properties that could belong to the object |
|
| 83 | - // and which are not intended to be handled by the ORM |
|
| 84 | - return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
| 85 | - if (in_array($item->getName(), $attributeList)) { |
|
| 86 | - return true; |
|
| 87 | - } |
|
| 88 | - }); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param string $name |
|
| 93 | - * @return \ReflectionProperty |
|
| 94 | - */ |
|
| 95 | - protected function getMappedProperty($name) |
|
| 96 | - { |
|
| 97 | - $name = $this->entityMap->getAttributeNameForColumn($name); |
|
| 98 | - return $this->reflection->getProperty($name); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Hydrate Plain PHP Object with wrapped attributes |
|
| 103 | - * |
|
| 104 | - * @param $attributes |
|
| 105 | - * @return void |
|
| 106 | - */ |
|
| 107 | - protected function hydrate($attributes) |
|
| 108 | - { |
|
| 109 | - $properties = $this->getMappedProperties(); |
|
| 110 | - |
|
| 111 | - foreach ($properties as $property) { |
|
| 112 | - $name = $property->getName(); |
|
| 113 | - |
|
| 114 | - if ($property->isPublic()) { |
|
| 115 | - $this->entity->$name = $attributes[$name]; |
|
| 116 | - } else { |
|
| 117 | - $property->setAccessible(true); |
|
| 118 | - if (isset($attributes[$name])) { |
|
| 119 | - $property->setValue($this->entity, $attributes[$name]); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Method used by the mapper to set the object |
|
| 127 | - * attribute raw values (hydration) |
|
| 128 | - * |
|
| 129 | - * @param array $attributes |
|
| 130 | - * |
|
| 131 | - * @return void |
|
| 132 | - */ |
|
| 133 | - public function setEntityAttributes(array $attributes) |
|
| 134 | - { |
|
| 135 | - $this->hydrate($attributes); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Method used by the mapper to get the |
|
| 140 | - * raw object's values. |
|
| 141 | - * |
|
| 142 | - * @return array |
|
| 143 | - */ |
|
| 144 | - public function getEntityAttributes() |
|
| 145 | - { |
|
| 146 | - return $this->extract(); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Method used by the mapper to set raw |
|
| 151 | - * key-value pair |
|
| 152 | - * |
|
| 153 | - * @param string $key |
|
| 154 | - * @param string $value |
|
| 155 | - * |
|
| 156 | - * @return void |
|
| 157 | - */ |
|
| 158 | - public function setEntityAttribute($key, $value) |
|
| 159 | - { |
|
| 160 | - $property = $this->getMappedProperty($key); |
|
| 161 | - |
|
| 162 | - if ($property->isPublic()) { |
|
| 163 | - $this->entity->$key = $value; |
|
| 164 | - } else { |
|
| 165 | - $property->setAccessible(true); |
|
| 66 | + $attributes[$name] = $property->getValue($this->entity); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return $attributes; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @return \ReflectionProperty[] |
|
| 75 | + */ |
|
| 76 | + protected function getMappedProperties() |
|
| 77 | + { |
|
| 78 | + $objectProperties = $this->reflection->getProperties(); |
|
| 79 | + |
|
| 80 | + $attributeList = $this->getAttributeList(); |
|
| 81 | + |
|
| 82 | + // We need to filter out properties that could belong to the object |
|
| 83 | + // and which are not intended to be handled by the ORM |
|
| 84 | + return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
| 85 | + if (in_array($item->getName(), $attributeList)) { |
|
| 86 | + return true; |
|
| 87 | + } |
|
| 88 | + }); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param string $name |
|
| 93 | + * @return \ReflectionProperty |
|
| 94 | + */ |
|
| 95 | + protected function getMappedProperty($name) |
|
| 96 | + { |
|
| 97 | + $name = $this->entityMap->getAttributeNameForColumn($name); |
|
| 98 | + return $this->reflection->getProperty($name); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Hydrate Plain PHP Object with wrapped attributes |
|
| 103 | + * |
|
| 104 | + * @param $attributes |
|
| 105 | + * @return void |
|
| 106 | + */ |
|
| 107 | + protected function hydrate($attributes) |
|
| 108 | + { |
|
| 109 | + $properties = $this->getMappedProperties(); |
|
| 110 | + |
|
| 111 | + foreach ($properties as $property) { |
|
| 112 | + $name = $property->getName(); |
|
| 113 | + |
|
| 114 | + if ($property->isPublic()) { |
|
| 115 | + $this->entity->$name = $attributes[$name]; |
|
| 116 | + } else { |
|
| 117 | + $property->setAccessible(true); |
|
| 118 | + if (isset($attributes[$name])) { |
|
| 119 | + $property->setValue($this->entity, $attributes[$name]); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Method used by the mapper to set the object |
|
| 127 | + * attribute raw values (hydration) |
|
| 128 | + * |
|
| 129 | + * @param array $attributes |
|
| 130 | + * |
|
| 131 | + * @return void |
|
| 132 | + */ |
|
| 133 | + public function setEntityAttributes(array $attributes) |
|
| 134 | + { |
|
| 135 | + $this->hydrate($attributes); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Method used by the mapper to get the |
|
| 140 | + * raw object's values. |
|
| 141 | + * |
|
| 142 | + * @return array |
|
| 143 | + */ |
|
| 144 | + public function getEntityAttributes() |
|
| 145 | + { |
|
| 146 | + return $this->extract(); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Method used by the mapper to set raw |
|
| 151 | + * key-value pair |
|
| 152 | + * |
|
| 153 | + * @param string $key |
|
| 154 | + * @param string $value |
|
| 155 | + * |
|
| 156 | + * @return void |
|
| 157 | + */ |
|
| 158 | + public function setEntityAttribute($key, $value) |
|
| 159 | + { |
|
| 160 | + $property = $this->getMappedProperty($key); |
|
| 161 | + |
|
| 162 | + if ($property->isPublic()) { |
|
| 163 | + $this->entity->$key = $value; |
|
| 164 | + } else { |
|
| 165 | + $property->setAccessible(true); |
|
| 166 | 166 | |
| 167 | - $property->setValue($this->entity, $value); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $this->attributes[$key] = $value; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Method used by the mapper to get single |
|
| 175 | - * key-value pair |
|
| 176 | - * |
|
| 177 | - * @param string $key |
|
| 178 | - * @return mixed |
|
| 179 | - */ |
|
| 180 | - public function getEntityAttribute($key) |
|
| 181 | - { |
|
| 182 | - $property = $this->getMappedProperty($key); |
|
| 183 | - |
|
| 184 | - if ($property->isPublic()) { |
|
| 185 | - $value = $this->entity->$key; |
|
| 186 | - } else { |
|
| 187 | - $property->setAccessible(true); |
|
| 188 | - $value = $property->getValue($this->entity); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return $value; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Test if a given attribute exists |
|
| 196 | - * |
|
| 197 | - * @param string $key |
|
| 198 | - * @return boolean |
|
| 199 | - */ |
|
| 200 | - public function hasAttribute($key) |
|
| 201 | - { |
|
| 202 | - if (array_key_exists($key, $this->attributeList)) { |
|
| 203 | - return true; |
|
| 204 | - } else { |
|
| 205 | - return false; |
|
| 206 | - } |
|
| 207 | - } |
|
| 167 | + $property->setValue($this->entity, $value); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $this->attributes[$key] = $value; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Method used by the mapper to get single |
|
| 175 | + * key-value pair |
|
| 176 | + * |
|
| 177 | + * @param string $key |
|
| 178 | + * @return mixed |
|
| 179 | + */ |
|
| 180 | + public function getEntityAttribute($key) |
|
| 181 | + { |
|
| 182 | + $property = $this->getMappedProperty($key); |
|
| 183 | + |
|
| 184 | + if ($property->isPublic()) { |
|
| 185 | + $value = $this->entity->$key; |
|
| 186 | + } else { |
|
| 187 | + $property->setAccessible(true); |
|
| 188 | + $value = $property->getValue($this->entity); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return $value; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Test if a given attribute exists |
|
| 196 | + * |
|
| 197 | + * @param string $key |
|
| 198 | + * @return boolean |
|
| 199 | + */ |
|
| 200 | + public function hasAttribute($key) |
|
| 201 | + { |
|
| 202 | + if (array_key_exists($key, $this->attributeList)) { |
|
| 203 | + return true; |
|
| 204 | + } else { |
|
| 205 | + return false; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | 208 | } |
@@ -17,218 +17,218 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class CollectionProxy extends Proxy implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Underlying Lazyloaded collection |
|
| 22 | - * @var EntityCollection |
|
| 23 | - */ |
|
| 24 | - protected $loadedCollection; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Added Items Collection |
|
| 28 | - * @var EntityCollection |
|
| 29 | - */ |
|
| 30 | - protected $addedItems; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @param mixed $parentEntity |
|
| 34 | - * @param string $relation relationship method handled by the proxy. |
|
| 35 | - */ |
|
| 36 | - public function __construct($parentEntity, $relation) |
|
| 37 | - { |
|
| 38 | - $this->addedItems = new EntityCollection; |
|
| 39 | - |
|
| 40 | - parent::__construct($parentEntity, $relation); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Add an entity to the proxy collection, weither it's loaded or not |
|
| 45 | - * |
|
| 46 | - * @param mixed $entity |
|
| 47 | - * @return self|void |
|
| 48 | - */ |
|
| 49 | - public function add($entity) |
|
| 50 | - { |
|
| 51 | - if ($this->isLoaded()) { |
|
| 52 | - return $this->loadedCollection->add($entity); |
|
| 53 | - } else { |
|
| 54 | - $this->addedItems->add($entity); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Check if Proxy collection has been lazy-loaded |
|
| 60 | - * |
|
| 61 | - * @return boolean |
|
| 62 | - */ |
|
| 63 | - public function isLoaded() |
|
| 64 | - { |
|
| 65 | - return !is_null($this->loadedCollection); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Return the underlying collection |
|
| 70 | - * |
|
| 71 | - * @return EntityCollection |
|
| 72 | - */ |
|
| 73 | - public function getUnderlyingCollection() |
|
| 74 | - { |
|
| 75 | - return $this->loadedCollection; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Return Items that has been added prior to lazy-loading |
|
| 80 | - * |
|
| 81 | - * @return EntityCollection |
|
| 82 | - */ |
|
| 83 | - public function getAddedItems() |
|
| 84 | - { |
|
| 85 | - return $this->addedItems; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Load the underlying relation |
|
| 90 | - * |
|
| 91 | - * @return void |
|
| 92 | - */ |
|
| 93 | - protected function loadOnce() |
|
| 94 | - { |
|
| 95 | - if ($this->isLoaded()) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 20 | + /** |
|
| 21 | + * Underlying Lazyloaded collection |
|
| 22 | + * @var EntityCollection |
|
| 23 | + */ |
|
| 24 | + protected $loadedCollection; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Added Items Collection |
|
| 28 | + * @var EntityCollection |
|
| 29 | + */ |
|
| 30 | + protected $addedItems; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @param mixed $parentEntity |
|
| 34 | + * @param string $relation relationship method handled by the proxy. |
|
| 35 | + */ |
|
| 36 | + public function __construct($parentEntity, $relation) |
|
| 37 | + { |
|
| 38 | + $this->addedItems = new EntityCollection; |
|
| 39 | + |
|
| 40 | + parent::__construct($parentEntity, $relation); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Add an entity to the proxy collection, weither it's loaded or not |
|
| 45 | + * |
|
| 46 | + * @param mixed $entity |
|
| 47 | + * @return self|void |
|
| 48 | + */ |
|
| 49 | + public function add($entity) |
|
| 50 | + { |
|
| 51 | + if ($this->isLoaded()) { |
|
| 52 | + return $this->loadedCollection->add($entity); |
|
| 53 | + } else { |
|
| 54 | + $this->addedItems->add($entity); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Check if Proxy collection has been lazy-loaded |
|
| 60 | + * |
|
| 61 | + * @return boolean |
|
| 62 | + */ |
|
| 63 | + public function isLoaded() |
|
| 64 | + { |
|
| 65 | + return !is_null($this->loadedCollection); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Return the underlying collection |
|
| 70 | + * |
|
| 71 | + * @return EntityCollection |
|
| 72 | + */ |
|
| 73 | + public function getUnderlyingCollection() |
|
| 74 | + { |
|
| 75 | + return $this->loadedCollection; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Return Items that has been added prior to lazy-loading |
|
| 80 | + * |
|
| 81 | + * @return EntityCollection |
|
| 82 | + */ |
|
| 83 | + public function getAddedItems() |
|
| 84 | + { |
|
| 85 | + return $this->addedItems; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Load the underlying relation |
|
| 90 | + * |
|
| 91 | + * @return void |
|
| 92 | + */ |
|
| 93 | + protected function loadOnce() |
|
| 94 | + { |
|
| 95 | + if ($this->isLoaded()) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - $this->loadedCollection = $this->load(); |
|
| 99 | + $this->loadedCollection = $this->load(); |
|
| 100 | 100 | |
| 101 | - foreach ($this->addedItems as $entity) { |
|
| 102 | - $this->loadedCollection->add($entity); |
|
| 103 | - } |
|
| 101 | + foreach ($this->addedItems as $entity) { |
|
| 102 | + $this->loadedCollection->add($entity); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - $this->addedItems = null; |
|
| 106 | - } |
|
| 105 | + $this->addedItems = null; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Count the number of items in the collection. |
|
| 110 | - * |
|
| 111 | - * @return int |
|
| 112 | - */ |
|
| 113 | - public function count() |
|
| 114 | - { |
|
| 115 | - $this->loadOnce(); |
|
| 116 | - |
|
| 117 | - return $this->getUnderlyingCollection()->count(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Determine if an item exists at an offset. |
|
| 122 | - * |
|
| 123 | - * @param mixed $key |
|
| 124 | - * @return bool |
|
| 125 | - */ |
|
| 126 | - public function offsetExists($key) |
|
| 127 | - { |
|
| 128 | - $this->loadOnce(); |
|
| 129 | - |
|
| 130 | - return $this->getUnderlyingCollection()->offsetExists($key); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Get an item at a given offset. |
|
| 135 | - * |
|
| 136 | - * @param mixed $key |
|
| 137 | - * @return mixed |
|
| 138 | - */ |
|
| 139 | - public function offsetGet($key) |
|
| 140 | - { |
|
| 141 | - $this->loadOnce(); |
|
| 142 | - |
|
| 143 | - return $this->getUnderlyingCollection()->offsetGet($key); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Set the item at a given offset. |
|
| 148 | - * |
|
| 149 | - * @param mixed $key |
|
| 150 | - * @param mixed $value |
|
| 151 | - */ |
|
| 152 | - public function offsetSet($key, $value) |
|
| 153 | - { |
|
| 154 | - $this->loadOnce(); |
|
| 155 | - |
|
| 156 | - $this->getUnderlyingCollection()->offsetSet($key, $value); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Unset the item at a given offset. |
|
| 161 | - * |
|
| 162 | - * @param string $key |
|
| 163 | - */ |
|
| 164 | - public function offsetUnset($key) |
|
| 165 | - { |
|
| 166 | - $this->loadOnce(); |
|
| 167 | - |
|
| 168 | - $this->getUnderlyingCollection()->offsetUnset($key); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get the collection of items as a plain array. |
|
| 173 | - * |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - public function toArray() |
|
| 177 | - { |
|
| 178 | - $this->loadOnce(); |
|
| 179 | - |
|
| 180 | - return $this->getUnderlyingCollection()->toArray(); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Convert the object into something JSON serializable. |
|
| 185 | - * |
|
| 186 | - * @return array |
|
| 187 | - */ |
|
| 188 | - public function jsonSerialize() |
|
| 189 | - { |
|
| 190 | - $this->loadOnce(); |
|
| 191 | - |
|
| 192 | - return $this->getUnderlyingCollection()->jsonSerialize(); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get the collection of items as JSON. |
|
| 197 | - * |
|
| 198 | - * @param int $options |
|
| 199 | - * @return string |
|
| 200 | - */ |
|
| 201 | - public function toJson($options = 0) |
|
| 202 | - { |
|
| 203 | - $this->loadOnce(); |
|
| 204 | - |
|
| 205 | - return $this->getUnderlyingCollection()->toJson(); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Get an iterator for the items. |
|
| 210 | - * |
|
| 211 | - * @return \ArrayIterator |
|
| 212 | - */ |
|
| 213 | - public function getIterator() |
|
| 214 | - { |
|
| 215 | - $this->loadOnce(); |
|
| 216 | - |
|
| 217 | - return $this->getUnderlyingCollection()->getIterator(); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param $method |
|
| 223 | - * @param $parameters |
|
| 224 | - * @return mixed |
|
| 225 | - */ |
|
| 226 | - public function __call($method, $parameters) |
|
| 227 | - { |
|
| 228 | - if (!$this->isLoaded()) { |
|
| 229 | - $this->loadOnce(); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - return call_user_func_array([$this->loadedCollection, $method], $parameters); |
|
| 233 | - } |
|
| 108 | + /** |
|
| 109 | + * Count the number of items in the collection. |
|
| 110 | + * |
|
| 111 | + * @return int |
|
| 112 | + */ |
|
| 113 | + public function count() |
|
| 114 | + { |
|
| 115 | + $this->loadOnce(); |
|
| 116 | + |
|
| 117 | + return $this->getUnderlyingCollection()->count(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Determine if an item exists at an offset. |
|
| 122 | + * |
|
| 123 | + * @param mixed $key |
|
| 124 | + * @return bool |
|
| 125 | + */ |
|
| 126 | + public function offsetExists($key) |
|
| 127 | + { |
|
| 128 | + $this->loadOnce(); |
|
| 129 | + |
|
| 130 | + return $this->getUnderlyingCollection()->offsetExists($key); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Get an item at a given offset. |
|
| 135 | + * |
|
| 136 | + * @param mixed $key |
|
| 137 | + * @return mixed |
|
| 138 | + */ |
|
| 139 | + public function offsetGet($key) |
|
| 140 | + { |
|
| 141 | + $this->loadOnce(); |
|
| 142 | + |
|
| 143 | + return $this->getUnderlyingCollection()->offsetGet($key); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Set the item at a given offset. |
|
| 148 | + * |
|
| 149 | + * @param mixed $key |
|
| 150 | + * @param mixed $value |
|
| 151 | + */ |
|
| 152 | + public function offsetSet($key, $value) |
|
| 153 | + { |
|
| 154 | + $this->loadOnce(); |
|
| 155 | + |
|
| 156 | + $this->getUnderlyingCollection()->offsetSet($key, $value); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Unset the item at a given offset. |
|
| 161 | + * |
|
| 162 | + * @param string $key |
|
| 163 | + */ |
|
| 164 | + public function offsetUnset($key) |
|
| 165 | + { |
|
| 166 | + $this->loadOnce(); |
|
| 167 | + |
|
| 168 | + $this->getUnderlyingCollection()->offsetUnset($key); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get the collection of items as a plain array. |
|
| 173 | + * |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + public function toArray() |
|
| 177 | + { |
|
| 178 | + $this->loadOnce(); |
|
| 179 | + |
|
| 180 | + return $this->getUnderlyingCollection()->toArray(); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Convert the object into something JSON serializable. |
|
| 185 | + * |
|
| 186 | + * @return array |
|
| 187 | + */ |
|
| 188 | + public function jsonSerialize() |
|
| 189 | + { |
|
| 190 | + $this->loadOnce(); |
|
| 191 | + |
|
| 192 | + return $this->getUnderlyingCollection()->jsonSerialize(); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get the collection of items as JSON. |
|
| 197 | + * |
|
| 198 | + * @param int $options |
|
| 199 | + * @return string |
|
| 200 | + */ |
|
| 201 | + public function toJson($options = 0) |
|
| 202 | + { |
|
| 203 | + $this->loadOnce(); |
|
| 204 | + |
|
| 205 | + return $this->getUnderlyingCollection()->toJson(); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Get an iterator for the items. |
|
| 210 | + * |
|
| 211 | + * @return \ArrayIterator |
|
| 212 | + */ |
|
| 213 | + public function getIterator() |
|
| 214 | + { |
|
| 215 | + $this->loadOnce(); |
|
| 216 | + |
|
| 217 | + return $this->getUnderlyingCollection()->getIterator(); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param $method |
|
| 223 | + * @param $parameters |
|
| 224 | + * @return mixed |
|
| 225 | + */ |
|
| 226 | + public function __call($method, $parameters) |
|
| 227 | + { |
|
| 228 | + if (!$this->isLoaded()) { |
|
| 229 | + $this->loadOnce(); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + return call_user_func_array([$this->loadedCollection, $method], $parameters); |
|
| 233 | + } |
|
| 234 | 234 | } |
@@ -5,17 +5,17 @@ |
||
| 5 | 5 | |
| 6 | 6 | interface ProxyInterface |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Convert a proxy into the underlying related Object |
|
| 10 | - * |
|
| 11 | - * @return Mappable|\Analogue\ORM\EntityCollection |
|
| 12 | - */ |
|
| 13 | - public function load(); |
|
| 8 | + /** |
|
| 9 | + * Convert a proxy into the underlying related Object |
|
| 10 | + * |
|
| 11 | + * @return Mappable|\Analogue\ORM\EntityCollection |
|
| 12 | + */ |
|
| 13 | + public function load(); |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Return true if the underlying relation has been lazy loaded |
|
| 17 | - * |
|
| 18 | - * @return boolean |
|
| 19 | - */ |
|
| 20 | - public function isLoaded(); |
|
| 15 | + /** |
|
| 16 | + * Return true if the underlying relation has been lazy loaded |
|
| 17 | + * |
|
| 18 | + * @return boolean |
|
| 19 | + */ |
|
| 20 | + public function isLoaded(); |
|
| 21 | 21 | } |
@@ -7,87 +7,87 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class Proxy implements ProxyInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The name of the relationship method handled by the proxy. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $relation; |
|
| 10 | + /** |
|
| 11 | + * The name of the relationship method handled by the proxy. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $relation; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Reference to parent entity object |
|
| 19 | - * |
|
| 20 | - * @var \Analogue\ORM\System\InternallyMappable |
|
| 21 | - */ |
|
| 22 | - protected $parentEntity; |
|
| 17 | + /** |
|
| 18 | + * Reference to parent entity object |
|
| 19 | + * |
|
| 20 | + * @var \Analogue\ORM\System\InternallyMappable |
|
| 21 | + */ |
|
| 22 | + protected $parentEntity; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Lazy loaded relation flag |
|
| 26 | - * |
|
| 27 | - * @var boolean |
|
| 28 | - */ |
|
| 29 | - protected $loaded = false; |
|
| 24 | + /** |
|
| 25 | + * Lazy loaded relation flag |
|
| 26 | + * |
|
| 27 | + * @var boolean |
|
| 28 | + */ |
|
| 29 | + protected $loaded = false; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param mixed $parentEntity |
|
| 33 | - * @param string $relation relationship method handled by the proxy. |
|
| 34 | - */ |
|
| 35 | - public function __construct($parentEntity, $relation) |
|
| 36 | - { |
|
| 37 | - $this->parentEntity = $parentEntity; |
|
| 31 | + /** |
|
| 32 | + * @param mixed $parentEntity |
|
| 33 | + * @param string $relation relationship method handled by the proxy. |
|
| 34 | + */ |
|
| 35 | + public function __construct($parentEntity, $relation) |
|
| 36 | + { |
|
| 37 | + $this->parentEntity = $parentEntity; |
|
| 38 | 38 | |
| 39 | - $this->relation = $relation; |
|
| 40 | - } |
|
| 39 | + $this->relation = $relation; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Call the relationship method on the underlying entity map |
|
| 44 | - * |
|
| 45 | - * @throws MappingException |
|
| 46 | - * @return mixed |
|
| 47 | - */ |
|
| 48 | - public function load() |
|
| 49 | - { |
|
| 50 | - $entities = $this->query($this->parentEntity, $this->relation)->getResults($this->relation); |
|
| 42 | + /** |
|
| 43 | + * Call the relationship method on the underlying entity map |
|
| 44 | + * |
|
| 45 | + * @throws MappingException |
|
| 46 | + * @return mixed |
|
| 47 | + */ |
|
| 48 | + public function load() |
|
| 49 | + { |
|
| 50 | + $entities = $this->query($this->parentEntity, $this->relation)->getResults($this->relation); |
|
| 51 | 51 | |
| 52 | - $this->loaded = true; |
|
| 52 | + $this->loaded = true; |
|
| 53 | 53 | |
| 54 | - return $entities; |
|
| 55 | - } |
|
| 54 | + return $entities; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Return true if the underlying relation has been lazy loaded |
|
| 59 | - * |
|
| 60 | - * @return boolean |
|
| 61 | - */ |
|
| 62 | - public function isLoaded() |
|
| 63 | - { |
|
| 64 | - return $this->loaded; |
|
| 65 | - } |
|
| 57 | + /** |
|
| 58 | + * Return true if the underlying relation has been lazy loaded |
|
| 59 | + * |
|
| 60 | + * @return boolean |
|
| 61 | + */ |
|
| 62 | + public function isLoaded() |
|
| 63 | + { |
|
| 64 | + return $this->loaded; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Return the Query Builder on the relation |
|
| 69 | - * |
|
| 70 | - * @param \Analogue\ORM\System\InternallyMappable $entity |
|
| 71 | - * @param string $relation |
|
| 72 | - * @throws MappingException |
|
| 73 | - * @return \Analogue\ORM\System\Query |
|
| 74 | - */ |
|
| 75 | - protected function query($entity, $relation) |
|
| 76 | - { |
|
| 77 | - $entityMap = $this->getMapper($entity)->getEntityMap(); |
|
| 67 | + /** |
|
| 68 | + * Return the Query Builder on the relation |
|
| 69 | + * |
|
| 70 | + * @param \Analogue\ORM\System\InternallyMappable $entity |
|
| 71 | + * @param string $relation |
|
| 72 | + * @throws MappingException |
|
| 73 | + * @return \Analogue\ORM\System\Query |
|
| 74 | + */ |
|
| 75 | + protected function query($entity, $relation) |
|
| 76 | + { |
|
| 77 | + $entityMap = $this->getMapper($entity)->getEntityMap(); |
|
| 78 | 78 | |
| 79 | - return $entityMap->$relation($entity); |
|
| 80 | - } |
|
| 79 | + return $entityMap->$relation($entity); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Get the mapper instance for the entity |
|
| 84 | - * |
|
| 85 | - * @param \Analogue\ORM\System\InternallyMappable $entity |
|
| 86 | - * @throws MappingException |
|
| 87 | - * @return \Analogue\ORM\System\Mapper |
|
| 88 | - */ |
|
| 89 | - protected function getMapper($entity) |
|
| 90 | - { |
|
| 91 | - return Manager::getMapper($entity); |
|
| 92 | - } |
|
| 82 | + /** |
|
| 83 | + * Get the mapper instance for the entity |
|
| 84 | + * |
|
| 85 | + * @param \Analogue\ORM\System\InternallyMappable $entity |
|
| 86 | + * @throws MappingException |
|
| 87 | + * @return \Analogue\ORM\System\Mapper |
|
| 88 | + */ |
|
| 89 | + protected function getMapper($entity) |
|
| 90 | + { |
|
| 91 | + return Manager::getMapper($entity); |
|
| 92 | + } |
|
| 93 | 93 | } |
@@ -13,70 +13,70 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class MapperFactory |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Manager instance |
|
| 18 | - * |
|
| 19 | - * @var \Analogue\ORM\System\Manager |
|
| 20 | - */ |
|
| 21 | - protected $manager; |
|
| 16 | + /** |
|
| 17 | + * Manager instance |
|
| 18 | + * |
|
| 19 | + * @var \Analogue\ORM\System\Manager |
|
| 20 | + */ |
|
| 21 | + protected $manager; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * DriverManager instance |
|
| 25 | - * |
|
| 26 | - * @var \Analogue\ORM\Drivers\Manager |
|
| 27 | - */ |
|
| 28 | - protected $drivers; |
|
| 23 | + /** |
|
| 24 | + * DriverManager instance |
|
| 25 | + * |
|
| 26 | + * @var \Analogue\ORM\Drivers\Manager |
|
| 27 | + */ |
|
| 28 | + protected $drivers; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Event dispatcher instance |
|
| 32 | - * |
|
| 33 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 34 | - */ |
|
| 35 | - protected $dispatcher; |
|
| 30 | + /** |
|
| 31 | + * Event dispatcher instance |
|
| 32 | + * |
|
| 33 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 34 | + */ |
|
| 35 | + protected $dispatcher; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * MapperFactory constructor. |
|
| 39 | - * @param DriverManager $drivers |
|
| 40 | - * @param Dispatcher $dispatcher |
|
| 41 | - * @param Manager $manager |
|
| 42 | - */ |
|
| 43 | - public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
| 44 | - { |
|
| 45 | - $this->drivers = $drivers; |
|
| 37 | + /** |
|
| 38 | + * MapperFactory constructor. |
|
| 39 | + * @param DriverManager $drivers |
|
| 40 | + * @param Dispatcher $dispatcher |
|
| 41 | + * @param Manager $manager |
|
| 42 | + */ |
|
| 43 | + public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
| 44 | + { |
|
| 45 | + $this->drivers = $drivers; |
|
| 46 | 46 | |
| 47 | - $this->dispatcher = $dispatcher; |
|
| 47 | + $this->dispatcher = $dispatcher; |
|
| 48 | 48 | |
| 49 | - $this->manager = $manager; |
|
| 50 | - } |
|
| 49 | + $this->manager = $manager; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Return a new Mapper instance |
|
| 54 | - * |
|
| 55 | - * @param string $entityClass |
|
| 56 | - * @param EntityMap $entityMap |
|
| 57 | - * @return Mapper |
|
| 58 | - */ |
|
| 59 | - public function make($entityClass, EntityMap $entityMap) |
|
| 60 | - { |
|
| 61 | - $driver = $entityMap->getDriver(); |
|
| 52 | + /** |
|
| 53 | + * Return a new Mapper instance |
|
| 54 | + * |
|
| 55 | + * @param string $entityClass |
|
| 56 | + * @param EntityMap $entityMap |
|
| 57 | + * @return Mapper |
|
| 58 | + */ |
|
| 59 | + public function make($entityClass, EntityMap $entityMap) |
|
| 60 | + { |
|
| 61 | + $driver = $entityMap->getDriver(); |
|
| 62 | 62 | |
| 63 | - $connection = $entityMap->getConnection(); |
|
| 63 | + $connection = $entityMap->getConnection(); |
|
| 64 | 64 | |
| 65 | - $adapter = $this->drivers->getAdapter($driver, $connection); |
|
| 65 | + $adapter = $this->drivers->getAdapter($driver, $connection); |
|
| 66 | 66 | |
| 67 | - $entityMap->setDateFormat($adapter->getDateFormat()); |
|
| 67 | + $entityMap->setDateFormat($adapter->getDateFormat()); |
|
| 68 | 68 | |
| 69 | - $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
| 69 | + $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
| 70 | 70 | |
| 71 | - // Fire Initializing Event |
|
| 72 | - $mapper->fireEvent('initializing', $mapper); |
|
| 71 | + // Fire Initializing Event |
|
| 72 | + $mapper->fireEvent('initializing', $mapper); |
|
| 73 | 73 | |
| 74 | - // Proceed necessary parsing on the EntityMap object |
|
| 75 | - $entityMap->initialize(); |
|
| 74 | + // Proceed necessary parsing on the EntityMap object |
|
| 75 | + $entityMap->initialize(); |
|
| 76 | 76 | |
| 77 | - // Fire Initialized Event |
|
| 78 | - $mapper->fireEvent('initialized', $mapper); |
|
| 77 | + // Fire Initialized Event |
|
| 78 | + $mapper->fireEvent('initialized', $mapper); |
|
| 79 | 79 | |
| 80 | - return $mapper; |
|
| 81 | - } |
|
| 80 | + return $mapper; |
|
| 81 | + } |
|
| 82 | 82 | } |
@@ -152,13 +152,13 @@ |
||
| 152 | 152 | $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass); |
| 153 | 153 | |
| 154 | 154 | foreach ($embeddedAttributes as $key) { |
| 155 | - $prefix = snake_case(class_basename($valueClass)) . '_'; |
|
| 155 | + $prefix = snake_case(class_basename($valueClass)).'_'; |
|
| 156 | 156 | |
| 157 | 157 | $voWrapper = $this->factory->make($valueObject); |
| 158 | 158 | |
| 159 | - $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]); |
|
| 159 | + $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]); |
|
| 160 | 160 | |
| 161 | - unset($attributes[$prefix . $key]); |
|
| 161 | + unset($attributes[$prefix.$key]); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | $attributes[$localKey] = $valueObject; |
@@ -11,196 +11,196 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class EntityBuilder |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * The mapper for the entity to build |
|
| 16 | - * @var \Analogue\ORM\System\Mapper |
|
| 17 | - */ |
|
| 18 | - protected $mapper; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The Entity Map for the entity to build. |
|
| 22 | - * |
|
| 23 | - * @var \Analogue\ORM\EntityMap |
|
| 24 | - */ |
|
| 25 | - protected $entityMap; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Relations that will be eager loaded on this query |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $eagerLoads; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Relations that will be lazy loaded on this query |
|
| 36 | - * |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - protected $lazyLoads; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Entity Wrapper Factory |
|
| 43 | - * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 44 | - */ |
|
| 45 | - protected $factory; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * EntityBuilder constructor. |
|
| 49 | - * @param Mapper $mapper |
|
| 50 | - * @param array $eagerLoads |
|
| 51 | - */ |
|
| 52 | - public function __construct(Mapper $mapper, array $eagerLoads) |
|
| 53 | - { |
|
| 54 | - $this->mapper = $mapper; |
|
| 55 | - |
|
| 56 | - $this->entityMap = $mapper->getEntityMap(); |
|
| 57 | - |
|
| 58 | - $this->eagerLoads = $eagerLoads; |
|
| 59 | - |
|
| 60 | - $this->lazyLoads = $this->prepareLazyLoading(); |
|
| 61 | - |
|
| 62 | - $this->entityMap = $mapper->getEntityMap(); |
|
| 63 | - |
|
| 64 | - $this->factory = new Factory; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Convert a result set into an array of entities |
|
| 69 | - * |
|
| 70 | - * @param array $results |
|
| 71 | - * @return array |
|
| 72 | - */ |
|
| 73 | - public function build($results) |
|
| 74 | - { |
|
| 75 | - $entities = []; |
|
| 76 | - |
|
| 77 | - //$prototype = $this->getWrapperPrototype(); |
|
| 78 | - //$prototype = $this->mapper->newInstance(); |
|
| 79 | - |
|
| 80 | - $keyName = $this->entityMap->getKeyName(); |
|
| 81 | - |
|
| 82 | - $tmpCache = []; |
|
| 83 | - |
|
| 84 | - foreach ($results as $result) { |
|
| 85 | - //$instance = clone $prototype; |
|
| 86 | - $instance = $this->getWrapperInstance(); |
|
| 87 | - |
|
| 88 | - $resultArray = (array) $result; |
|
| 89 | - |
|
| 90 | - $tmpCache[$resultArray[$keyName]] = $resultArray; |
|
| 91 | - |
|
| 92 | - // Hydrate any embedded Value Object |
|
| 93 | - $this->hydrateValueObjects($resultArray); |
|
| 94 | - |
|
| 95 | - $resultArray = $this->entityMap->mapColumnsToAttributes($resultArray); |
|
| 96 | - |
|
| 97 | - $instance->setEntityAttributes($resultArray); |
|
| 98 | - |
|
| 99 | - // Hydrate relation attributes with lazyloading proxies |
|
| 100 | - if (count($this->lazyLoads) > 0) { |
|
| 101 | - $proxies = $this->getLazyLoadingProxies($instance); |
|
| 102 | - $instance->setEntityAttributes($resultArray + $proxies); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // Directly Unwrap the entity now that it has been hydrated |
|
| 106 | - $entities[] = $instance->getObject(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - $this->mapper->getEntityCache()->add($tmpCache); |
|
| 110 | - |
|
| 111 | - return $entities; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the correct wrapper prototype corresponding to the object type |
|
| 116 | - * |
|
| 117 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 118 | - * @return InternallyMappable |
|
| 119 | - */ |
|
| 120 | - protected function getWrapperInstance() |
|
| 121 | - { |
|
| 122 | - return $this->factory->make($this->mapper->newInstance()); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Hydrate value object embedded in this entity |
|
| 127 | - * |
|
| 128 | - * @param array $attributes |
|
| 129 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - protected function hydrateValueObjects(& $attributes) |
|
| 133 | - { |
|
| 134 | - foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) { |
|
| 135 | - $this->hydrateValueObject($attributes, $localKey, $valueClass); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Hydrate a single value object |
|
| 141 | - * |
|
| 142 | - * @param array $attributes |
|
| 143 | - * @param string $localKey |
|
| 144 | - * @param string $valueClass |
|
| 145 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 146 | - * @return void |
|
| 147 | - */ |
|
| 148 | - protected function hydrateValueObject(& $attributes, $localKey, $valueClass) |
|
| 149 | - { |
|
| 150 | - $map = $this->mapper->getManager()->getValueMap($valueClass); |
|
| 14 | + /** |
|
| 15 | + * The mapper for the entity to build |
|
| 16 | + * @var \Analogue\ORM\System\Mapper |
|
| 17 | + */ |
|
| 18 | + protected $mapper; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The Entity Map for the entity to build. |
|
| 22 | + * |
|
| 23 | + * @var \Analogue\ORM\EntityMap |
|
| 24 | + */ |
|
| 25 | + protected $entityMap; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Relations that will be eager loaded on this query |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $eagerLoads; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Relations that will be lazy loaded on this query |
|
| 36 | + * |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + protected $lazyLoads; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Entity Wrapper Factory |
|
| 43 | + * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 44 | + */ |
|
| 45 | + protected $factory; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * EntityBuilder constructor. |
|
| 49 | + * @param Mapper $mapper |
|
| 50 | + * @param array $eagerLoads |
|
| 51 | + */ |
|
| 52 | + public function __construct(Mapper $mapper, array $eagerLoads) |
|
| 53 | + { |
|
| 54 | + $this->mapper = $mapper; |
|
| 55 | + |
|
| 56 | + $this->entityMap = $mapper->getEntityMap(); |
|
| 57 | + |
|
| 58 | + $this->eagerLoads = $eagerLoads; |
|
| 59 | + |
|
| 60 | + $this->lazyLoads = $this->prepareLazyLoading(); |
|
| 61 | + |
|
| 62 | + $this->entityMap = $mapper->getEntityMap(); |
|
| 63 | + |
|
| 64 | + $this->factory = new Factory; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Convert a result set into an array of entities |
|
| 69 | + * |
|
| 70 | + * @param array $results |
|
| 71 | + * @return array |
|
| 72 | + */ |
|
| 73 | + public function build($results) |
|
| 74 | + { |
|
| 75 | + $entities = []; |
|
| 76 | + |
|
| 77 | + //$prototype = $this->getWrapperPrototype(); |
|
| 78 | + //$prototype = $this->mapper->newInstance(); |
|
| 79 | + |
|
| 80 | + $keyName = $this->entityMap->getKeyName(); |
|
| 81 | + |
|
| 82 | + $tmpCache = []; |
|
| 83 | + |
|
| 84 | + foreach ($results as $result) { |
|
| 85 | + //$instance = clone $prototype; |
|
| 86 | + $instance = $this->getWrapperInstance(); |
|
| 87 | + |
|
| 88 | + $resultArray = (array) $result; |
|
| 89 | + |
|
| 90 | + $tmpCache[$resultArray[$keyName]] = $resultArray; |
|
| 91 | + |
|
| 92 | + // Hydrate any embedded Value Object |
|
| 93 | + $this->hydrateValueObjects($resultArray); |
|
| 94 | + |
|
| 95 | + $resultArray = $this->entityMap->mapColumnsToAttributes($resultArray); |
|
| 96 | + |
|
| 97 | + $instance->setEntityAttributes($resultArray); |
|
| 98 | + |
|
| 99 | + // Hydrate relation attributes with lazyloading proxies |
|
| 100 | + if (count($this->lazyLoads) > 0) { |
|
| 101 | + $proxies = $this->getLazyLoadingProxies($instance); |
|
| 102 | + $instance->setEntityAttributes($resultArray + $proxies); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // Directly Unwrap the entity now that it has been hydrated |
|
| 106 | + $entities[] = $instance->getObject(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + $this->mapper->getEntityCache()->add($tmpCache); |
|
| 110 | + |
|
| 111 | + return $entities; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the correct wrapper prototype corresponding to the object type |
|
| 116 | + * |
|
| 117 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 118 | + * @return InternallyMappable |
|
| 119 | + */ |
|
| 120 | + protected function getWrapperInstance() |
|
| 121 | + { |
|
| 122 | + return $this->factory->make($this->mapper->newInstance()); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Hydrate value object embedded in this entity |
|
| 127 | + * |
|
| 128 | + * @param array $attributes |
|
| 129 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + protected function hydrateValueObjects(& $attributes) |
|
| 133 | + { |
|
| 134 | + foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) { |
|
| 135 | + $this->hydrateValueObject($attributes, $localKey, $valueClass); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Hydrate a single value object |
|
| 141 | + * |
|
| 142 | + * @param array $attributes |
|
| 143 | + * @param string $localKey |
|
| 144 | + * @param string $valueClass |
|
| 145 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 146 | + * @return void |
|
| 147 | + */ |
|
| 148 | + protected function hydrateValueObject(& $attributes, $localKey, $valueClass) |
|
| 149 | + { |
|
| 150 | + $map = $this->mapper->getManager()->getValueMap($valueClass); |
|
| 151 | 151 | |
| 152 | - $embeddedAttributes = $map->getAttributes(); |
|
| 152 | + $embeddedAttributes = $map->getAttributes(); |
|
| 153 | 153 | |
| 154 | - $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass); |
|
| 154 | + $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass); |
|
| 155 | 155 | |
| 156 | - foreach ($embeddedAttributes as $key) { |
|
| 157 | - $prefix = snake_case(class_basename($valueClass)) . '_'; |
|
| 158 | - |
|
| 159 | - $voWrapper = $this->factory->make($valueObject); |
|
| 160 | - |
|
| 161 | - $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]); |
|
| 156 | + foreach ($embeddedAttributes as $key) { |
|
| 157 | + $prefix = snake_case(class_basename($valueClass)) . '_'; |
|
| 158 | + |
|
| 159 | + $voWrapper = $this->factory->make($valueObject); |
|
| 160 | + |
|
| 161 | + $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]); |
|
| 162 | 162 | |
| 163 | - unset($attributes[$prefix . $key]); |
|
| 164 | - } |
|
| 163 | + unset($attributes[$prefix . $key]); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - $attributes[$localKey] = $valueObject; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Deduce the relationships that will be lazy loaded from the eagerLoads array |
|
| 171 | - * |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - protected function prepareLazyLoading() |
|
| 175 | - { |
|
| 176 | - $relations = $this->entityMap->getRelationships(); |
|
| 166 | + $attributes[$localKey] = $valueObject; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Deduce the relationships that will be lazy loaded from the eagerLoads array |
|
| 171 | + * |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + protected function prepareLazyLoading() |
|
| 175 | + { |
|
| 176 | + $relations = $this->entityMap->getRelationships(); |
|
| 177 | 177 | |
| 178 | - return array_diff($relations, $this->eagerLoads); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Build lazy loading proxies for the current entity |
|
| 183 | - * |
|
| 184 | - * @param InternallyMappable $entity |
|
| 185 | - * |
|
| 186 | - * @return array |
|
| 187 | - */ |
|
| 188 | - protected function getLazyLoadingProxies(InternallyMappable $entity) |
|
| 189 | - { |
|
| 190 | - $proxies = []; |
|
| 191 | - |
|
| 192 | - $singleRelations = $this->entityMap->getSingleRelationships(); |
|
| 193 | - $manyRelations = $this->entityMap->getManyRelationships(); |
|
| 194 | - |
|
| 195 | - foreach ($this->lazyLoads as $relation) { |
|
| 196 | - if (in_array($relation, $singleRelations)) { |
|
| 197 | - $proxies[$relation] = new EntityProxy($entity->getObject(), $relation); |
|
| 198 | - } |
|
| 199 | - if (in_array($relation, $manyRelations)) { |
|
| 200 | - $proxies[$relation] = new CollectionProxy($entity->getObject(), $relation); |
|
| 201 | - } |
|
| 202 | - } |
|
| 178 | + return array_diff($relations, $this->eagerLoads); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Build lazy loading proxies for the current entity |
|
| 183 | + * |
|
| 184 | + * @param InternallyMappable $entity |
|
| 185 | + * |
|
| 186 | + * @return array |
|
| 187 | + */ |
|
| 188 | + protected function getLazyLoadingProxies(InternallyMappable $entity) |
|
| 189 | + { |
|
| 190 | + $proxies = []; |
|
| 191 | + |
|
| 192 | + $singleRelations = $this->entityMap->getSingleRelationships(); |
|
| 193 | + $manyRelations = $this->entityMap->getManyRelationships(); |
|
| 194 | + |
|
| 195 | + foreach ($this->lazyLoads as $relation) { |
|
| 196 | + if (in_array($relation, $singleRelations)) { |
|
| 197 | + $proxies[$relation] = new EntityProxy($entity->getObject(), $relation); |
|
| 198 | + } |
|
| 199 | + if (in_array($relation, $manyRelations)) { |
|
| 200 | + $proxies[$relation] = new CollectionProxy($entity->getObject(), $relation); |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - return $proxies; |
|
| 205 | - } |
|
| 204 | + return $proxies; |
|
| 205 | + } |
|
| 206 | 206 | } |
@@ -231,7 +231,7 @@ |
||
| 231 | 231 | |
| 232 | 232 | $keyName = $mapper->getEntityMap()->getKeyName(); |
| 233 | 233 | |
| 234 | - return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 234 | + return $class.'.'.$entity->getEntityAttribute($keyName); |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |
@@ -15,319 +15,319 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class EntityCache |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Entity's raw attributes/relationships |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $cache = []; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Entity Map for the current Entity Type |
|
| 27 | - * @var \Analogue\ORM\EntityMap |
|
| 28 | - */ |
|
| 29 | - protected $entityMap; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Wrapper factory |
|
| 33 | - * |
|
| 34 | - * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 35 | - */ |
|
| 36 | - protected $factory; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Associative array containing list of pivot attributes per relationship |
|
| 40 | - * so we don't have to call relationship method on refresh. |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $pivotAttributes = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * EntityCache constructor. |
|
| 48 | - * @param EntityMap $entityMap |
|
| 49 | - */ |
|
| 50 | - public function __construct(EntityMap $entityMap) |
|
| 51 | - { |
|
| 52 | - $this->entityMap = $entityMap; |
|
| 53 | - |
|
| 54 | - $this->factory = new Factory; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Add an array of key=>attributes representing |
|
| 59 | - * the initial state of loaded entities. |
|
| 60 | - * |
|
| 61 | - * @param array $entities |
|
| 62 | - */ |
|
| 63 | - public function add(array $entities) |
|
| 64 | - { |
|
| 65 | - if (count($this->cache) == 0) { |
|
| 66 | - $this->cache = $entities; |
|
| 67 | - } else { |
|
| 68 | - $this->mergeCacheResults($entities); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Retrieve initial attributes for a single entity |
|
| 74 | - * |
|
| 75 | - * @param string $id |
|
| 76 | - * @return array |
|
| 77 | - */ |
|
| 78 | - public function get($id) |
|
| 79 | - { |
|
| 80 | - if ($this->has($id)) { |
|
| 81 | - return $this->cache[$id]; |
|
| 82 | - } else { |
|
| 83 | - return []; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Check if a record for this id exists. |
|
| 89 | - * |
|
| 90 | - * @param string $id |
|
| 91 | - * @return boolean |
|
| 92 | - */ |
|
| 93 | - public function has($id) |
|
| 94 | - { |
|
| 95 | - return array_key_exists($id, $this->cache); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Combine new result set with existing attributes in |
|
| 100 | - * cache. |
|
| 101 | - * |
|
| 102 | - * @param array $entities |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - protected function mergeCacheResults($entities) |
|
| 106 | - { |
|
| 107 | - foreach ($entities as $key => $entity) { |
|
| 108 | - $this->cache[$key] = $entity; |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Cache Relation's query result for an entity |
|
| 114 | - * |
|
| 115 | - * @param mixed $parent |
|
| 116 | - * @param string $relation name of the relation |
|
| 117 | - * @param mixed $results results of the relationship's query |
|
| 118 | - * @param Relationship $relationship |
|
| 119 | - * @throws MappingException |
|
| 120 | - * @return void |
|
| 121 | - */ |
|
| 122 | - public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship) |
|
| 123 | - { |
|
| 124 | - $keyName = $this->entityMap->getKeyName(); |
|
| 125 | - |
|
| 126 | - if (!$parent instanceof InternallyMappable) { |
|
| 127 | - $parent = $this->factory->make($parent); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $key = $parent->getEntityAttribute($keyName); |
|
| 18 | + /** |
|
| 19 | + * Entity's raw attributes/relationships |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $cache = []; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Entity Map for the current Entity Type |
|
| 27 | + * @var \Analogue\ORM\EntityMap |
|
| 28 | + */ |
|
| 29 | + protected $entityMap; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Wrapper factory |
|
| 33 | + * |
|
| 34 | + * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 35 | + */ |
|
| 36 | + protected $factory; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Associative array containing list of pivot attributes per relationship |
|
| 40 | + * so we don't have to call relationship method on refresh. |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $pivotAttributes = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * EntityCache constructor. |
|
| 48 | + * @param EntityMap $entityMap |
|
| 49 | + */ |
|
| 50 | + public function __construct(EntityMap $entityMap) |
|
| 51 | + { |
|
| 52 | + $this->entityMap = $entityMap; |
|
| 53 | + |
|
| 54 | + $this->factory = new Factory; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Add an array of key=>attributes representing |
|
| 59 | + * the initial state of loaded entities. |
|
| 60 | + * |
|
| 61 | + * @param array $entities |
|
| 62 | + */ |
|
| 63 | + public function add(array $entities) |
|
| 64 | + { |
|
| 65 | + if (count($this->cache) == 0) { |
|
| 66 | + $this->cache = $entities; |
|
| 67 | + } else { |
|
| 68 | + $this->mergeCacheResults($entities); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Retrieve initial attributes for a single entity |
|
| 74 | + * |
|
| 75 | + * @param string $id |
|
| 76 | + * @return array |
|
| 77 | + */ |
|
| 78 | + public function get($id) |
|
| 79 | + { |
|
| 80 | + if ($this->has($id)) { |
|
| 81 | + return $this->cache[$id]; |
|
| 82 | + } else { |
|
| 83 | + return []; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Check if a record for this id exists. |
|
| 89 | + * |
|
| 90 | + * @param string $id |
|
| 91 | + * @return boolean |
|
| 92 | + */ |
|
| 93 | + public function has($id) |
|
| 94 | + { |
|
| 95 | + return array_key_exists($id, $this->cache); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Combine new result set with existing attributes in |
|
| 100 | + * cache. |
|
| 101 | + * |
|
| 102 | + * @param array $entities |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + protected function mergeCacheResults($entities) |
|
| 106 | + { |
|
| 107 | + foreach ($entities as $key => $entity) { |
|
| 108 | + $this->cache[$key] = $entity; |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Cache Relation's query result for an entity |
|
| 114 | + * |
|
| 115 | + * @param mixed $parent |
|
| 116 | + * @param string $relation name of the relation |
|
| 117 | + * @param mixed $results results of the relationship's query |
|
| 118 | + * @param Relationship $relationship |
|
| 119 | + * @throws MappingException |
|
| 120 | + * @return void |
|
| 121 | + */ |
|
| 122 | + public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship) |
|
| 123 | + { |
|
| 124 | + $keyName = $this->entityMap->getKeyName(); |
|
| 125 | + |
|
| 126 | + if (!$parent instanceof InternallyMappable) { |
|
| 127 | + $parent = $this->factory->make($parent); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $key = $parent->getEntityAttribute($keyName); |
|
| 131 | 131 | |
| 132 | - if ($results instanceof EntityCollection) { |
|
| 133 | - $this->cacheManyRelationResults($key, $relation, $results, $relationship); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // POPO : Maybe this check isn't needed, or we have to check for stdClass |
|
| 137 | - // instead |
|
| 138 | - if ($results instanceof Mappable) { |
|
| 139 | - $this->cacheSingleRelationResult($key, $relation, $results, $relationship); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any. |
|
| 145 | - * |
|
| 146 | - * @param string $parentKey |
|
| 147 | - * @param string $relation |
|
| 148 | - * @param array $result |
|
| 149 | - * @param Relationship $relationship |
|
| 150 | - * @throws MappingException |
|
| 151 | - * @return CachedRelationship |
|
| 152 | - */ |
|
| 153 | - protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship) |
|
| 154 | - { |
|
| 155 | - $pivotColumns = $relationship->getPivotAttributes(); |
|
| 156 | - |
|
| 157 | - if (!array_key_exists($relation, $this->pivotAttributes)) { |
|
| 158 | - $this->pivotAttributes[$relation] = $pivotColumns; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $wrapper = $this->factory->make($result); |
|
| 162 | - |
|
| 163 | - $hash = $this->getEntityHash($wrapper); |
|
| 164 | - |
|
| 165 | - if (count($pivotColumns) > 0) { |
|
| 166 | - $pivotAttributes = []; |
|
| 167 | - foreach ($pivotColumns as $column) { |
|
| 168 | - $pivot = $wrapper->getEntityAttribute('pivot'); |
|
| 169 | - |
|
| 170 | - $pivotWrapper = $this->factory->make($pivot); |
|
| 171 | - |
|
| 172 | - $pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $cachedRelationship = new CachedRelationship($hash, $pivotAttributes); |
|
| 176 | - } else { |
|
| 177 | - $cachedRelationship = new CachedRelationship($hash); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return $cachedRelationship; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Cache a many relationship |
|
| 185 | - * |
|
| 186 | - * @param $parentKey |
|
| 187 | - * @param string $relation |
|
| 188 | - * @param EntityCollection $results |
|
| 189 | - * @param Relationship $relationship |
|
| 190 | - * @throws MappingException |
|
| 191 | - */ |
|
| 192 | - protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship) |
|
| 193 | - { |
|
| 194 | - $this->cache[$parentKey][$relation] = []; |
|
| 195 | - |
|
| 196 | - foreach ($results as $result) { |
|
| 197 | - $cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship); |
|
| 198 | - |
|
| 199 | - $relatedHash = $cachedRelationship->getHash(); |
|
| 200 | - |
|
| 201 | - $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship; |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Cache a single relationship |
|
| 207 | - * |
|
| 208 | - * @param $parentKey |
|
| 209 | - * @param string $relation |
|
| 210 | - * @param Mappable $result |
|
| 211 | - * @param Relationship $relationship |
|
| 212 | - * @throws MappingException |
|
| 213 | - */ |
|
| 214 | - protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship) |
|
| 215 | - { |
|
| 216 | - $this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Get Entity's Hash |
|
| 221 | - * |
|
| 222 | - * @param $entity |
|
| 223 | - * @throws MappingException |
|
| 224 | - * @return string |
|
| 225 | - */ |
|
| 226 | - protected function getEntityHash(InternallyMappable $entity) |
|
| 227 | - { |
|
| 228 | - $class = get_class($entity->getObject()); |
|
| 229 | - |
|
| 230 | - $mapper = Manager::getMapper($class); |
|
| 231 | - |
|
| 232 | - $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 233 | - |
|
| 234 | - return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Refresh the cache record for an aggregated entity after a write operation |
|
| 239 | - * @param Aggregate $entity |
|
| 240 | - */ |
|
| 241 | - public function refresh(Aggregate $entity) |
|
| 242 | - { |
|
| 243 | - $this->cache[$entity->getEntityId()] = $this->transform($entity); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Transform an Aggregated Entity into a cache record |
|
| 248 | - * |
|
| 249 | - * @param Aggregate $aggregatedEntity |
|
| 250 | - * @throws MappingException |
|
| 251 | - * @return array |
|
| 252 | - */ |
|
| 253 | - protected function transform(Aggregate $aggregatedEntity) |
|
| 254 | - { |
|
| 255 | - $baseAttributes = $aggregatedEntity->getRawAttributes(); |
|
| 256 | - |
|
| 257 | - $relationAttributes = []; |
|
| 258 | - |
|
| 259 | - // First we'll handle each relationships that are a one to one |
|
| 260 | - // relation, and which will be saved as a CachedRelationship |
|
| 261 | - // object inside the cache. |
|
| 262 | - |
|
| 263 | - // NOTE : storing localRelationships maybe useless has we store |
|
| 264 | - // the foreign key in the attributes already. |
|
| 265 | - |
|
| 266 | - foreach ($this->entityMap->getSingleRelationships() as $relation) { |
|
| 267 | - $aggregates = $aggregatedEntity->getRelationship($relation); |
|
| 132 | + if ($results instanceof EntityCollection) { |
|
| 133 | + $this->cacheManyRelationResults($key, $relation, $results, $relationship); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // POPO : Maybe this check isn't needed, or we have to check for stdClass |
|
| 137 | + // instead |
|
| 138 | + if ($results instanceof Mappable) { |
|
| 139 | + $this->cacheSingleRelationResult($key, $relation, $results, $relationship); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any. |
|
| 145 | + * |
|
| 146 | + * @param string $parentKey |
|
| 147 | + * @param string $relation |
|
| 148 | + * @param array $result |
|
| 149 | + * @param Relationship $relationship |
|
| 150 | + * @throws MappingException |
|
| 151 | + * @return CachedRelationship |
|
| 152 | + */ |
|
| 153 | + protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship) |
|
| 154 | + { |
|
| 155 | + $pivotColumns = $relationship->getPivotAttributes(); |
|
| 156 | + |
|
| 157 | + if (!array_key_exists($relation, $this->pivotAttributes)) { |
|
| 158 | + $this->pivotAttributes[$relation] = $pivotColumns; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $wrapper = $this->factory->make($result); |
|
| 162 | + |
|
| 163 | + $hash = $this->getEntityHash($wrapper); |
|
| 164 | + |
|
| 165 | + if (count($pivotColumns) > 0) { |
|
| 166 | + $pivotAttributes = []; |
|
| 167 | + foreach ($pivotColumns as $column) { |
|
| 168 | + $pivot = $wrapper->getEntityAttribute('pivot'); |
|
| 169 | + |
|
| 170 | + $pivotWrapper = $this->factory->make($pivot); |
|
| 171 | + |
|
| 172 | + $pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $cachedRelationship = new CachedRelationship($hash, $pivotAttributes); |
|
| 176 | + } else { |
|
| 177 | + $cachedRelationship = new CachedRelationship($hash); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return $cachedRelationship; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Cache a many relationship |
|
| 185 | + * |
|
| 186 | + * @param $parentKey |
|
| 187 | + * @param string $relation |
|
| 188 | + * @param EntityCollection $results |
|
| 189 | + * @param Relationship $relationship |
|
| 190 | + * @throws MappingException |
|
| 191 | + */ |
|
| 192 | + protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship) |
|
| 193 | + { |
|
| 194 | + $this->cache[$parentKey][$relation] = []; |
|
| 195 | + |
|
| 196 | + foreach ($results as $result) { |
|
| 197 | + $cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship); |
|
| 198 | + |
|
| 199 | + $relatedHash = $cachedRelationship->getHash(); |
|
| 200 | + |
|
| 201 | + $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Cache a single relationship |
|
| 207 | + * |
|
| 208 | + * @param $parentKey |
|
| 209 | + * @param string $relation |
|
| 210 | + * @param Mappable $result |
|
| 211 | + * @param Relationship $relationship |
|
| 212 | + * @throws MappingException |
|
| 213 | + */ |
|
| 214 | + protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship) |
|
| 215 | + { |
|
| 216 | + $this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Get Entity's Hash |
|
| 221 | + * |
|
| 222 | + * @param $entity |
|
| 223 | + * @throws MappingException |
|
| 224 | + * @return string |
|
| 225 | + */ |
|
| 226 | + protected function getEntityHash(InternallyMappable $entity) |
|
| 227 | + { |
|
| 228 | + $class = get_class($entity->getObject()); |
|
| 229 | + |
|
| 230 | + $mapper = Manager::getMapper($class); |
|
| 231 | + |
|
| 232 | + $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 233 | + |
|
| 234 | + return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Refresh the cache record for an aggregated entity after a write operation |
|
| 239 | + * @param Aggregate $entity |
|
| 240 | + */ |
|
| 241 | + public function refresh(Aggregate $entity) |
|
| 242 | + { |
|
| 243 | + $this->cache[$entity->getEntityId()] = $this->transform($entity); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Transform an Aggregated Entity into a cache record |
|
| 248 | + * |
|
| 249 | + * @param Aggregate $aggregatedEntity |
|
| 250 | + * @throws MappingException |
|
| 251 | + * @return array |
|
| 252 | + */ |
|
| 253 | + protected function transform(Aggregate $aggregatedEntity) |
|
| 254 | + { |
|
| 255 | + $baseAttributes = $aggregatedEntity->getRawAttributes(); |
|
| 256 | + |
|
| 257 | + $relationAttributes = []; |
|
| 258 | + |
|
| 259 | + // First we'll handle each relationships that are a one to one |
|
| 260 | + // relation, and which will be saved as a CachedRelationship |
|
| 261 | + // object inside the cache. |
|
| 262 | + |
|
| 263 | + // NOTE : storing localRelationships maybe useless has we store |
|
| 264 | + // the foreign key in the attributes already. |
|
| 265 | + |
|
| 266 | + foreach ($this->entityMap->getSingleRelationships() as $relation) { |
|
| 267 | + $aggregates = $aggregatedEntity->getRelationship($relation); |
|
| 268 | 268 | |
| 269 | - if (count($aggregates) == 1) { |
|
| 270 | - $related = $aggregates[0]; |
|
| 271 | - $relationAttributes[$relation] = new CachedRelationship($related->getEntityHash()); |
|
| 272 | - } |
|
| 273 | - if (count($aggregates) > 1) { |
|
| 274 | - throw new MappingException("Single Relationship '$relation' contains several related entities"); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // Then we'll handle the 'many' relationships and store them as |
|
| 279 | - // an array of CachedRelationship objects. |
|
| 280 | - |
|
| 281 | - foreach ($this->entityMap->getManyRelationships() as $relation) { |
|
| 282 | - $aggregates = $aggregatedEntity->getRelationship($relation); |
|
| 283 | - |
|
| 284 | - $relationAttributes[$relation] = []; |
|
| 285 | - |
|
| 286 | - foreach ($aggregates as $aggregate) { |
|
| 287 | - $relationAttributes[$relation][] = new CachedRelationship( |
|
| 288 | - $aggregate->getEntityHash(), |
|
| 289 | - $aggregate->getPivotAttributes() |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return $baseAttributes + $relationAttributes; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Get pivot attributes for a relation |
|
| 299 | - * |
|
| 300 | - * @param string $relation |
|
| 301 | - * @param InternallyMappable $entity |
|
| 302 | - * @return array |
|
| 303 | - */ |
|
| 304 | - protected function getPivotValues($relation, InternallyMappable $entity) |
|
| 305 | - { |
|
| 306 | - $values = []; |
|
| 307 | - |
|
| 308 | - $entityAttributes = $entity->getEntityAttributes(); |
|
| 309 | - |
|
| 310 | - if (array_key_exists($relation, $this->pivotAttributes)) { |
|
| 311 | - foreach ($this->pivotAttributes[$relation] as $attribute) { |
|
| 312 | - if (array_key_exists($attribute, $entityAttributes)) { |
|
| 313 | - $values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute; |
|
| 314 | - } |
|
| 315 | - } |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - return $values; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Clear the entity Cache. Use with caution as it could result |
|
| 323 | - * in impredictable behaviour if the cached entities are stored |
|
| 324 | - * after the cache clear operation. |
|
| 325 | - * |
|
| 326 | - * @return void |
|
| 327 | - */ |
|
| 328 | - public function clear() |
|
| 329 | - { |
|
| 330 | - $this->cache = []; |
|
| 331 | - $this->pivotAttributes = []; |
|
| 332 | - } |
|
| 269 | + if (count($aggregates) == 1) { |
|
| 270 | + $related = $aggregates[0]; |
|
| 271 | + $relationAttributes[$relation] = new CachedRelationship($related->getEntityHash()); |
|
| 272 | + } |
|
| 273 | + if (count($aggregates) > 1) { |
|
| 274 | + throw new MappingException("Single Relationship '$relation' contains several related entities"); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // Then we'll handle the 'many' relationships and store them as |
|
| 279 | + // an array of CachedRelationship objects. |
|
| 280 | + |
|
| 281 | + foreach ($this->entityMap->getManyRelationships() as $relation) { |
|
| 282 | + $aggregates = $aggregatedEntity->getRelationship($relation); |
|
| 283 | + |
|
| 284 | + $relationAttributes[$relation] = []; |
|
| 285 | + |
|
| 286 | + foreach ($aggregates as $aggregate) { |
|
| 287 | + $relationAttributes[$relation][] = new CachedRelationship( |
|
| 288 | + $aggregate->getEntityHash(), |
|
| 289 | + $aggregate->getPivotAttributes() |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return $baseAttributes + $relationAttributes; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Get pivot attributes for a relation |
|
| 299 | + * |
|
| 300 | + * @param string $relation |
|
| 301 | + * @param InternallyMappable $entity |
|
| 302 | + * @return array |
|
| 303 | + */ |
|
| 304 | + protected function getPivotValues($relation, InternallyMappable $entity) |
|
| 305 | + { |
|
| 306 | + $values = []; |
|
| 307 | + |
|
| 308 | + $entityAttributes = $entity->getEntityAttributes(); |
|
| 309 | + |
|
| 310 | + if (array_key_exists($relation, $this->pivotAttributes)) { |
|
| 311 | + foreach ($this->pivotAttributes[$relation] as $attribute) { |
|
| 312 | + if (array_key_exists($attribute, $entityAttributes)) { |
|
| 313 | + $values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute; |
|
| 314 | + } |
|
| 315 | + } |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + return $values; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Clear the entity Cache. Use with caution as it could result |
|
| 323 | + * in impredictable behaviour if the cached entities are stored |
|
| 324 | + * after the cache clear operation. |
|
| 325 | + * |
|
| 326 | + * @return void |
|
| 327 | + */ |
|
| 328 | + public function clear() |
|
| 329 | + { |
|
| 330 | + $this->cache = []; |
|
| 331 | + $this->pivotAttributes = []; |
|
| 332 | + } |
|
| 333 | 333 | } |