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
|
|
|
if($this->__isset($property)){ |
48
|
|
|
return $this->{$property}; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
# if(property_exists($this, 'env') && $this->env instanceof DotEnv){ |
52
|
|
|
# return $this->env->get($property); |
53
|
|
|
# } |
54
|
|
|
|
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $property |
60
|
|
|
* @param mixed $value |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function __set(string $property, $value){ |
65
|
|
|
|
66
|
|
|
// avoid overwriting private properties |
67
|
|
|
if(property_exists($this, $property) && !$this->__isPrivate($property)){ |
68
|
|
|
$this->{$property} = $value; |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
# if(property_exists($this, 'env') && $this->env instanceof DotEnv){ |
73
|
|
|
# $this->env->set($property, $value); |
74
|
|
|
# return; |
75
|
|
|
# } |
76
|
|
|
|
77
|
|
|
return; // should not see me |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $property |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function __isset(string $property):bool{ |
86
|
|
|
return (isset($this->{$property}) && !$this->__isPrivate($property)) |
87
|
|
|
# || (property_exists($this, 'env') && $this->env instanceof DotEnv && $this->env->get($property)) |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $property |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
protected function __isPrivate(string $property):bool{ |
97
|
|
|
return (new ReflectionProperty($this, $property))->isPrivate(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $property |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function __unset(string $property){ |
106
|
|
|
|
107
|
|
|
// avoid unsetting private properties |
108
|
|
|
if($this->__isset($property)){ |
109
|
|
|
unset($this->{$property}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function __toString():string{ |
118
|
|
|
return $this->__toJSON(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function __toArray():array{ |
125
|
|
|
$data = []; |
126
|
|
|
|
127
|
|
|
foreach($this as $property => $value){ |
128
|
|
|
|
129
|
|
|
// exclude private properties |
130
|
|
|
if($this->__isset($property)){ |
131
|
|
|
$data[$property] = $value; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $data; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param iterable $properties |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function __fromIterable(array $properties){ |
145
|
|
|
|
146
|
|
|
foreach($properties as $key => $value){ |
147
|
|
|
$this->__set($key, $value); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param bool|null $prettyprint |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function __toJSON(bool $prettyprint = null):string{ |
159
|
|
|
return json_encode($this->__toArray(), $prettyprint ? JSON_PRETTY_PRINT : 0); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $json |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function __fromJSON(string $json){ |
168
|
|
|
return $this->__fromIterable(json_decode($json, true)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.