Passed
Push — master ( c6902f...770aa3 )
by smiley
02:51
created

Container::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 7
rs 9.4285
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 array                          $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
36
			foreach($properties as $key => $value){
37
				$this->__set($key, $value);
38
			}
39
40
		}
41
42
	}
43
44
	/**
45
	 * @param string $property
46
	 *
47
	 * @return mixed
48
	 */
49
	public function __get(string $property){
50
51
		if($this->__isset($property)){
52
			return $this->{$property};
53
		}
54
		elseif($this->env instanceof DotEnv){
55
			return $this->env->get($property);
56
		}
57
58
		return null;
59
	}
60
61
	/**
62
	 * @param string $property
63
	 * @param mixed  $value
64
	 *
65
	 * @return void
66
	 */
67
	public function __set(string $property, $value){
68
69
		// avoid overwriting private properties
70
		if(!property_exists($this, $property) || !$this->__isPrivate($property)){
71
			$this->{$property} = $value;
72
		}
73
		elseif($this->env instanceof DotEnv){
74
			$this->env->set($property, $value);
75
		}
76
77
	}
78
79
	/**
80
	 * @param string $property
81
	 *
82
	 * @return bool
83
	 */
84
	public function __isset(string $property):bool{
85
		return (property_exists($this, $property) && !$this->__isPrivate($property)) || ($this->env instanceof DotEnv && $this->env->get($property));
86
	}
87
88
	/**
89
	 * @param string $property
90
	 *
91
	 * @return bool
92
	 */
93
	protected function __isPrivate(string $property):bool{
94
		return (new ReflectionProperty($this, $property))->isPrivate();
95
	}
96
97
	/**
98
	 * @param string $property
99
	 *
100
	 * @return void
101
	 */
102
	public function __unset(string $property){
103
104
		// avoid unsetting private properties
105
		if($this->__isPrivate($property)){
106
			unset($this->{$property});
107
		}
108
109
	}
110
111
	/**
112
	 * @return string
113
	 */
114
	public function __toString():string{
115
		return json_encode($this->__toArray());
116
	}
117
118
	/**
119
	 * @return array
120
	 */
121
	public function __toArray():array{
122
		$data = [];
123
124
		foreach($this as $property => $value){
125
126
			// exclude private properties
127
			if($this->__isset($property)){
128
				$data[$property] = $value;
129
			}
130
131
		}
132
133
		return $data;
134
	}
135
136
}
137