Completed
Push — master ( ccbea3...19d1f9 )
by smiley
01:51
created

Container::__set()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 2
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
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(iterable $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
		elseif($this->env instanceof DotEnv){
51
			return $this->env->get($property);
52
		}
53
54
		return null;
55
	}
56
57
	/**
58
	 * @param string $property
59
	 * @param mixed  $value
60
	 *
61
	 * @return void
62
	 */
63
	public function __set(string $property, $value){
64
65
		// avoid overwriting private properties
66
		if(property_exists($this, $property) && !$this->__isPrivate($property)){
67
			$this->{$property} = $value;
68
		}
69
		elseif($this->env instanceof DotEnv){
70
			$this->env->set($property, $value);
71
		}
72
73
	}
74
75
	/**
76
	 * @param string $property
77
	 *
78
	 * @return bool
79
	 */
80
	public function __isset(string $property):bool{
81
		return (property_exists($this, $property) && !$this->__isPrivate($property)) || ($this->env instanceof DotEnv && $this->env->get($property));
82
	}
83
84
	/**
85
	 * @param string $property
86
	 *
87
	 * @return bool
88
	 */
89
	protected function __isPrivate(string $property):bool{
90
		return (new ReflectionProperty($this, $property))->isPrivate();
91
	}
92
93
	/**
94
	 * @param string $property
95
	 *
96
	 * @return void
97
	 */
98
	public function __unset(string $property){
99
100
		// avoid unsetting private properties
101
		if($this->__isset($property)){
102
			unset($this->{$property});
103
		}
104
105
	}
106
107
	/**
108
	 * @return string
109
	 */
110
	public function __toString():string{
111
		return $this->__toJSON();
112
	}
113
114
	/**
115
	 * @return array
116
	 */
117
	public function __toArray():array{
118
		$data = [];
119
120
		foreach($this as $property => $value){
121
122
			// exclude private properties
123
			if($this->__isset($property)){
124
				$data[$property] = $value;
125
			}
126
127
		}
128
129
		return $data;
130
	}
131
132
	/**
133
	 * @param iterable $properties
134
	 *
135
	 * @return $this
136
	 */
137
	public function __fromIterable(iterable $properties){
138
139
		foreach($properties as $key => $value){
140
			$this->__set($key, $value);
141
		}
142
143
		return $this;
144
	}
145
146
	/**
147
	 * @return string
148
	 */
149
	public function __toJSON():string{
150
		return json_encode($this->__toArray());
151
	}
152
153
	/**
154
	 * @param string $json
155
	 *
156
	 * @return $this
157
	 */
158
	public function __fromJSON(string $json){
159
		return $this->__fromIterable(json_decode($json, true));
160
	}
161
162
}
163