1 | <?php |
||
10 | class IniObject |
||
11 | { |
||
12 | private $filename; |
||
13 | |||
14 | protected $config; |
||
15 | |||
16 | private $sections; |
||
17 | |||
18 | /** |
||
19 | * IniObject constructor. |
||
20 | * |
||
21 | * @param string|null $filename |
||
22 | * @param array $data |
||
23 | * @param array $config |
||
24 | */ |
||
25 | 12 | public function __construct(?string $filename = null, array $data = [], array $config = []) |
|
26 | { |
||
27 | 12 | if (! count($config)) { |
|
28 | 12 | $this->initDefaultConfig(); |
|
29 | } |
||
30 | 12 | if (! count($data) && file_exists($filename)) { |
|
31 | 12 | $data = parse_ini_file( |
|
32 | 12 | $filename, |
|
33 | 12 | true, |
|
34 | 12 | $this->config['strict'] ? INI_SCANNER_TYPED : INI_SCANNER_NORMAL |
|
35 | ); |
||
36 | } |
||
37 | 12 | if (! empty($data)) { |
|
38 | 12 | $this->sectionLoad($data); |
|
39 | } |
||
40 | 12 | $this->setFilename($filename); |
|
41 | 12 | } |
|
42 | |||
43 | /** |
||
44 | * @param array $data |
||
45 | * @throws InvalidArgumentException |
||
46 | */ |
||
47 | 12 | private function sectionLoad(array $data): void |
|
55 | |||
56 | /** |
||
57 | * @param string $name |
||
58 | * @return Section |
||
59 | */ |
||
60 | 7 | public function getSection(string $name): Section |
|
68 | |||
69 | /** |
||
70 | * @param string $name |
||
71 | */ |
||
72 | 1 | public function removeSection(string $name): void |
|
79 | |||
80 | 2 | public function addSection(string $name, array $section): void |
|
87 | |||
88 | 1 | public function updateSection(string $name, array $section): void |
|
95 | |||
96 | /** |
||
97 | * default config |
||
98 | */ |
||
99 | 12 | private function initDefaultConfig(): void |
|
106 | |||
107 | /** |
||
108 | * @param string $filename |
||
109 | */ |
||
110 | 12 | private function setFilename(?string $filename): void |
|
114 | |||
115 | 1 | public function __toString() |
|
125 | |||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | 1 | public function toArray(): array |
|
139 | |||
140 | /** |
||
141 | * @param string|null $filename |
||
142 | * @return bool |
||
143 | */ |
||
144 | 1 | public function save(string $filename = null): bool |
|
155 | } |
||
156 |