1 | <?php namespace Comodojo\Foundation\Base; |
||
21 | trait ConfigurationParametersTrait { |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $parameters = []; |
||
27 | |||
28 | /** |
||
29 | * Get parameter (property) from stack |
||
30 | * |
||
31 | * This method supports nesting of properties using dot notation |
||
32 | * |
||
33 | * @example Configuration::get("authentication.ldap.server") |
||
34 | * @param string $parameter |
||
35 | * @return mixed|null |
||
36 | */ |
||
37 | 9 | public function get($parameter=null) { |
|
44 | |||
45 | /** |
||
46 | * Set a parameter (property) |
||
47 | * |
||
48 | * This method supports nesting of properties using dot notation |
||
49 | * |
||
50 | * @example Configuration::set("authentication.ldap.server", "192.168.1.1") |
||
51 | * @param string $parameter |
||
52 | * @return self |
||
53 | */ |
||
54 | 2 | public function set($parameter, $value) { |
|
65 | |||
66 | /** |
||
67 | * Check if parameter (property) is defined in current stack |
||
68 | * |
||
69 | * This method supports nesting of properties using dot notation |
||
70 | * |
||
71 | * @example Configuration::has("authentication.ldap.server") |
||
72 | * @param string $parameter |
||
73 | * @return bool |
||
74 | */ |
||
75 | 3 | public function has($parameter) { |
|
80 | |||
81 | /** |
||
82 | * Remove (delete) parameter (property) from stack |
||
83 | * |
||
84 | * This method supports nesting of properties using dot notation |
||
85 | * |
||
86 | * @example Configuration::delete("authentication.ldap.server") |
||
87 | * @param string $parameter |
||
88 | * @return bool |
||
89 | */ |
||
90 | 3 | public function delete($parameter = null) { |
|
106 | |||
107 | 10 | protected function getFromParts(array $parts) { |
|
108 | |||
109 | 10 | if ( empty($parts) ) return null; |
|
110 | |||
111 | 10 | $reference = &$this->parameters; |
|
112 | |||
113 | 10 | foreach ($parts as $part) { |
|
114 | |||
115 | 10 | if ( !isset($reference[$part]) ) { |
|
116 | 5 | return null; |
|
117 | } |
||
118 | |||
119 | 9 | $reference = &$reference[$part]; |
|
120 | |||
121 | 9 | } |
|
122 | |||
123 | 9 | $data = $reference; |
|
124 | |||
125 | 9 | return $data; |
|
126 | |||
127 | } |
||
128 | |||
129 | 2 | protected function setFromParts(array $parts, $value) { |
|
130 | |||
131 | 2 | $reference = &$this->parameters; |
|
132 | |||
133 | 2 | $plength = count($parts); |
|
134 | |||
135 | 2 | for ($i=0; $i < $plength; $i++) { |
|
136 | |||
137 | 2 | if ( !isset($reference[$parts[$i]]) ) { |
|
138 | 2 | $reference[$parts[$i]] = []; |
|
139 | 2 | } |
|
140 | |||
141 | 2 | if ( ($i < $plength-1) && !is_array($reference[$parts[$i]]) ) { |
|
142 | $reference[$parts[$i]] = [$reference[$parts[$i]]]; |
||
143 | } |
||
144 | |||
145 | 2 | $reference = &$reference[$parts[$i]]; |
|
146 | |||
147 | 2 | } |
|
148 | |||
149 | 2 | $reference = $value; |
|
150 | |||
151 | 2 | return true; |
|
152 | |||
153 | } |
||
154 | |||
155 | 2 | protected function deleteFromParts(array $parts) { |
|
156 | |||
157 | 2 | $reference = &$this->parameters; |
|
158 | 2 | $l = count($parts); |
|
159 | |||
160 | 2 | for ($i=0; $i < $l; $i++) { |
|
161 | 2 | if ( !isset($reference[$parts[$i]]) ) { |
|
162 | return false; |
||
163 | } |
||
164 | 2 | if ($i == $l-1) { |
|
165 | 2 | unset($reference[$parts[$i]]); |
|
166 | 2 | } else { |
|
167 | 1 | $reference = &$reference[$parts[$i]]; |
|
168 | } |
||
169 | 2 | } |
|
170 | |||
171 | 2 | return true; |
|
172 | |||
173 | } |
||
174 | |||
175 | 10 | protected static function splitParts($parameter) { |
|
180 | |||
181 | } |
||
182 |