1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FlexPHP\Entities; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Entity |
7
|
|
|
* |
8
|
|
|
* @method $this|int id($id = null) |
9
|
|
|
*/ |
10
|
|
|
abstract class Entity implements EntityInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Save attribute names hydrate on instance |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $attributesHydrated = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Entity constructor. |
21
|
|
|
* @param array $attributes |
22
|
|
|
*/ |
23
|
|
|
public function __construct(array $attributes = []) |
24
|
|
|
{ |
25
|
|
|
$this->hydrate($attributes); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function toArray(): array |
29
|
|
|
{ |
30
|
|
|
$toArray = []; |
31
|
|
|
|
32
|
|
|
foreach ($this->attributesHydrated as $index => $attribute) { |
33
|
|
|
if (\property_exists($this, $attribute)) { |
34
|
|
|
$toArray[$attribute] = $this->{$attribute}; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $toArray; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $attributes |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
private function hydrate(array $attributes): self |
46
|
|
|
{ |
47
|
|
|
foreach ($attributes as $attribute => $value) { |
48
|
|
|
$this->{$attribute} = $value; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $name |
56
|
|
|
* @param mixed $value |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function __set($name, $value) |
60
|
|
|
{ |
61
|
|
|
$this->{$name} = $value; |
62
|
|
|
|
63
|
|
|
$this->attributesHydrated[] = $name; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $name |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function __get($name) |
73
|
|
|
{ |
74
|
|
|
return $this->{$name} ?? null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param array $arguments |
80
|
|
|
* @return $this|mixed |
81
|
|
|
*/ |
82
|
|
|
public function __call(string $name, array $arguments) |
83
|
|
|
{ |
84
|
|
|
$attribute = $this->snakeCase($name); |
85
|
|
|
|
86
|
|
|
if (\count($arguments) > 0) { |
87
|
|
|
return $this->__set($attribute, ...$arguments); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->__get($attribute); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Entity to json string |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function __toString() |
99
|
|
|
{ |
100
|
|
|
return (string)\json_encode($this->toArray(), JSON_ERROR_NONE); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Change to snake_case attribute names |
105
|
|
|
* |
106
|
|
|
* @param string $attribute |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
private function snakeCase(string $attribute): string |
110
|
|
|
{ |
111
|
|
|
return \mb_strtolower(\preg_replace('~(?<=\\w)([A-Z])~', '_$1', $attribute) ?? $attribute); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|