|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Trait Container |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource Container.php |
|
6
|
|
|
* @created 13.11.2017 |
|
7
|
|
|
* @package chillerlan\Traits |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2017 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Traits; |
|
14
|
|
|
|
|
15
|
|
|
use ReflectionProperty; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* a generic container with magic getter and setter |
|
19
|
|
|
*/ |
|
20
|
|
|
trait Container{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \chillerlan\Traits\DotEnv|null |
|
24
|
|
|
*/ |
|
25
|
|
|
private $env; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param iterable $properties |
|
29
|
|
|
* @param \chillerlan\Traits\DotEnv|null $env |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(array $properties = null, DotEnv $env = null){ |
|
32
|
|
|
$this->env = $env; |
|
33
|
|
|
|
|
34
|
|
|
if(!empty($properties)){ |
|
35
|
|
|
$this->__fromIterable($properties); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $property |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __get(string $property){ |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
# if(method_exists($this, 'get_'.$property) && $this->__isset($property)){ |
|
49
|
|
|
# return call_user_func([$this, 'get_'.$property]); |
|
50
|
|
|
# } |
|
51
|
|
|
|
|
52
|
|
|
if($this->__isset($property)){ |
|
53
|
|
|
return $this->{$property}; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if(property_exists($this, 'env') && $this->env instanceof DotEnv){ |
|
57
|
|
|
return $this->env->get($property); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $property |
|
65
|
|
|
* @param mixed $value |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __set(string $property, $value){ |
|
70
|
|
|
|
|
71
|
|
|
# if(method_exists($this, 'set_'.$property) && !$this->__isPrivate($property)){ |
|
72
|
|
|
# call_user_func_array([$this, 'set_'.$property], [$value]); |
|
73
|
|
|
# return; |
|
74
|
|
|
# } |
|
75
|
|
|
|
|
76
|
|
|
// avoid overwriting private properties |
|
77
|
|
|
if(property_exists($this, $property) && !$this->__isPrivate($property)){ |
|
78
|
|
|
$this->{$property} = $value; |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if(property_exists($this, 'env') && $this->env instanceof DotEnv){ |
|
83
|
|
|
$this->env->set($property, $value); |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return; // should not see me |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $property |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function __isset(string $property):bool{ |
|
96
|
|
|
return (isset($this->{$property}) && !$this->__isPrivate($property)) |
|
97
|
|
|
|| (property_exists($this, 'env') && $this->env instanceof DotEnv && $this->env->get($property)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $property |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function __isPrivate(string $property):bool{ |
|
106
|
|
|
return (new ReflectionProperty($this, $property))->isPrivate(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $property |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
public function __unset(string $property){ |
|
115
|
|
|
|
|
116
|
|
|
// avoid unsetting private properties |
|
117
|
|
|
if($this->__isset($property)){ |
|
118
|
|
|
unset($this->{$property}); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __toString():string{ |
|
127
|
|
|
return $this->__toJSON(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __toArray():array{ |
|
134
|
|
|
$data = []; |
|
135
|
|
|
|
|
136
|
|
|
foreach($this as $property => $value){ |
|
137
|
|
|
|
|
138
|
|
|
// exclude private properties |
|
139
|
|
|
if($this->__isset($property)){ |
|
140
|
|
|
$data[$property] = $value; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $data; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param iterable $properties |
|
150
|
|
|
* |
|
151
|
|
|
* @return $this |
|
152
|
|
|
*/ |
|
153
|
|
|
public function __fromIterable(array $properties){ |
|
154
|
|
|
|
|
155
|
|
|
foreach($properties as $key => $value){ |
|
156
|
|
|
$this->__set($key, $value); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param bool|null $prettyprint |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function __toJSON(bool $prettyprint = null):string{ |
|
168
|
|
|
return json_encode($this->__toArray(), $prettyprint ? JSON_PRETTY_PRINT : 0); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $json |
|
173
|
|
|
* |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
|
|
public function __fromJSON(string $json){ |
|
177
|
|
|
return $this->__fromIterable(json_decode($json, true)); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|