1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DataObjects; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Carbon\CarbonInterface; |
7
|
|
|
use DateTimeInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ValueCaster |
11
|
|
|
* @package ByTIC\DataObjects |
12
|
|
|
*/ |
13
|
|
|
class ValueCaster |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The built-in, primitive cast types |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
public static $primitiveTypes = [ |
21
|
|
|
'array', |
22
|
|
|
'bool', |
23
|
|
|
'boolean', |
24
|
|
|
'collection', |
25
|
|
|
'custom_datetime', |
26
|
|
|
'date', |
27
|
|
|
'datetime', |
28
|
|
|
'decimal', |
29
|
|
|
'double', |
30
|
|
|
'float', |
31
|
|
|
'int', |
32
|
|
|
'integer', |
33
|
|
|
'json', |
34
|
|
|
'object', |
35
|
|
|
'real', |
36
|
|
|
'string', |
37
|
|
|
'timestamp', |
38
|
|
|
]; |
39
|
|
|
|
40
|
1 |
|
public static function as($value, string $type) |
41
|
|
|
{ |
42
|
1 |
|
switch ($type) { |
43
|
1 |
|
case 'int': |
44
|
1 |
|
case 'integer': |
45
|
|
|
return (int)$value; |
46
|
|
|
|
47
|
1 |
|
case 'real': |
48
|
1 |
|
case 'float': |
49
|
1 |
|
case 'double': |
50
|
|
|
return static::asFloat($value); |
51
|
|
|
|
52
|
1 |
|
case 'decimal': |
53
|
|
|
return static::asDecimal($value); |
54
|
|
|
|
55
|
1 |
|
case 'string': |
56
|
1 |
|
case 'str': |
57
|
|
|
return static::asString($value); |
58
|
|
|
|
59
|
1 |
|
case 'bool': |
60
|
1 |
|
case 'boolean': |
61
|
|
|
return (bool) $value; |
62
|
|
|
|
63
|
1 |
|
case 'date': |
64
|
|
|
return static::asDate($value); |
65
|
|
|
|
66
|
1 |
|
case 'datetime': |
67
|
|
|
case 'custom_datetime': |
68
|
1 |
|
return static::asDateTime($value); |
69
|
|
|
|
70
|
|
|
case 'timestamp': |
71
|
|
|
return static::asTimestamp($value); |
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
case 'object': |
75
|
|
|
return static::fromJson($value, true); |
76
|
|
|
|
77
|
|
|
case 'array': |
78
|
|
|
case 'json': |
79
|
|
|
return static::fromJson($value); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Cast given value to a string |
85
|
|
|
* |
86
|
|
|
* @param mixed $value |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public static function asString($value): string |
91
|
|
|
{ |
92
|
|
|
return (string)$value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Cast given value to a string |
97
|
|
|
* |
98
|
|
|
* @param mixed $value |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public static function asInteger($value): string |
103
|
|
|
{ |
104
|
|
|
return (int)$value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Decode the given float. |
109
|
|
|
* |
110
|
|
|
* @param mixed $value |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public static function asFloat($value) |
114
|
|
|
{ |
115
|
|
|
switch ((string) $value) { |
116
|
|
|
case 'Infinity': |
117
|
|
|
return INF; |
118
|
|
|
case '-Infinity': |
119
|
|
|
return -INF; |
120
|
|
|
case 'NaN': |
121
|
|
|
return NAN; |
122
|
|
|
default: |
123
|
|
|
return (float) $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return a decimal as string. |
129
|
|
|
* |
130
|
|
|
* @param float $value |
131
|
|
|
* @param int $decimals |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public static function asDecimal($value, $decimals = 3) |
135
|
|
|
{ |
136
|
|
|
return number_format($value, $decimals, '.', ''); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Cast given value to a DateTime instance |
141
|
|
|
* |
142
|
|
|
* @param string|null $value |
143
|
|
|
* |
144
|
|
|
* @return Carbon|DateTimeInterface |
145
|
|
|
*/ |
146
|
|
|
public static function asDate($value) |
147
|
1 |
|
{ |
148
|
|
|
return Carbon::parse($value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
1 |
|
* @param $value |
153
|
1 |
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
public static function asDateTime($value) |
156
|
|
|
{ |
157
|
|
|
// If this value is already a Carbon instance, we shall just return it as is. |
158
|
|
|
// This prevents us having to re-instantiate a Carbon instance when we know |
159
|
1 |
|
// it already is one, which wouldn't be fulfilled by the DateTime check. |
160
|
|
|
if ($value instanceof CarbonInterface) { |
161
|
|
|
return Carbon::instance($value); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// If the value is already a DateTime instance, we will just skip the rest of |
165
|
|
|
// these checks since they will be a waste of time, and hinder performance |
166
|
|
|
// when checking the field. We will just return the DateTime right away. |
167
|
|
|
if ($value instanceof DateTimeInterface) { |
168
|
|
|
return Carbon::parse( |
169
|
1 |
|
$value->format('Y-m-d H:i:s.u'), |
170
|
|
|
$value->getTimezone() |
171
|
|
|
); |
172
|
|
|
} |
173
|
1 |
|
|
174
|
|
|
// If this value is an integer, we will assume it is a UNIX timestamp's value |
175
|
|
|
// and format a Carbon object from this timestamp. This allows flexibility |
176
|
|
|
// when defining your date fields as they might be UNIX timestamps here. |
177
|
|
|
if (is_numeric($value)) { |
178
|
|
|
return Carbon::createFromTimestamp($value); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return Carbon::parse($value); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Decode the given JSON back into an array or object. |
186
|
|
|
* |
187
|
|
|
* @param string $value |
188
|
|
|
* @param bool $asObject |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public static function fromJson($value, $asObject = false) |
192
|
|
|
{ |
193
|
|
|
return json_decode($value, !$asObject); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param $value |
198
|
|
|
* @return false|string |
199
|
|
|
*/ |
200
|
|
|
public static function asJson($value) |
201
|
|
|
{ |
202
|
|
|
return json_encode($value); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.