-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractConfigProps.php
More file actions
171 lines (150 loc) · 3.78 KB
/
AbstractConfigProps.php
File metadata and controls
171 lines (150 loc) · 3.78 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
<?php
declare(strict_types=1);
namespace MaplePHP\Emitron;
use MaplePHP\DTO\Format\Str;
use MaplePHP\Emitron\Contracts\ConfigPropsInterface;
abstract class AbstractConfigProps implements ConfigPropsInterface
{
/** @var array <int, string> */
public array $missingProps = [];
public ?string $path = null;
public ?string $test = null;
private array $propDesc = [];
private static array $childPropCache = [];
/**
* Hydrate the properties/object with expected data, and handle unexpected data
*
* @param string|bool $key
* @param mixed $value
* @return void
*/
abstract protected function propsHydration(string|bool $key, mixed $value): void;
/**
* Set type safe config props
*
* @param array $props
*/
public function __construct(array $props = [])
{
if ($props !== []) {
$this->setProps($props);
}
}
/**
* Check if property exists
*
* @param string|string $key
* @return bool|string
*/
public function hasProp(string $key): bool|string
{
if(str_contains($key, "-")) {
$key = Str::value($key)->camelCaseFromSep()->get();
}
return property_exists($this, $key) ? $key : false;
}
/**
* Set config props - this is passed as configurations to the dispatcher
*
* @param string $key
* @param mixed $value
* @return self
*/
public function setProp(string $key, mixed $value): self
{
$newKey = $this->hasProp($key);
if ($newKey === false) {
$this->missingProps[] = $key;
}
$this->propsHydration($newKey, $value);
return $this;
}
/**
* Add description to prop
*
* @param string $key
* @param string $desc
* @return $this
*/
protected function setPropDesc(string $key, string $desc): self
{
$this->propDesc[$key] = $desc;
return $this;
}
/**
* Get description to prop
*
* @param string $key
* @param string $desc
* @return string
*/
public function getPropDesc(string $key): string
{
return $this->propDesc[$key] ?? "";
}
/**
* Set multiple config props
*
* @param array $props
* @return self
*/
public function setProps(array $props): self
{
foreach ($props as $key => $value) {
$this->setProp($key, $value);
}
return $this;
}
/**
* If you try to set unsupported properties, this will return them
*
* @return array
*/
public function hasMissingProps(): array
{
return $this->missingProps;
}
/**
* Get property value else null if not set nor exists
*
* @return array
*/
public function get(string $key): mixed
{
$newKey = $this->hasProp($key);
return ($newKey !== false) ? $this->{$newKey} : null;
}
/**
* Return public properties defined on the concrete class
*/
public function toArray(): array
{
$vars = get_object_vars($this);
if (!isset(self::$childPropCache[static::class])) {
$childDefaults = get_class_vars(static::class);
$baseDefaults = get_class_vars(self::class);
self::$childPropCache[static::class] = array_diff_key($childDefaults, $baseDefaults);
}
return array_intersect_key($vars, self::$childPropCache[static::class]);
}
/**
* Get value as bool value
*
* @param string $value
* @return bool
*/
public function dataToBool(mixed $value): bool
{
return isset($value) && $value !== false && $value !== 'false';
}
/**
* Quick validation of locale
*
* @param string $locale
* @return bool
*/
protected function isValidLocale(string $locale): bool
{
return (bool) preg_match('/^[a-z]{2,3}(?:_[A-Z]{2})?$/', $locale);
}
}