|
1
|
|
|
<?php |
|
2
|
|
|
namespace Puppy\Config; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class SimpleConfig |
|
6
|
|
|
* @package Puppy\Config |
|
7
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
class ArrayConfig extends \ArrayObject |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param array|\ArrayObject $data |
|
14
|
|
|
*/ |
|
15
|
23 |
|
public function __construct($data) |
|
16
|
|
|
{ |
|
17
|
23 |
|
if(!($data instanceof self)){ |
|
18
|
23 |
|
$data = $this->buildConfig($data); |
|
19
|
23 |
|
} |
|
20
|
23 |
|
parent::__construct($data); |
|
21
|
23 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* restricts the config to a specific visibility. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $restriction |
|
27
|
|
|
* @param string $separator |
|
28
|
|
|
* @return RestrictedConfig |
|
29
|
|
|
*/ |
|
30
|
2 |
|
public function restrict($restriction, $separator = '.') |
|
31
|
|
|
{ |
|
32
|
2 |
|
return new RestrictedConfig($restriction, $separator, $this); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param mixed $key |
|
37
|
|
|
* @param mixed $value |
|
38
|
|
|
*/ |
|
39
|
6 |
|
public function offsetSet($key, $value) |
|
40
|
|
|
{ |
|
41
|
6 |
|
parent::offsetSet($key, $value); |
|
42
|
6 |
|
$this->exchangeArray($this->buildConfig(parent::getArrayCopy())); |
|
|
|
|
|
|
43
|
6 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array|\ArrayObject $data |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
23 |
|
private function buildConfig($data) |
|
50
|
|
|
{ |
|
51
|
23 |
|
return $this->replace($data, $this->formatKeys($data)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array|\ArrayObject $data |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
23 |
|
private function formatKeys($data) |
|
59
|
|
|
{ |
|
60
|
23 |
|
$result = []; |
|
61
|
23 |
|
foreach ($data as $key => $value) { |
|
62
|
21 |
|
if (is_string($value)) { |
|
63
|
21 |
|
$result['%' . $key . '%'] = $value; |
|
64
|
21 |
|
} |
|
65
|
23 |
|
} |
|
66
|
23 |
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $data |
|
71
|
|
|
* @param array $replacements |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
23 |
|
private function replace($data, array $replacements) |
|
75
|
|
|
{ |
|
76
|
23 |
|
foreach ($data as $key => $var) { |
|
77
|
21 |
|
if (is_string($var)) { |
|
78
|
21 |
|
$data[$key] = strtr($var, $replacements); |
|
79
|
21 |
|
}elseif(is_array($var)){ |
|
80
|
2 |
|
$data[$key] = $this->replace($var, $replacements); |
|
81
|
2 |
|
} |
|
82
|
23 |
|
} |
|
83
|
23 |
|
return $data; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.