Completed
Push — 4.0 ( 866ac0...53586d )
by Marco
10:57
created

Configuration::pathGetAbsolute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
/**
4
 * @package     Comodojo Dispatcher
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @author      Marco Castiello <[email protected]>
7
 * @license     GPL-3.0+
8
 *
9
 * LICENSE:
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
26
class Configuration {
27
28
    protected $attributes = array();
29
30
    public function __construct( $configuration = array() ) {
31
32
        $this->attributes = array_merge($this->attributes, $configuration);
33
34
    }
35
36
    final public function get($property = null) {
37
38
        if ( is_null($property) ) {
39
            
40
            return $this->attributes;
41
            
42
        } else if (array_key_exists($property, $this->attributes)) {
43
44
            $value = $this->attributes[$property];
45
46
            if ( is_scalar($value) && preg_match_all('/%(.+?)%/', $value, $matches) ) {
47
48
                $substitutions = array();
49
50
                foreach ( $matches as $match ) {
51
52
                    $backreference = $match[1];
53
54
                    if ( $backreference != $property && !isset($substitutions['/%'.$backreference.'%/']) ) {
55
56
                        $substitutions['/%'.$backreference.'%/'] = $this->$backreference;
57
58
                    }
59
60
                }
61
62
                $value = preg_replace(array_keys($substitutions), array_values($substitutions), $value);
63
64
            }
65
66
            return $value;
67
68
        } else {
69
            
70
            return null;
71
            
72
        }
73
74
    }
75
76
    final public function set($property, $value) {
77
78
        $this->attributes[$property] = $value;
79
80
        return $this;
81
82
    }
83
84
    final public function isDefined($property) {
85
86
        return isset($this->attributes[$property]);
87
88
    }
89
90
    final public function delete($property = null) {
91
92
        if ( is_null($property) ) {
93
94
            $this->attributes = array();
95
96
            return true;
97
98
        } else if ( $this->isDefined($property) ) {
99
100
            unset($this->attributes[$property]);
101
102
            return true;
103
104
        } else {
105
106
            return false;
107
108
        }
109
110
    }
111
112
    final public function merge($properties) {
113
114
        return array_replace($this->attributes, $properties);
115
116
    }
117
118
}
119