1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bludata\Common\Traits; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
trait AttributesTrait |
9
|
|
|
{ |
10
|
|
|
public function getAttributes() |
11
|
|
|
{ |
12
|
|
|
$attributes = array_keys( |
13
|
|
|
get_class_vars( |
14
|
|
|
get_class($this) |
15
|
|
|
) |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
return array_combine( |
19
|
|
|
$attributes, |
20
|
|
|
array_map( |
21
|
|
|
function ($attr) { |
22
|
|
|
$getMethod = $this->getMethod($attr); |
23
|
|
|
if (method_exists($this, $getMethod)) { |
24
|
|
|
return $this->$getMethod(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->$attr; |
28
|
|
|
}, |
29
|
|
|
$attributes |
30
|
|
|
) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getMethod($attr) |
35
|
|
|
{ |
36
|
|
|
return sprintf('get%s', ucfirst($attr)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setMethod($attr) |
40
|
|
|
{ |
41
|
|
|
return sprintf('set%s', ucfirst($attr)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function get($attr) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$attributes = $this->getAttributes(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$getMethod = $this->getMethod($attr); |
49
|
|
|
if (method_exists($this, $getMethod)) { |
50
|
|
|
return $this->$getMethod(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new InvalidArgumentException( |
54
|
|
|
sprintf( |
55
|
|
|
'The class "%s" don\'t have a method "%s"', |
56
|
|
|
get_class($this), |
57
|
|
|
$getMethod |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function set($attr, $value) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$setMethod = $this->setMethod($attr); |
65
|
|
|
|
66
|
|
|
if (method_exists($this, $setMethod)) { |
67
|
|
|
return $this->$setMethod($value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new InvalidArgumentException( |
71
|
|
|
sprintf( |
72
|
|
|
'The class "%s" don\'t have a method "%s"', |
73
|
|
|
get_class($this), |
74
|
|
|
$setMethod |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function toArray() |
80
|
|
|
{ |
81
|
|
|
return array_map(function ($attr) { |
82
|
|
|
if (method_exists($attr, 'toArray')) { |
83
|
|
|
return $attr->toArray(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($attr instanceof ArrayAccess) { |
87
|
|
|
$newParam = []; |
88
|
|
|
foreach ($attr as $p) { |
89
|
|
|
if (method_exists($p, 'toArray')) { |
90
|
|
|
$newParam[] = $p->toArray(); |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$newParam[] = $p; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $newParam; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $attr; |
101
|
|
|
}, $this->getAttributes()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function toJson() |
105
|
|
|
{ |
106
|
|
|
return json_encode($this->toArray()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function toString() |
110
|
|
|
{ |
111
|
|
|
return print_r($this, true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function __toString() |
115
|
|
|
{ |
116
|
|
|
return $this->toString(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function __get($attr) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
return $this->get($attr); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function __set($attr, $value) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
return $this->set($attr, $value); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function __isset($attr) |
130
|
|
|
{ |
131
|
|
|
return isset($this->$attr); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function __unset($attr) |
135
|
|
|
{ |
136
|
|
|
unset($this->$attr); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.