1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Configuration |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | namespace Slick\Configuration\Driver; |
||
11 | |||
12 | use Slick\Configuration\ConfigurationInterface; |
||
13 | use Slick\Configuration\Exception\FileNotFoundException; |
||
14 | |||
15 | /** |
||
16 | * Common Driver Methods Trait |
||
17 | * |
||
18 | * @package Slick\Configuration\Driver |
||
19 | */ |
||
20 | trait CommonDriverMethods |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var array<string, mixed> |
||
25 | */ |
||
26 | protected array|int $data = []; |
||
27 | |||
28 | /** |
||
29 | * Checks if provided file exists |
||
30 | * |
||
31 | * @param string $file |
||
32 | * |
||
33 | * @throws FileNotFoundException if provided file does not exist |
||
34 | */ |
||
35 | protected function checkFile(string $file): void |
||
36 | { |
||
37 | if (!is_file($file)) { |
||
38 | throw new FileNotFoundException( |
||
39 | "Configuration file $file could not be found." |
||
40 | ); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Returns the value store with provided key or the default value. |
||
46 | * |
||
47 | * @param string $key The key used to store the value in configuration |
||
48 | * @param mixed|null $default The default value if not found |
||
49 | * @return mixed The stored value or the default value if key |
||
50 | * was not found. |
||
51 | */ |
||
52 | public function get(string $key, mixed $default = null): mixed |
||
53 | { |
||
54 | $data = is_array($this->data) ? $this->data : []; |
||
55 | return static::getValue($key, $default, $data); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Set/Store the provided value with a given key. |
||
60 | * |
||
61 | * @param string $key The key used to store the value in configuration. |
||
62 | * @param mixed $value The value to store under the provided key. |
||
63 | * |
||
64 | * @return ConfigurationInterface Self instance for method call chains. |
||
65 | */ |
||
66 | public function set(string $key, mixed $value): ConfigurationInterface |
||
67 | { |
||
68 | static::setValue($key, $value, $this->data); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
69 | return $this; |
||
0 ignored issues
–
show
|
|||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Recursive method to parse dot notation keys and retrieve the value |
||
74 | * |
||
75 | * @param string $key The key/index to search |
||
76 | * @param mixed $default The value if key doesn't exist |
||
77 | * @param array<string, mixed> $data The data to search |
||
78 | * |
||
79 | * @return mixed The stored value or the default value if key |
||
80 | * or index don't exist |
||
81 | */ |
||
82 | public static function getValue(string $key, mixed $default, array $data): mixed |
||
83 | { |
||
84 | $parts = explode('.', $key); |
||
85 | $first = array_shift($parts); |
||
86 | if (isset($data[$first])) { |
||
87 | if (count($parts) > 0) { |
||
88 | $newKey = implode('.', $parts); |
||
89 | return static::getValue($newKey, $default, $data[$first]); |
||
90 | } |
||
91 | $default = $data[$first]; |
||
92 | } |
||
93 | return $default; |
||
94 | } |
||
95 | /** |
||
96 | * Recursive method to parse dot notation keys and set the value |
||
97 | * |
||
98 | * @param string $key The key used to store the value in configuration. |
||
99 | * @param mixed $value The value to store under the provided key. |
||
100 | * @param array<string, mixed> $data The data to search |
||
101 | */ |
||
102 | public static function setValue(string $key, mixed $value, array &$data): void |
||
103 | { |
||
104 | $parts = explode('.', $key); |
||
105 | $first = array_shift($parts); |
||
106 | if (count($parts) > 0) { |
||
107 | $newKey = implode('.', $parts); |
||
108 | $data[$first] = array_key_exists($first, $data) ? [$first => $data[$first]] : []; |
||
109 | static::setValue($newKey, $value, $data[$first]); |
||
110 | return; |
||
111 | } |
||
112 | $data[$first] = $value; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | */ |
||
118 | public function asArray(): array |
||
119 | { |
||
120 | return is_array($this->data) ? $this->data : []; |
||
121 | } |
||
122 | } |
||
123 |