Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Config.php

raw symbols SCIP
1 <?php
2 /*
3 * File: Config.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 10.04.24 15:42
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP;
14
15 use Webklex\PHPIMAP\Decoder\DecoderInterface;
16 use Webklex\PHPIMAP\Exceptions\DecoderNotFoundException;
17
18 /**
19 * Class Config
20 *
21 * @package Webklex\PHPIMAP
22 */
23 class Config {
24
25 /**
26 * Configuration array
27 * @var array $config
28 */
29 protected array $config = [];
30
31 /**
32 * Config constructor.
33 * @param array $config
34 */
35 public function __construct(array $config = []) {
36 $this->config = $config;
37 }
38
39 /**
40 * Get a dotted config parameter
41 * @param string $key
42 * @param null $default
43 *
44 * @return mixed|null
45 */
46 public function get(string $key, $default = null): mixed {
47 $parts = explode('.', $key);
48 $value = null;
49 foreach ($parts as $part) {
50 if ($value === null) {
51 if (isset($this->config[$part])) {
52 $value = $this->config[$part];
53 } else {
54 break;
55 }
56 } else {
57 if (isset($value[$part])) {
58 $value = $value[$part];
59 } else {
60 break;
61 }
62 }
63 }
64
65 return $value === null ? $default : $value;
66 }
67
68 /**
69 * Set a dotted config parameter
70 * @param string $key
71 * @param string|array|mixed$value
72 *
73 * @return void
74 */
75 public function set(string $key, mixed $value): void {
76 $parts = explode('.', $key);
77 $config = &$this->config;
78
79 foreach ($parts as $part) {
80 if (!isset($config[$part])) {
81 $config[$part] = [];
82 }
83 $config = &$config[$part];
84 }
85
86 if(is_array($config) && is_array($value)){
87 $config = array_merge($config, $value);
88 }else{
89 $config = $value;
90 }
91 }
92
93 /**
94 * Get the decoder for a given name
95 * @param $name string Decoder name
96 *
97 * @return DecoderInterface
98 * @throws DecoderNotFoundException
99 */
100 public function getDecoder(string $name): DecoderInterface {
101 $default_decoders = $this->get('decoding.decoder', [
102 'header' => \Webklex\PHPIMAP\Decoder\HeaderDecoder::class,
103 'message' => \Webklex\PHPIMAP\Decoder\MessageDecoder::class,
104 'attachment' => \Webklex\PHPIMAP\Decoder\AttachmentDecoder::class
105 ]);
106 $options = $this->get('decoding.options', [
107 'header' => 'utf-8',
108 'message' => 'utf-8',
109 'attachment' => 'utf-8',
110 ]);
111 if (isset($default_decoders[$name])) {
112 if (class_exists($default_decoders[$name])) {
113 return new $default_decoders[$name]($options);
114 }
115 }
116 throw new DecoderNotFoundException();
117 }
118
119 /**
120 * Get the mask for a given section
121 * @param string $section section name such as "message" or "attachment"
122 *
123 * @return string|null
124 */
125 public function getMask(string $section): ?string {
126 $default_masks = $this->get('masks', []);
127 if (isset($default_masks[$section])) {
128 if (class_exists($default_masks[$section])) {
129 return $default_masks[$section];
130 }
131 }
132 return null;
133 }
134
135 /**
136 * Get the account configuration.
137 * @param string|null $name
138 *
139 * @return self
140 */
141 public function getClientConfig(?string $name): self {
142 $config = $this->all();
143 $defaultName = $this->getDefaultAccount();
144 $defaultAccount = $this->get('accounts.'.$defaultName, []);
145
146 if ($name === null || $name === 'null' || $name === "") {
147 $account = $defaultAccount;
148 $name = $defaultName;
149 }else{
150 $account = $this->get('accounts.'.$name, $defaultAccount);
151 }
152
153 $config["default"] = $name;
154 $config["accounts"] = [
155 $name => $account
156 ];
157
158 return new self($config);
159 }
160
161 /**
162 * Get the name of the default account.
163 *
164 * @return string
165 */
166 public function getDefaultAccount(): string {
167 return $this->get('default', 'default');
168 }
169
170 /**
171 * Set the name of the default account.
172 * @param string $name
173 *
174 * @return void
175 */
176 public function setDefaultAccount(string $name): void {
177 $this->set('default', $name);
178 }
179
180 /**
181 * Create a new instance of the Config class
182 * @param array|string $config
183 * @return Config
184 */
185 public static function make(array|string $config = []): Config {
186 if (is_array($config) === false) {
187 $config = require $config;
188 }
189
190 $config_key = 'imap';
191 $path = __DIR__ . '/config/' . $config_key . '.php';
192
193 $vendor_config = require $path;
194 $config = self::array_merge_recursive_distinct($vendor_config, $config);
195
196 if (isset($config['default'])) {
197 if (isset($config['accounts']) && $config['default']) {
198
199 $default_config = $vendor_config['accounts']['default'];
200 if (isset($config['accounts'][$config['default']])) {
201 $default_config = array_merge($default_config, $config['accounts'][$config['default']]);
202 }
203
204 if (is_array($config['accounts'])) {
205 foreach ($config['accounts'] as $account_key => $account) {
206 $config['accounts'][$account_key] = array_merge($default_config, $account);
207 }
208 }
209 }
210 }
211
212 return new self($config);
213 }
214
215 /**
216 * Marge arrays recursively and distinct
217 *
218 * Merges any number of arrays / parameters recursively, replacing
219 * entries with string keys with values from latter arrays.
220 * If the entry or the next value to be assigned is an array, then it
221 * automatically treats both arguments as an array.
222 * Numeric entries are appended, not replaced, but only if they are
223 * unique
224 *
225 * @return array
226 *
227 * @link http://www.php.net/manual/en/function.array-merge-recursive.php#96201
228 * @author Mark Roduner <[email protected]>
229 */
230 private static function array_merge_recursive_distinct(): array {
231 $arrays = func_get_args();
232 $base = array_shift($arrays);
233
234 // From https://stackoverflow.com/a/173479
235 $isAssoc = function(array $arr) {
236 if (array() === $arr) return false;
237 return array_keys($arr) !== range(0, count($arr) - 1);
238 };
239
240 if (!is_array($base)) $base = empty($base) ? array() : array($base);
241
242 foreach ($arrays as $append) {
243 if (!is_array($append)) $append = array($append);
244
245 foreach ($append as $key => $value) {
246
247 if (!array_key_exists($key, $base) and !is_numeric($key)) {
248 $base[$key] = $value;
249 continue;
250 }
251
252 if ((is_array($value) && $isAssoc($value)) || (is_array($base[$key]) && $isAssoc($base[$key]))) {
253 // If the arrays are not associates we don't want to array_merge_recursive_distinct
254 // else merging $baseConfig['dispositions'] = ['attachment', 'inline'] with $customConfig['dispositions'] = ['attachment']
255 // results in $resultConfig['dispositions'] = ['attachment', 'inline']
256 $base[$key] = self::array_merge_recursive_distinct($base[$key], $value);
257 } else if (is_numeric($key)) {
258 if (!in_array($value, $base)) $base[] = $value;
259 } else {
260 $base[$key] = $value;
261 }
262
263 }
264
265 }
266
267 return $base;
268 }
269
270 /**
271 * Get all configuration values
272 * @return array
273 */
274 public function all(): array {
275 return $this->config;
276 }
277
278 /**
279 * Check if a configuration value exists
280 * @param string $key
281 * @return bool
282 */
283 public function has(string $key): bool {
284 return $this->get($key) !== null;
285 }
286
287 /**
288 * Remove all configuration values
289 * @return $this
290 */
291 public function clear(): static {
292 $this->config = [];
293 return $this;
294 }
295 }