Completed
Push — master ( 5d8fdd...46e3e5 )
by Marco
01:37
created

ConfigurationParametersTrait::setFromParts()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 10
cts 11
cp 0.9091
rs 8.439
cc 5
eloc 11
nc 5
nop 2
crap 5.0187
1
<?php namespace Comodojo\Foundation\Base;
2
3
use \InvalidArgumentException;
4
5
/**
6
 * @package     Comodojo Foundation
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
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 6
    public function get($parameter=null) {
38
39 6
        if ( is_null($parameter) ) return $this->parameters;
40
41 5
        return $this->getFromParts(self::splitParts($parameter));
42
43
    }
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) {
55
56 2
        $parts = self::splitParts($parameter);
57
58 2
        if ( empty($parts) ) throw new InvalidArgumentException("Invalid parameter $parameter");
59
60 2
        $this->setFromParts($parts, $value);
61
62 2
        return $this;
63
64
    }
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) {
76
77 3
        return is_null($this->getFromParts(self::splitParts($parameter))) ? false : true;
78
79
    }
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) {
91
92 3
        if ( is_null($parameter) ) {
93
94 1
            $this->parameters = [];
95 1
            return true;
96
97
        }
98
99 2
        $parts = self::splitParts($parameter);
100
101 2
        if ( empty($parts) ) throw new InvalidArgumentException("Invalid parameter $parameter");
102
103 2
        return $this->deleteFromParts($parts);
104
105
    }
106
107 7
    protected function getFromParts(array $parts) {
108
109 7
        if ( empty($parts) ) return null;
110
111 7
        $reference = &$this->parameters;
112
113 7
        foreach ($parts as $part) {
114
115 7
            if ( !isset($reference[$part]) ) {
116 4
                return null;
117
            }
118
119 6
            $reference = &$reference[$part];
120
121
        }
122
123 6
        $data = $reference;
124
125 6
        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
            }
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
        }
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
            } else {
167 1
                $reference = &$reference[$parts[$i]];
168
            }
169
        }
170
171 2
        return true;
172
173
    }
174
175 7
    protected static function splitParts($parameter) {
176
177 7
        return preg_split('/(\s)?\.(\s)?/', $parameter, null, PREG_SPLIT_NO_EMPTY);
178
179
    }
180
181
}
182