1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Analogue\ORM; |
4
|
|
|
|
5
|
|
|
use Analogue\ORM\System\Proxies\EntityProxy; |
6
|
|
|
|
7
|
|
|
class Entity extends ValueObject |
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 = []; |
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); |
27
|
|
|
|
28
|
|
|
$attribute = null; |
29
|
|
|
|
30
|
|
|
if (isset($this->attributes[$key])) { |
31
|
|
|
$attribute = $this->attributes[$key]; |
32
|
|
|
} |
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
|
|
|
} |
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); |
56
|
|
|
|
57
|
|
|
$this->$method($value); |
58
|
|
|
} else { |
59
|
|
|
$this->attributes[$key] = $value; |
60
|
|
|
} |
61
|
|
|
} |
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
|
|
|
} |
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
|
|
|
} |
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
|
|
|
} |
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); |
105
|
|
|
|
106
|
|
View Code Duplication |
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
|
|
|
|
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
|
|
View Code Duplication |
foreach ($attributes as $key => $attribute) { |
|
|
|
|
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
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.