|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
MIT License |
|
4
|
|
|
Copyright (c) 2010 - 2018 Peter Petermann |
|
5
|
|
|
|
|
6
|
|
|
Permission is hereby granted, free of charge, to any person |
|
7
|
|
|
obtaining a copy of this software and associated documentation |
|
8
|
|
|
files (the "Software"), to deal in the Software without |
|
9
|
|
|
restriction, including without limitation the rights to use, |
|
10
|
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
copies of the Software, and to permit persons to whom the |
|
12
|
|
|
Software is furnished to do so, subject to the following |
|
13
|
|
|
conditions: |
|
14
|
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be |
|
16
|
|
|
included in all copies or substantial portions of the Software. |
|
17
|
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
19
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
20
|
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
21
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
22
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
23
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
24
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
25
|
|
|
OTHER DEALINGS IN THE SOFTWARE. |
|
26
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace King23\Settings; |
|
30
|
|
|
|
|
31
|
|
|
class JsonSettings implements SettingsInterface |
|
32
|
|
|
{ |
|
33
|
|
|
private $data = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $filename |
|
37
|
|
|
* @return JsonSettings |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function fromFilename(string $filename): JsonSettings |
|
40
|
|
|
{ |
|
41
|
|
|
return static::fromJsonString(file_get_contents($filename)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $string |
|
46
|
|
|
* @return JsonSettings |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function fromJsonString(string $string): JsonSettings |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
// @todo remove once minimum is php 7.3 |
|
52
|
|
|
if (!defined("JSON_THROW_ON_ERROR")) { |
|
53
|
|
|
define("JSON_THROW_ON_ERROR", 4194304); |
|
54
|
|
|
} |
|
55
|
|
|
$settings = new JsonSettings(); |
|
56
|
|
|
$settings->data = (array) json_decode($string, true, 512, JSON_THROW_ON_ERROR); |
|
57
|
|
|
return $settings; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $key |
|
62
|
|
|
* @param mixed $default |
|
63
|
|
|
* @return array|int|mixed|null|string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get($key, $default = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$keys = explode(".", $key); |
|
68
|
|
|
$result = $this->getFromSubKeys($keys, $this->data); |
|
69
|
|
|
if (is_null($result)) { |
|
70
|
|
|
return $default; |
|
71
|
|
|
} |
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $keys |
|
77
|
|
|
* @param array $data |
|
78
|
|
|
* @return null|string|int|array |
|
79
|
|
|
*/ |
|
80
|
|
|
private function getFromSubKeys(array $keys, array $data) |
|
81
|
|
|
{ |
|
82
|
|
|
$key = array_shift($keys); |
|
83
|
|
|
|
|
84
|
|
|
// key doesn't exist, so we can return null |
|
85
|
|
|
if (!array_key_exists($key, $data)) { |
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
// key is the last key, so we can return the contents |
|
89
|
|
|
if (count($keys) == 0) { |
|
90
|
|
|
return $data[$key]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// more subkeys, so lets dive in |
|
94
|
|
|
return $this->getFromSubKeys($keys, $data[$key]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|