1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Kernel\DataStructures; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Config |
22
|
|
|
* |
23
|
|
|
* @package O2System\Core\Metadata |
24
|
|
|
*/ |
25
|
|
|
class Config extends SplArrayObject |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Camelcase Offset CONSTANT Flag |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
const CAMELCASE_OFFSET = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Standard Offset CONSTANT Flag |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
const STD_OFFSET = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Config with Camelcase Offset Flag. |
43
|
|
|
* |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $camelcaseOffset; |
47
|
|
|
|
48
|
|
|
// ------------------------------------------------------------------------ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Config::__construct |
52
|
|
|
* |
53
|
|
|
* @param array $config Initial array of config values |
54
|
|
|
* @param bool $flag Flag using camelcase offset |
55
|
|
|
* |
56
|
|
|
* @return Config Returns an SplArrayObject object on success. |
57
|
|
|
*/ |
58
|
|
|
public function __construct(array $config = [], $flag = self::CAMELCASE_OFFSET) |
59
|
|
|
{ |
60
|
|
|
parent::__construct($config); |
61
|
|
|
|
62
|
|
|
$this->camelcaseOffset = $flag; |
63
|
|
|
|
64
|
|
|
if ($this->camelcaseOffset === true) { |
65
|
|
|
if (count($config) > 0) { |
66
|
|
|
foreach ($config as $offset => $value) { |
67
|
|
|
$this->offsetSet($offset, $value); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// ------------------------------------------------------------------------ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Config::offsetSet |
78
|
|
|
* |
79
|
|
|
* Overriding method for SplArrayObject |
80
|
|
|
* |
81
|
|
|
* @param string $offset The index being set. |
82
|
|
|
* @param mixed $value The new value for the <i>index</i>. |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function offsetSet($offset, $value) |
87
|
|
|
{ |
88
|
|
|
if ($this->camelcaseOffset === true) { |
89
|
|
|
if (is_array($value)) { |
90
|
|
|
if (is_string(key($value))) { |
91
|
|
|
$newValue = []; |
92
|
|
|
|
93
|
|
|
foreach ($value as $key => $val) { |
94
|
|
|
$newValue[ camelcase($key) ] = $val; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
parent::offsetSet(camelcase($offset), new self($newValue)); |
98
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
} elseif (is_string($offset)) { |
|
|
|
|
102
|
|
|
parent::offsetSet(camelcase($offset), $value); |
103
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
parent::offsetSet($offset, $value); |
109
|
|
|
} |
110
|
|
|
} |