1 | <?php |
||
2 | |||
3 | namespace BMorais\Database; |
||
4 | |||
5 | /** |
||
6 | * CLASS CRUD |
||
7 | * Basic class to make connection between the database and the application |
||
8 | * |
||
9 | * @author Bruno Morais <[email protected]> |
||
10 | * @copyright MIT, bmorais.com |
||
11 | * @package bmorais\database |
||
12 | * @subpackage class |
||
13 | * @access private |
||
14 | */ |
||
15 | |||
16 | use ReflectionObject; |
||
17 | |||
18 | #[\AllowDynamicProperties] |
||
19 | abstract class ModelAbstract |
||
20 | { |
||
21 | /** |
||
22 | * @param array|null $params |
||
23 | */ |
||
24 | public function __construct(?array $params = null) |
||
25 | { |
||
26 | if (!empty($params)) { |
||
27 | $this->fromMapToModel($params); |
||
28 | } |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param $name |
||
33 | */ |
||
34 | public function __get($name) |
||
35 | { |
||
36 | return property_exists($this, $name) ? $this->{$name} : null; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param $key |
||
41 | * @param $value |
||
42 | * @return void |
||
43 | */ |
||
44 | public function __set($key, $value): void |
||
45 | { |
||
46 | if (property_exists($this, $key)) { |
||
47 | $this->{$key} = $value; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param $key |
||
53 | * @return bool |
||
54 | */ |
||
55 | function __isset($key) { |
||
0 ignored issues
–
show
|
|||
56 | |||
57 | return property_exists($this, $key) && isset($this->{$key}); |
||
58 | |||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param array $params |
||
63 | * @return void |
||
64 | */ |
||
65 | public function fromMapToModel(array $params) |
||
66 | { |
||
67 | $reflection = new \ReflectionClass($this); |
||
68 | |||
69 | foreach ($params as $key => $item) { |
||
70 | if ($reflection->hasProperty($key)) { |
||
71 | $property = $reflection->getProperty($key); |
||
72 | $property->setAccessible(true); |
||
73 | $property->setValue($this, $item); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | return $this; |
||
0 ignored issues
–
show
|
|||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $json |
||
82 | * @return void |
||
83 | */ |
||
84 | public function fromJsonToModel(string $json) |
||
85 | { |
||
86 | $params = json_decode($json, true, flags: JSON_THROW_ON_ERROR); |
||
87 | return $this->fromMapToModel($params); |
||
0 ignored issues
–
show
Are you sure the usage of
$this->fromMapToModel($params) targeting BMorais\Database\ModelAbstract::fromMapToModel() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
88 | |||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param $objArray |
||
93 | * @return array|null |
||
94 | */ |
||
95 | public function toMap($objArray = null): ?array |
||
96 | { |
||
97 | $data = $objArray ?? $this; |
||
98 | if (is_array($data) || is_object($data)) { |
||
99 | $result = []; |
||
100 | foreach ($data as $key => $value) { |
||
101 | $result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value; |
||
102 | } |
||
103 | |||
104 | return $result; |
||
105 | } |
||
106 | |||
107 | return null; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return \stdClass |
||
112 | */ |
||
113 | public function toObject(): \stdClass{ |
||
114 | $reflection = new \ReflectionClass($this); |
||
115 | $objeto = new \stdClass; |
||
116 | |||
117 | foreach ($reflection->getProperties() as $prop) { |
||
118 | $prop->setAccessible(true); |
||
119 | |||
120 | $method = 'get' . $prop->getName(); |
||
121 | |||
122 | if (method_exists($this, $method)) { |
||
123 | $objeto->{$prop->getName()} = call_user_func([$this, $method]); |
||
124 | } else { |
||
125 | $objeto->{$prop->getName()} = $prop->getValue($this); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | return $objeto; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return array |
||
134 | */ |
||
135 | public function toArray(): array |
||
136 | { |
||
137 | $array = []; |
||
138 | foreach ((new \ReflectionClass($this))->getProperties() as $prop) { |
||
139 | $prop->setAccessible(true); |
||
140 | $method = 'get' . ucfirst($prop->getName()); |
||
141 | |||
142 | $array[$prop->getName()] = method_exists($this, $method) |
||
143 | ? $this->{$method}() |
||
144 | : $prop->getValue($this); |
||
145 | } |
||
146 | return $array; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | public function toJson(): string |
||
153 | { |
||
154 | return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | */ |
||
160 | public function toString(): string |
||
161 | { |
||
162 | $classname = (new \ReflectionClass($this))->getShortName(); |
||
163 | $properties = array_map( |
||
164 | fn($prop) => "{$prop->getName()}: '" . ($prop->getValue($this) ?? '') . "'", |
||
165 | (new \ReflectionClass($this))->getProperties() |
||
166 | ); |
||
167 | return "$classname {" . implode(', ', $properties) . '}'; |
||
168 | } |
||
169 | |||
170 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.