@@ -11,191 +11,191 @@ |
||
| 11 | 11 | |
| 12 | 12 | class ValueObject implements Mappable, ArrayAccess, Jsonable, JsonSerializable, Arrayable |
| 13 | 13 | { |
| 14 | - use MappableTrait; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * Dynamically retrieve attributes on the entity. |
|
| 18 | - * |
|
| 19 | - * @param string $key |
|
| 20 | - * @return mixed |
|
| 21 | - */ |
|
| 22 | - public function __get($key) |
|
| 23 | - { |
|
| 24 | - return $this->attributes[$key]; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Dynamically set attributes on the entity. |
|
| 29 | - * |
|
| 30 | - * @param string $key |
|
| 31 | - * @param mixed $value |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function __set($key, $value) |
|
| 35 | - { |
|
| 36 | - $this->attributes[$key] = $value; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Determine if an attribute exists on the entity. |
|
| 41 | - * |
|
| 42 | - * @param string $key |
|
| 43 | - * @return bool |
|
| 44 | - */ |
|
| 45 | - public function __isset($key) |
|
| 46 | - { |
|
| 47 | - return array_key_exists($key, $this->attributes); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Unset an attribute on the entity. |
|
| 52 | - * |
|
| 53 | - * @param string $key |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - public function __unset($key) |
|
| 57 | - { |
|
| 58 | - unset($this->attributes[$key]); |
|
| 59 | - } |
|
| 14 | + use MappableTrait; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * Dynamically retrieve attributes on the entity. |
|
| 18 | + * |
|
| 19 | + * @param string $key |
|
| 20 | + * @return mixed |
|
| 21 | + */ |
|
| 22 | + public function __get($key) |
|
| 23 | + { |
|
| 24 | + return $this->attributes[$key]; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Dynamically set attributes on the entity. |
|
| 29 | + * |
|
| 30 | + * @param string $key |
|
| 31 | + * @param mixed $value |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function __set($key, $value) |
|
| 35 | + { |
|
| 36 | + $this->attributes[$key] = $value; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Determine if an attribute exists on the entity. |
|
| 41 | + * |
|
| 42 | + * @param string $key |
|
| 43 | + * @return bool |
|
| 44 | + */ |
|
| 45 | + public function __isset($key) |
|
| 46 | + { |
|
| 47 | + return array_key_exists($key, $this->attributes); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Unset an attribute on the entity. |
|
| 52 | + * |
|
| 53 | + * @param string $key |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + public function __unset($key) |
|
| 57 | + { |
|
| 58 | + unset($this->attributes[$key]); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Determine if the given attribute exists. |
|
| 64 | - * |
|
| 65 | - * @param mixed $offset |
|
| 66 | - * @return bool |
|
| 67 | - */ |
|
| 68 | - public function offsetExists($offset) |
|
| 69 | - { |
|
| 70 | - return isset($this->$offset); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Get the value for a given offset. |
|
| 75 | - * |
|
| 76 | - * @param mixed $offset |
|
| 77 | - * @return mixed |
|
| 78 | - */ |
|
| 79 | - public function offsetGet($offset) |
|
| 80 | - { |
|
| 81 | - return $this->$offset; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Set the value for a given offset. |
|
| 86 | - * |
|
| 87 | - * @param mixed $offset |
|
| 88 | - * @param mixed $value |
|
| 89 | - * @return void |
|
| 90 | - */ |
|
| 91 | - public function offsetSet($offset, $value) |
|
| 92 | - { |
|
| 93 | - $this->$offset = $value; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Unset the value for a given offset. |
|
| 98 | - * |
|
| 99 | - * @param mixed $offset |
|
| 100 | - * @return void |
|
| 101 | - */ |
|
| 102 | - public function offsetUnset($offset) |
|
| 103 | - { |
|
| 104 | - unset($this->$offset); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Convert the object into something JSON serializable. |
|
| 109 | - * |
|
| 110 | - * @return array |
|
| 111 | - */ |
|
| 112 | - public function jsonSerialize() |
|
| 113 | - { |
|
| 114 | - return $this->toArray(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Convert the entity instance to JSON. |
|
| 119 | - * |
|
| 120 | - * @param int $options |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function toJson($options = 0) |
|
| 124 | - { |
|
| 125 | - return json_encode($this->toArray(), $options); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Convert Mappable object to array; |
|
| 130 | - * |
|
| 131 | - * @return array |
|
| 132 | - */ |
|
| 133 | - public function toArray() |
|
| 134 | - { |
|
| 135 | - return $this->attributesToArray($this->attributes); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Transform the Object to array/json, |
|
| 140 | - * |
|
| 141 | - * @param array $sourceAttributes |
|
| 142 | - * @return array |
|
| 143 | - */ |
|
| 144 | - protected function attributesToArray(array $sourceAttributes) |
|
| 145 | - { |
|
| 146 | - $attributes = []; |
|
| 147 | - |
|
| 148 | - foreach ($sourceAttributes as $key => $attribute) { |
|
| 149 | - // If the attribute is a proxy, and hasn't be loaded, we discard |
|
| 150 | - // it from the returned set. |
|
| 151 | - if ($attribute instanceof ProxyInterface && !$attribute->isLoaded()) { |
|
| 152 | - continue; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if ($attribute instanceof Carbon) { |
|
| 156 | - $attributes[$key] = $attribute->__toString(); |
|
| 157 | - continue; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if ($attribute instanceof Arrayable) { |
|
| 161 | - $attributes[$key] = $attribute->toArray(); |
|
| 162 | - } else { |
|
| 163 | - $attributes[$key] = $attribute; |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - return $attributes; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Create entity with arbitrary arguments |
|
| 171 | - * |
|
| 172 | - * @param array|string $argumentsOrPrimaryKey |
|
| 173 | - * @return $this |
|
| 174 | - * @throws MappingException |
|
| 175 | - * @throws \InvalidArgumentException |
|
| 176 | - */ |
|
| 177 | - public static function mock($argumentsOrPrimaryKey) |
|
| 178 | - { |
|
| 179 | - $arguments = $argumentsOrPrimaryKey; |
|
| 180 | - |
|
| 181 | - if (!is_array($argumentsOrPrimaryKey)) { |
|
| 182 | - $primaryKey = Manager::getInstance() |
|
| 183 | - ->mapper(static::class) |
|
| 184 | - ->getEntityMap() |
|
| 185 | - ->getKeyName(); |
|
| 186 | - |
|
| 187 | - $arguments = [$primaryKey => $argumentsOrPrimaryKey]; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $instance = (new \ReflectionClass(static::class)) |
|
| 191 | - ->newInstanceWithoutConstructor(); |
|
| 192 | - |
|
| 193 | - $arguments = $instance->attributesToArray($arguments); |
|
| 194 | - |
|
| 195 | - foreach ($arguments as $key => $value) { |
|
| 196 | - $instance->$key = $value; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $instance; |
|
| 200 | - } |
|
| 62 | + /** |
|
| 63 | + * Determine if the given attribute exists. |
|
| 64 | + * |
|
| 65 | + * @param mixed $offset |
|
| 66 | + * @return bool |
|
| 67 | + */ |
|
| 68 | + public function offsetExists($offset) |
|
| 69 | + { |
|
| 70 | + return isset($this->$offset); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Get the value for a given offset. |
|
| 75 | + * |
|
| 76 | + * @param mixed $offset |
|
| 77 | + * @return mixed |
|
| 78 | + */ |
|
| 79 | + public function offsetGet($offset) |
|
| 80 | + { |
|
| 81 | + return $this->$offset; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Set the value for a given offset. |
|
| 86 | + * |
|
| 87 | + * @param mixed $offset |
|
| 88 | + * @param mixed $value |
|
| 89 | + * @return void |
|
| 90 | + */ |
|
| 91 | + public function offsetSet($offset, $value) |
|
| 92 | + { |
|
| 93 | + $this->$offset = $value; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Unset the value for a given offset. |
|
| 98 | + * |
|
| 99 | + * @param mixed $offset |
|
| 100 | + * @return void |
|
| 101 | + */ |
|
| 102 | + public function offsetUnset($offset) |
|
| 103 | + { |
|
| 104 | + unset($this->$offset); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Convert the object into something JSON serializable. |
|
| 109 | + * |
|
| 110 | + * @return array |
|
| 111 | + */ |
|
| 112 | + public function jsonSerialize() |
|
| 113 | + { |
|
| 114 | + return $this->toArray(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Convert the entity instance to JSON. |
|
| 119 | + * |
|
| 120 | + * @param int $options |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function toJson($options = 0) |
|
| 124 | + { |
|
| 125 | + return json_encode($this->toArray(), $options); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Convert Mappable object to array; |
|
| 130 | + * |
|
| 131 | + * @return array |
|
| 132 | + */ |
|
| 133 | + public function toArray() |
|
| 134 | + { |
|
| 135 | + return $this->attributesToArray($this->attributes); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Transform the Object to array/json, |
|
| 140 | + * |
|
| 141 | + * @param array $sourceAttributes |
|
| 142 | + * @return array |
|
| 143 | + */ |
|
| 144 | + protected function attributesToArray(array $sourceAttributes) |
|
| 145 | + { |
|
| 146 | + $attributes = []; |
|
| 147 | + |
|
| 148 | + foreach ($sourceAttributes as $key => $attribute) { |
|
| 149 | + // If the attribute is a proxy, and hasn't be loaded, we discard |
|
| 150 | + // it from the returned set. |
|
| 151 | + if ($attribute instanceof ProxyInterface && !$attribute->isLoaded()) { |
|
| 152 | + continue; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if ($attribute instanceof Carbon) { |
|
| 156 | + $attributes[$key] = $attribute->__toString(); |
|
| 157 | + continue; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + if ($attribute instanceof Arrayable) { |
|
| 161 | + $attributes[$key] = $attribute->toArray(); |
|
| 162 | + } else { |
|
| 163 | + $attributes[$key] = $attribute; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + return $attributes; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Create entity with arbitrary arguments |
|
| 171 | + * |
|
| 172 | + * @param array|string $argumentsOrPrimaryKey |
|
| 173 | + * @return $this |
|
| 174 | + * @throws MappingException |
|
| 175 | + * @throws \InvalidArgumentException |
|
| 176 | + */ |
|
| 177 | + public static function mock($argumentsOrPrimaryKey) |
|
| 178 | + { |
|
| 179 | + $arguments = $argumentsOrPrimaryKey; |
|
| 180 | + |
|
| 181 | + if (!is_array($argumentsOrPrimaryKey)) { |
|
| 182 | + $primaryKey = Manager::getInstance() |
|
| 183 | + ->mapper(static::class) |
|
| 184 | + ->getEntityMap() |
|
| 185 | + ->getKeyName(); |
|
| 186 | + |
|
| 187 | + $arguments = [$primaryKey => $argumentsOrPrimaryKey]; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $instance = (new \ReflectionClass(static::class)) |
|
| 191 | + ->newInstanceWithoutConstructor(); |
|
| 192 | + |
|
| 193 | + $arguments = $instance->attributesToArray($arguments); |
|
| 194 | + |
|
| 195 | + foreach ($arguments as $key => $value) { |
|
| 196 | + $instance->$key = $value; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $instance; |
|
| 200 | + } |
|
| 201 | 201 | } |