-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathBaseConfigSection.php
More file actions
350 lines (298 loc) · 9.63 KB
/
BaseConfigSection.php
File metadata and controls
350 lines (298 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Configuration;
use Cloudinary\ArrayUtils;
use Cloudinary\ClassUtils;
use Cloudinary\JsonUtils;
use Cloudinary\StringUtils;
use InvalidArgumentException;
/**
* Class BaseConfigSection
*
* A base class for a single configuration section.
*/
abstract class BaseConfigSection implements ConfigurableInterface
{
/**
* @var string Placeholder for configuration name, must de defined in each derived class.
*/
protected const CONFIG_NAME = 'BASE_CONFIG';
/**
* @var array of configuration keys that contain sensitive data that should not be exported (for example api key).
*/
protected static array $sensitiveDataKeys = [];
/**
* @var array of configuration key aliases (usually used for deprecated keys backwards compatibility).
*/
protected static array $aliases = [];
/**
* @var array of configuration keys that were explicitly set by user. Used to distinguish from default values.
*/
protected array $explicitlySetKeys = [];
/**
* BaseConfig constructor.
*
*/
public function __construct($parameters = null, bool $includeSensitive = true)
{
$this->importParams($parameters, $includeSensitive);
}
/**
* A getter method for accessing non-public properties.
*
* Used for providing default values for not configured parameters.
*
* @param string $property Property name to get.
*
* @return mixed|null Property value.
*/
public function __get(string $property)
{
if (! property_exists($this, $property)) {
trigger_error('Undefined property: ' . static::class . '::$' . $property);
return null;
}
if (! isset($this->{$property})) {
$defaultConstName = 'DEFAULT_' . strtoupper(StringUtils::camelCaseToSnakeCase($property));
if (defined("static::$defaultConstName")) {
return constant("static::$defaultConstName");
}
}
return isset($this->{$property}) ? $this->{$property} : null;
}
/**
* A setter method for accessing non-public properties.
*
* @param string $name Property name.
* @param mixed $value Property value.
*/
public function __set(string $name, mixed $value)
{
$this->$name = $value;
$this->explicitlySetKeys[$name] = true;
}
/**
* A setter method with chaining for accessing non-public properties.
*
* @param string $name Property name.
* @param mixed $value Property value.
*
* @return $this
*
* @internal
*/
public function setConfig(string $name, mixed $value): static
{
$this->__set(StringUtils::snakeCaseToCamelCase($name), $value);
return $this;
}
/**
* Indicates whether the specified name was explicitly set by user.
*
* @param string $name Property name.
*
*
* @internal
*/
public function isExplicitlySet(string $name): bool
{
return ArrayUtils::get($this->explicitlySetKeys, StringUtils::snakeCaseToCamelCase($name), false);
}
/**
* Determines if a property is set.
*
* @param string $name The name of the property.
*
* @return bool
*/
public function __isset(string $name)
{
$getter = 'get' . ucfirst($name);
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return isset($this->$name);
}
/**
* Imports configuration properties from an array of parameters.
*
* @param ?array $parameters Configuration section parameters.
* @param bool $includeSensitive Whether to include sensitive keys.
*
*/
public function importParams(?array $parameters, bool $includeSensitive = true): static
{
$validKeys = self::importableKeys(self::exportableKeys($includeSensitive));
$validParams = ArrayUtils::whitelist($parameters, $validKeys);
if (empty($validParams)) {
return $this;
}
// set class properties
foreach ($validParams as $name => $value) {
$propertyName = StringUtils::snakeCaseToCamelCase(ArrayUtils::get(static::$aliases, $name, $name));
if (property_exists(static::class, $propertyName)) {
$this->$propertyName = $value;
$this->explicitlySetKeys[$propertyName] = true;
}
}
return $this;
}
/**
* Checks whether provided keys are configured.
*
* @param array $keys The keys to check.
*
* @throws InvalidArgumentException In case not all keys are set.
*/
public function assertNotEmpty(array $keys): void
{
foreach ($keys as $key) {
if (empty($this->$key)) {
throw new InvalidArgumentException("Must supply $key");
}
}
}
/**
* Returns an array of exportable configuration section keys.
*
* @param bool $includeSensitive Whether to include sensitive keys.
*
* @return array of keys
*/
protected static function exportableKeys(bool $includeSensitive = true): array
{
$blacklisted = [static::CONFIG_NAME];
if (! $includeSensitive) {
$blacklisted = array_merge($blacklisted, static::$sensitiveDataKeys);
}
return array_filter(
ClassUtils::getConstants(static::class, $blacklisted),
static fn($key) => ! empty($key) && is_string($key)
);
}
/**
* Returns an array of importable configuration section keys.
*
* @param array $exportableKeys The exportable keys to extend with aliases.
*
* @return array of keys
*/
protected static function importableKeys(array $exportableKeys): array
{
return array_merge($exportableKeys, array_keys(static::$aliases));
}
/**
* Instantiates a new config section using json array as a source.
*
* @param array|string $json Configuration source.
*
* @return static brand-new instance of the configuration section.
*/
public static function fromJson(array|string $json): static
{
$json = JsonUtils::decode($json);
// If provided nested array, pass only parameters
if (array_key_exists(static::CONFIG_NAME, $json)) {
$json = $json[static::CONFIG_NAME];
}
return new static($json);
}
/**
* Instantiates a new config section using Cloudinary url as a source.
*
* @param string $cloudinaryUrl The Cloudinary url.
*
*/
public static function fromCloudinaryUrl(string $cloudinaryUrl): static
{
$config = ConfigUtils::parseCloudinaryUrl($cloudinaryUrl);
return static::fromJson($config);
}
/**
* Imports configuration from a json string or an array as a source.
*
* @param array|string $json Configuration json.
*
*/
public function importJson(array|string $json): static
{
$json = JsonUtils::decode($json);
// If provided nested array, pass only parameters
if (array_key_exists(static::CONFIG_NAME, $json)) {
$json = $json[static::CONFIG_NAME];
}
return $this->importParams($json);
}
/**
* Imports configuration from a Cloudinary url as a source.
*
* @param string $cloudinaryUrl The Cloudinary url.
*
*/
public function importCloudinaryUrl(string $cloudinaryUrl): static
{
$config = ConfigUtils::parseCloudinaryUrl($cloudinaryUrl);
return $this->importJson($config);
}
/**
* Serialises configuration section to a string representation.
*
* @param array $excludedKeys The keys to exclude from export to string.
*
*/
public function toString(array $excludedKeys = []): string
{
$sectionJson = $this->jsonSerialize();
if (empty($sectionJson)) {
return '';
}
$sectionJson[static::CONFIG_NAME] = ArrayUtils::blacklist($sectionJson[static::CONFIG_NAME], $excludedKeys);
return urldecode(http_build_query($sectionJson));
}
/**
* Serialises configuration section to a string representation.
*
* @return string
*/
public function __toString()
{
return $this->toString();
}
/**
* Serialises configuration section to a json array.
*
* @param bool $includeSensitive Whether to include sensitive keys during serialisation.
* @param bool $includeEmptyKeys Whether to include keys without values.
*
* @param bool $includeEmptySections Whether to include sections without keys with non-empty values.
*
* @return array data which can be serialized by json_encode.
*/
public function jsonSerialize(
bool $includeSensitive = true,
bool $includeEmptyKeys = false,
bool $includeEmptySections = false
): array {
$keys = [];
// set class properties
foreach (self::exportableKeys($includeSensitive) as $key) {
$propertyName = StringUtils::snakeCaseToCamelCase($key);
if (property_exists(static::class, $propertyName)
&& ($includeEmptyKeys || isset($this->$propertyName))
) {
$keys[$key] = $this->$propertyName;
}
}
if (empty($keys) && ! $includeEmptySections) {
return [];
}
return [static::CONFIG_NAME => $keys];
}
}