forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataCaster.php
More file actions
187 lines (159 loc) · 5.69 KB
/
DataCaster.php
File metadata and controls
187 lines (159 loc) · 5.69 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\DataCaster;
use CodeIgniter\DataCaster\Cast\ArrayCast;
use CodeIgniter\DataCaster\Cast\BooleanCast;
use CodeIgniter\DataCaster\Cast\CastInterface;
use CodeIgniter\DataCaster\Cast\CSVCast;
use CodeIgniter\DataCaster\Cast\DatetimeCast;
use CodeIgniter\DataCaster\Cast\FloatCast;
use CodeIgniter\DataCaster\Cast\IntBoolCast;
use CodeIgniter\DataCaster\Cast\IntegerCast;
use CodeIgniter\DataCaster\Cast\JsonCast;
use CodeIgniter\DataCaster\Cast\TimestampCast;
use CodeIgniter\DataCaster\Cast\URICast;
use CodeIgniter\Entity\Cast\CastInterface as EntityCastInterface;
use CodeIgniter\Entity\Exceptions\CastException;
use CodeIgniter\Exceptions\InvalidArgumentException;
final class DataCaster
{
/**
* Array of field names and the type of value to cast.
*
* @var array<string, string> [field => type]
*/
private array $types = [];
/**
* Convert handlers
*
* @var array<string, class-string> [type => classname]
*/
private array $castHandlers = [
'array' => ArrayCast::class,
'bool' => BooleanCast::class,
'boolean' => BooleanCast::class,
'csv' => CSVCast::class,
'datetime' => DatetimeCast::class,
'double' => FloatCast::class,
'float' => FloatCast::class,
'int' => IntegerCast::class,
'integer' => IntegerCast::class,
'int-bool' => IntBoolCast::class,
'json' => JsonCast::class,
'timestamp' => TimestampCast::class,
'uri' => URICast::class,
];
/**
* @param array<string, class-string>|null $castHandlers Custom convert handlers
* @param array<string, string>|null $types [field => type]
* @param object|null $helper Helper object.
* @param bool $strict Strict mode? Set to false for casts for Entity.
*/
public function __construct(
?array $castHandlers = null,
?array $types = null,
private readonly ?object $helper = null,
private readonly bool $strict = true,
) {
$this->castHandlers = array_merge($this->castHandlers, $castHandlers);
if ($types !== null) {
$this->setTypes($types);
}
if ($this->strict) {
foreach ($this->castHandlers as $handler) {
if (
! is_subclass_of($handler, CastInterface::class)
&& ! is_subclass_of($handler, EntityCastInterface::class)
) {
throw new InvalidArgumentException(
'Invalid class type. It must implement CastInterface. class: ' . $handler,
);
}
}
}
}
/**
* This method is only for Entity.
*
* @TODO if Entity::$casts is readonly, we don't need this method.
*
* @param array<string, string> $types [field => type]
*
* @return $this
*
* @internal
*/
public function setTypes(array $types): static
{
$this->types = $types;
return $this;
}
/**
* Provides the ability to cast an item as a specific data type.
* Add ? at the beginning of the type (i.e. ?string) to get `null`
* instead of casting $value when $value is null.
*
* @param mixed $value The value to convert
* @param string $field The field name
* @param 'get'|'set' $method Allowed to "get" and "set"
*/
public function castAs(mixed $value, string $field, string $method = 'get'): mixed
{
// If the type is not defined, return as it is.
if (! isset($this->types[$field])) {
return $value;
}
$type = $this->types[$field];
$isNullable = false;
// Is nullable?
if (str_starts_with($type, '?')) {
$isNullable = true;
if ($value === null) {
return null;
}
$type = substr($type, 1);
} elseif ($value === null) {
if ($this->strict) {
$message = 'Field "' . $field . '" is not nullable, but null was passed.';
throw new InvalidArgumentException($message);
}
}
// In order not to create a separate handler for the
// json-array type, we transform the required one.
$type = ($type === 'json-array') ? 'json[array]' : $type;
$params = [];
// Attempt to retrieve additional parameters if specified
// type[param, param2,param3]
if (preg_match('/\A(.+)\[(.+)\]\z/', $type, $matches)) {
$type = $matches[1];
$params = array_map(trim(...), explode(',', $matches[2]));
}
if ($isNullable && ! $this->strict) {
$params[] = 'nullable';
}
$type = trim($type, '[]');
$handlers = $this->castHandlers;
if (! isset($handlers[$type])) {
throw new InvalidArgumentException(
'No such handler for "' . $field . '". Invalid type: ' . $type,
);
}
$handler = $handlers[$type];
if (
! $this->strict
&& ! is_subclass_of($handler, CastInterface::class)
&& ! is_subclass_of($handler, EntityCastInterface::class)
) {
throw CastException::forInvalidInterface($handler);
}
return $handler::$method($value, $params, $this->helper);
}
}