@@ -6,201 +6,201 @@ |
||
| 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 | - return $this->reflection->getProperty($name); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Hydrate Plain PHP Object with wrapped attributes |
|
| 102 | - * |
|
| 103 | - * @param $attributes |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - protected function hydrate($attributes) |
|
| 107 | - { |
|
| 108 | - $properties = $this->getMappedProperties(); |
|
| 109 | - |
|
| 110 | - foreach ($properties as $property) { |
|
| 111 | - $name = $property->getName(); |
|
| 112 | - |
|
| 113 | - if ($property->isPublic()) { |
|
| 114 | - $this->entity->$name = $attributes[$name]; |
|
| 115 | - } else { |
|
| 116 | - $property->setAccessible(true); |
|
| 117 | - |
|
| 118 | - $property->setValue($this->entity, $attributes[$name]); |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Method used by the mapper to set the object |
|
| 125 | - * attribute raw values (hydration) |
|
| 126 | - * |
|
| 127 | - * @param array $attributes |
|
| 128 | - * |
|
| 129 | - * @return void |
|
| 130 | - */ |
|
| 131 | - public function setEntityAttributes(array $attributes) |
|
| 132 | - { |
|
| 133 | - $this->hydrate($attributes); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Method used by the mapper to get the |
|
| 138 | - * raw object's values. |
|
| 139 | - * |
|
| 140 | - * @return array |
|
| 141 | - */ |
|
| 142 | - public function getEntityAttributes() |
|
| 143 | - { |
|
| 144 | - return $this->extract(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Method used by the mapper to set raw |
|
| 149 | - * key-value pair |
|
| 150 | - * |
|
| 151 | - * @param string $key |
|
| 152 | - * @param string $value |
|
| 153 | - * |
|
| 154 | - * @return void |
|
| 155 | - */ |
|
| 156 | - public function setEntityAttribute($key, $value) |
|
| 157 | - { |
|
| 158 | - $property = $this->getMappedProperty($key); |
|
| 159 | - |
|
| 160 | - if ($property->isPublic()) { |
|
| 161 | - $this->entity->$key = $value; |
|
| 162 | - } else { |
|
| 163 | - $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 | + return $this->reflection->getProperty($name); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Hydrate Plain PHP Object with wrapped attributes |
|
| 102 | + * |
|
| 103 | + * @param $attributes |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + protected function hydrate($attributes) |
|
| 107 | + { |
|
| 108 | + $properties = $this->getMappedProperties(); |
|
| 109 | + |
|
| 110 | + foreach ($properties as $property) { |
|
| 111 | + $name = $property->getName(); |
|
| 112 | + |
|
| 113 | + if ($property->isPublic()) { |
|
| 114 | + $this->entity->$name = $attributes[$name]; |
|
| 115 | + } else { |
|
| 116 | + $property->setAccessible(true); |
|
| 117 | + |
|
| 118 | + $property->setValue($this->entity, $attributes[$name]); |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Method used by the mapper to set the object |
|
| 125 | + * attribute raw values (hydration) |
|
| 126 | + * |
|
| 127 | + * @param array $attributes |
|
| 128 | + * |
|
| 129 | + * @return void |
|
| 130 | + */ |
|
| 131 | + public function setEntityAttributes(array $attributes) |
|
| 132 | + { |
|
| 133 | + $this->hydrate($attributes); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Method used by the mapper to get the |
|
| 138 | + * raw object's values. |
|
| 139 | + * |
|
| 140 | + * @return array |
|
| 141 | + */ |
|
| 142 | + public function getEntityAttributes() |
|
| 143 | + { |
|
| 144 | + return $this->extract(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Method used by the mapper to set raw |
|
| 149 | + * key-value pair |
|
| 150 | + * |
|
| 151 | + * @param string $key |
|
| 152 | + * @param string $value |
|
| 153 | + * |
|
| 154 | + * @return void |
|
| 155 | + */ |
|
| 156 | + public function setEntityAttribute($key, $value) |
|
| 157 | + { |
|
| 158 | + $property = $this->getMappedProperty($key); |
|
| 159 | + |
|
| 160 | + if ($property->isPublic()) { |
|
| 161 | + $this->entity->$key = $value; |
|
| 162 | + } else { |
|
| 163 | + $property->setAccessible(true); |
|
| 164 | 164 | |
| 165 | - $property->setValue($this->entity, $value); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $this->attributes[$key] = $value; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Method used by the mapper to get single |
|
| 173 | - * key-value pair |
|
| 174 | - * |
|
| 175 | - * @param string $key |
|
| 176 | - * @return mixed |
|
| 177 | - */ |
|
| 178 | - public function getEntityAttribute($key) |
|
| 179 | - { |
|
| 180 | - $property = $this->getMappedProperty($key); |
|
| 181 | - |
|
| 182 | - if ($property->isPublic()) { |
|
| 183 | - $value = $this->entity->$key; |
|
| 184 | - } else { |
|
| 185 | - $property->setAccessible(true); |
|
| 186 | - $value = $property->getValue($this->entity); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - return $value; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Test if a given attribute exists |
|
| 194 | - * |
|
| 195 | - * @param string $key |
|
| 196 | - * @return boolean |
|
| 197 | - */ |
|
| 198 | - public function hasAttribute($key) |
|
| 199 | - { |
|
| 200 | - if (array_key_exists($key, $this->attributeList)) { |
|
| 201 | - return true; |
|
| 202 | - } else { |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - } |
|
| 165 | + $property->setValue($this->entity, $value); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $this->attributes[$key] = $value; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Method used by the mapper to get single |
|
| 173 | + * key-value pair |
|
| 174 | + * |
|
| 175 | + * @param string $key |
|
| 176 | + * @return mixed |
|
| 177 | + */ |
|
| 178 | + public function getEntityAttribute($key) |
|
| 179 | + { |
|
| 180 | + $property = $this->getMappedProperty($key); |
|
| 181 | + |
|
| 182 | + if ($property->isPublic()) { |
|
| 183 | + $value = $this->entity->$key; |
|
| 184 | + } else { |
|
| 185 | + $property->setAccessible(true); |
|
| 186 | + $value = $property->getValue($this->entity); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + return $value; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Test if a given attribute exists |
|
| 194 | + * |
|
| 195 | + * @param string $key |
|
| 196 | + * @return boolean |
|
| 197 | + */ |
|
| 198 | + public function hasAttribute($key) |
|
| 199 | + { |
|
| 200 | + if (array_key_exists($key, $this->attributeList)) { |
|
| 201 | + return true; |
|
| 202 | + } else { |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | 206 | } |