-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataObject.php
More file actions
176 lines (147 loc) · 3.84 KB
/
DataObject.php
File metadata and controls
176 lines (147 loc) · 3.84 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
<?php
namespace Gt\DataObject;
use DateTimeImmutable;
use DateTimeInterface;
use Gt\TypeSafeGetter\NullableTypeSafeGetter;
use Gt\TypeSafeGetter\TypeSafeGetter;
use JsonSerializable;
use TypeError;
class DataObject implements JsonSerializable, TypeSafeGetter {
use NullableTypeSafeGetter;
/** @var array<string, mixed> */
protected array $data;
public function __construct() {
$this->data = [];
}
public function with(string $key, mixed $data):static {
$clone = clone $this;
$clone->data[$key] = $data;
return $clone;
}
public function without(string $key):static {
$clone = clone $this;
unset($clone->data[$key]);
return $clone;
}
public function get(string $name):mixed {
return $this->data[$name] ?? null;
}
public function getObject(string $name):?static {
return $this->getInstance($name, static::class);
}
public function contains(string $name):bool {
return isset($this->data[$name]);
}
public function typeof(string $name):?string {
if(!array_key_exists($name, $this->data)) {
return null;
}
$value = $this->data[$name];
switch(gettype($value)) {
case "integer":
return "int";
case "double":
return "float";
case "boolean":
return "bool";
case "NULL":
return "null";
case "object":
return get_class($value);
default:
return gettype($value);
}
}
/** @noinspection PhpMixedReturnTypeCanBeReducedInspection - This allows php.gt/json to extend and return primitives */
public function jsonSerialize():mixed {
return $this->asArray();
}
/**
* Get an array by name, with optionally fixed types - specify a $type
* as either an inbuilt primitive (string, int, etc.) or a class name
* (DateTime::class, Example::class, etc.)
* @return mixed[]
*/
public function getArray(string $name, ?string $type = null):?array {
$array = $this->get($name);
if(is_object($array) && method_exists($array, "asArray")) {
$array = $array->asArray();
}
if($array && $type) {
$array = $this->checkArrayType($array, $type);
}
return $array;
}
/** @return array<string, mixed> */
public function asArray():array {
$array = $this->data;
array_walk_recursive($array, function(&$item):void {
if($item instanceof static) {
$item = $item->asArray();
}
});
return $array;
}
public function asObject():object {
$array = $this->data;
array_walk_recursive($array, function(&$item):void {
if($item instanceof static) {
$item = $item->asObject();
}
});
return (object)$array;
}
/**
* @param array<mixed> $array
* @return array<mixed>
*/
private function checkArrayType(array $array, string $type): array {
$this->validateTypeExists($type);
foreach ($array as $i => $value) {
$array[$i] = $this->processValue($value, $type, $i);
}
return $array;
}
private function validateTypeExists(string $type): void {
if(!class_exists($type)
&& !interface_exists($type)
&& !function_exists("is_$type")) {
throw new TypeError("Invalid type: $type does not exist.");
}
}
private function processValue(
mixed $value,
string $type,
int $index,
): mixed {
if (class_exists($type) || interface_exists($type)) {
$this->assertInstanceOfType($value, $type, $index);
} elseif (function_exists("is_$type")) {
return $this->castValue($value, $type);
}
return $value;
}
private function assertInstanceOfType(
mixed $value,
string $type,
int $index,
): void {
if (!is_a($value, $type)) {
$actualType = is_scalar($value)
? gettype($value)
: get_class($value);
throw new TypeError("Array index $index"
. " must be of type $type, $actualType given");
}
}
private function castValue(mixed $value, string $type): mixed {
return match ($type) {
"int" => (int)$value,
"bool" => (bool)$value,
"string" => (string)$value,
"float", "double" => (float)$value,
"array" => (array)$value,
default => null,
};
}
}