@@ -6,133 +6,133 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Entity extends ValueObject |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Entities Hidden Attributes, that will be discarded when converting |
|
| 11 | - * the entity to Array/Json |
|
| 12 | - * (can include any embedded object's attribute) |
|
| 13 | - * |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - protected $hidden = []; |
|
| 9 | + /** |
|
| 10 | + * Entities Hidden Attributes, that will be discarded when converting |
|
| 11 | + * the entity to Array/Json |
|
| 12 | + * (can include any embedded object's attribute) |
|
| 13 | + * |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + protected $hidden = []; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Return the entity's attribute |
|
| 20 | - * @param string $key |
|
| 21 | - * @return mixed |
|
| 22 | - */ |
|
| 23 | - public function __get($key) |
|
| 24 | - { |
|
| 25 | - if ($this->hasGetMutator($key)) { |
|
| 26 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 18 | + /** |
|
| 19 | + * Return the entity's attribute |
|
| 20 | + * @param string $key |
|
| 21 | + * @return mixed |
|
| 22 | + */ |
|
| 23 | + public function __get($key) |
|
| 24 | + { |
|
| 25 | + if ($this->hasGetMutator($key)) { |
|
| 26 | + $method = 'get' . $this->getMutatorMethod($key); |
|
| 27 | 27 | |
| 28 | - $attribute = null; |
|
| 28 | + $attribute = null; |
|
| 29 | 29 | |
| 30 | - if (isset($this->attributes[$key])) { |
|
| 31 | - $attribute = $this->attributes[$key]; |
|
| 32 | - } |
|
| 30 | + if (isset($this->attributes[$key])) { |
|
| 31 | + $attribute = $this->attributes[$key]; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return $this->$method($attribute); |
|
| 35 | - } |
|
| 36 | - if (!array_key_exists($key, $this->attributes)) { |
|
| 37 | - return null; |
|
| 38 | - } |
|
| 39 | - if ($this->attributes[$key] instanceof EntityProxy) { |
|
| 40 | - $this->attributes[$key] = $this->attributes[$key]->load(); |
|
| 41 | - } |
|
| 42 | - return $this->attributes[$key]; |
|
| 43 | - } |
|
| 34 | + return $this->$method($attribute); |
|
| 35 | + } |
|
| 36 | + if (!array_key_exists($key, $this->attributes)) { |
|
| 37 | + return null; |
|
| 38 | + } |
|
| 39 | + if ($this->attributes[$key] instanceof EntityProxy) { |
|
| 40 | + $this->attributes[$key] = $this->attributes[$key]->load(); |
|
| 41 | + } |
|
| 42 | + return $this->attributes[$key]; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Dynamically set attributes on the entity. |
|
| 47 | - * |
|
| 48 | - * @param string $key |
|
| 49 | - * @param mixed $value |
|
| 50 | - * @return void |
|
| 51 | - */ |
|
| 52 | - public function __set($key, $value) |
|
| 53 | - { |
|
| 54 | - if ($this->hasSetMutator($key)) { |
|
| 55 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 45 | + /** |
|
| 46 | + * Dynamically set attributes on the entity. |
|
| 47 | + * |
|
| 48 | + * @param string $key |
|
| 49 | + * @param mixed $value |
|
| 50 | + * @return void |
|
| 51 | + */ |
|
| 52 | + public function __set($key, $value) |
|
| 53 | + { |
|
| 54 | + if ($this->hasSetMutator($key)) { |
|
| 55 | + $method = 'set' . $this->getMutatorMethod($key); |
|
| 56 | 56 | |
| 57 | - $this->$method($value); |
|
| 58 | - } else { |
|
| 59 | - $this->attributes[$key] = $value; |
|
| 60 | - } |
|
| 61 | - } |
|
| 57 | + $this->$method($value); |
|
| 58 | + } else { |
|
| 59 | + $this->attributes[$key] = $value; |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Is a getter method defined ? |
|
| 65 | - * |
|
| 66 | - * @param string $key |
|
| 67 | - * @return boolean |
|
| 68 | - */ |
|
| 69 | - protected function hasGetMutator($key) |
|
| 70 | - { |
|
| 71 | - return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
| 72 | - } |
|
| 63 | + /** |
|
| 64 | + * Is a getter method defined ? |
|
| 65 | + * |
|
| 66 | + * @param string $key |
|
| 67 | + * @return boolean |
|
| 68 | + */ |
|
| 69 | + protected function hasGetMutator($key) |
|
| 70 | + { |
|
| 71 | + return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Is a setter method defined ? |
|
| 76 | - * |
|
| 77 | - * @param string $key |
|
| 78 | - * @return boolean |
|
| 79 | - */ |
|
| 80 | - protected function hasSetMutator($key) |
|
| 81 | - { |
|
| 82 | - return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
| 83 | - } |
|
| 74 | + /** |
|
| 75 | + * Is a setter method defined ? |
|
| 76 | + * |
|
| 77 | + * @param string $key |
|
| 78 | + * @return boolean |
|
| 79 | + */ |
|
| 80 | + protected function hasSetMutator($key) |
|
| 81 | + { |
|
| 82 | + return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * @param $key |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - protected function getMutatorMethod($key) |
|
| 90 | - { |
|
| 91 | - $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
| 92 | - return str_replace(' ', '', $key) . "Attribute"; |
|
| 93 | - } |
|
| 85 | + /** |
|
| 86 | + * @param $key |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + protected function getMutatorMethod($key) |
|
| 90 | + { |
|
| 91 | + $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
| 92 | + return str_replace(' ', '', $key) . "Attribute"; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Convert every attributes to value / arrays |
|
| 97 | - * |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - public function toArray() |
|
| 101 | - { |
|
| 102 | - // First, call the trait method before filtering |
|
| 103 | - // with Entity specific methods |
|
| 104 | - $attributes = $this->attributesToArray($this->attributes); |
|
| 95 | + /** |
|
| 96 | + * Convert every attributes to value / arrays |
|
| 97 | + * |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + public function toArray() |
|
| 101 | + { |
|
| 102 | + // First, call the trait method before filtering |
|
| 103 | + // with Entity specific methods |
|
| 104 | + $attributes = $this->attributesToArray($this->attributes); |
|
| 105 | 105 | |
| 106 | - foreach ($this->attributes as $key => $attribute) { |
|
| 107 | - if (in_array($key, $this->hidden)) { |
|
| 108 | - unset($attributes[$key]); |
|
| 109 | - continue; |
|
| 110 | - } |
|
| 111 | - if ($this->hasGetMutator($key)) { |
|
| 112 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 113 | - $attributes[$key] = $this->$method($attribute); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - return $attributes; |
|
| 117 | - } |
|
| 106 | + foreach ($this->attributes as $key => $attribute) { |
|
| 107 | + if (in_array($key, $this->hidden)) { |
|
| 108 | + unset($attributes[$key]); |
|
| 109 | + continue; |
|
| 110 | + } |
|
| 111 | + if ($this->hasGetMutator($key)) { |
|
| 112 | + $method = 'get' . $this->getMutatorMethod($key); |
|
| 113 | + $attributes[$key] = $this->$method($attribute); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + return $attributes; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Fill an entity with key-value pairs |
|
| 121 | - * |
|
| 122 | - * @param array $attributes |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function fill(array $attributes) |
|
| 126 | - { |
|
| 127 | - foreach ($attributes as $key => $attribute) { |
|
| 119 | + /** |
|
| 120 | + * Fill an entity with key-value pairs |
|
| 121 | + * |
|
| 122 | + * @param array $attributes |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function fill(array $attributes) |
|
| 126 | + { |
|
| 127 | + foreach ($attributes as $key => $attribute) { |
|
| 128 | 128 | |
| 129 | - if ($this->hasSetMutator($key)) { |
|
| 130 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 131 | - $this->attributes[$key] = $this->$method($attribute); |
|
| 132 | - } |
|
| 133 | - else { |
|
| 134 | - $this->attributes[$key] = $attribute; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - } |
|
| 129 | + if ($this->hasSetMutator($key)) { |
|
| 130 | + $method = 'set' . $this->getMutatorMethod($key); |
|
| 131 | + $this->attributes[$key] = $this->$method($attribute); |
|
| 132 | + } |
|
| 133 | + else { |
|
| 134 | + $this->attributes[$key] = $attribute; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | 138 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | public function __get($key) |
| 24 | 24 | { |
| 25 | 25 | if ($this->hasGetMutator($key)) { |
| 26 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 26 | + $method = 'get'.$this->getMutatorMethod($key); |
|
| 27 | 27 | |
| 28 | 28 | $attribute = null; |
| 29 | 29 | |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | public function __set($key, $value) |
| 53 | 53 | { |
| 54 | 54 | if ($this->hasSetMutator($key)) { |
| 55 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 55 | + $method = 'set'.$this->getMutatorMethod($key); |
|
| 56 | 56 | |
| 57 | 57 | $this->$method($value); |
| 58 | 58 | } else { |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | protected function hasGetMutator($key) |
| 70 | 70 | { |
| 71 | - return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
| 71 | + return method_exists($this, 'get'.$this->getMutatorMethod($key)) ? true : false; |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | protected function hasSetMutator($key) |
| 81 | 81 | { |
| 82 | - return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
| 82 | + return method_exists($this, 'set'.$this->getMutatorMethod($key)) ? true : false; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | protected function getMutatorMethod($key) |
| 90 | 90 | { |
| 91 | 91 | $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
| 92 | - return str_replace(' ', '', $key) . "Attribute"; |
|
| 92 | + return str_replace(' ', '', $key)."Attribute"; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | continue; |
| 110 | 110 | } |
| 111 | 111 | if ($this->hasGetMutator($key)) { |
| 112 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 112 | + $method = 'get'.$this->getMutatorMethod($key); |
|
| 113 | 113 | $attributes[$key] = $this->$method($attribute); |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | foreach ($attributes as $key => $attribute) { |
| 128 | 128 | |
| 129 | 129 | if ($this->hasSetMutator($key)) { |
| 130 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 130 | + $method = 'set'.$this->getMutatorMethod($key); |
|
| 131 | 131 | $this->attributes[$key] = $this->$method($attribute); |
| 132 | 132 | } |
| 133 | 133 | else { |
@@ -129,8 +129,7 @@ |
||
| 129 | 129 | if ($this->hasSetMutator($key)) { |
| 130 | 130 | $method = 'set' . $this->getMutatorMethod($key); |
| 131 | 131 | $this->attributes[$key] = $this->$method($attribute); |
| 132 | - } |
|
| 133 | - else { |
|
| 132 | + } else { |
|
| 134 | 133 | $this->attributes[$key] = $attribute; |
| 135 | 134 | } |
| 136 | 135 | } |