1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BMorais\Database; |
4
|
|
|
|
5
|
|
|
use ReflectionObject; |
6
|
|
|
|
7
|
|
|
abstract class ModelAbstract |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param array|null $params |
11
|
|
|
*/ |
12
|
|
|
public function __construct(array $params = null){ |
13
|
|
|
if (!empty($params)) |
14
|
|
|
$this->fromMapToModel($params); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param array $params |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function fromMapToModel(array $params): void |
22
|
|
|
{ |
23
|
|
|
foreach ($params as $key => $item) |
24
|
|
|
{ |
25
|
|
|
$this->{$key} = $item; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $json |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function fromJsonToModel(string $json): void |
34
|
|
|
{ |
35
|
|
|
$params = json_decode($json, true); |
36
|
|
|
$this->fromMapToModel($params); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array|null |
41
|
|
|
*/ |
42
|
|
|
public function toMap(): ?array |
43
|
|
|
{ |
44
|
|
|
$data = $this; |
45
|
|
|
if (is_array($data) || is_object($data)) |
46
|
|
|
{ |
47
|
|
|
$result = []; |
48
|
|
|
foreach ($data as $key => $value) |
49
|
|
|
{ |
50
|
|
|
$result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function toJson():string |
61
|
|
|
{ |
62
|
|
|
return json_encode($this->toMap(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function toString():string |
69
|
|
|
{ |
70
|
|
|
$data = (object) $this->toMap(); |
71
|
|
|
$re_2 = new ReflectionObject($data); |
72
|
|
|
$classname = get_class($this); |
73
|
|
|
if ($pos = strrpos($classname, '\\')) $classname = substr($classname, $pos + 1); |
74
|
|
|
return $classname .' {' . implode(', ', array_map( |
75
|
|
|
function($p_0) use ($data) |
76
|
|
|
{ |
77
|
|
|
$p_0->setAccessible(true); |
78
|
|
|
return $p_0->getName() .': '. $p_0->getValue($data); |
79
|
|
|
}, $re_2->getProperties())) .'}'; |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param |
85
|
|
|
* @return string|null |
86
|
|
|
*/ |
|
|
|
|
87
|
|
|
public function __get($attribute) { |
88
|
|
|
return $this->{$attribute} ; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
public function __set($attribute, $value): void { |
93
|
|
|
$this->{$attribute} = $value ; |
94
|
|
|
} |
95
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.