|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Jared King <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @see http://jaredtking.com |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright 2015 Jared King |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Pulsar; |
|
13
|
|
|
|
|
14
|
|
|
use Defuse\Crypto\Crypto; |
|
15
|
|
|
use Defuse\Crypto\Key; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Handles value type casting. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Type |
|
21
|
|
|
{ |
|
22
|
|
|
const STRING = 'string'; |
|
23
|
|
|
const INTEGER = 'integer'; |
|
24
|
|
|
const FLOAT = 'float'; |
|
25
|
|
|
const BOOLEAN = 'boolean'; |
|
26
|
|
|
const DATE = 'date'; |
|
27
|
|
|
const OBJECT = 'object'; |
|
28
|
|
|
const ARRAY = 'array'; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Key|null */ |
|
31
|
|
|
private static $encryptionKey; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Marshals a value for a given property from storage. |
|
35
|
|
|
* |
|
36
|
|
|
* @param mixed $value |
|
37
|
|
|
* |
|
38
|
|
|
* @return mixed type-casted value |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function cast(Property $property, $value) |
|
41
|
|
|
{ |
|
42
|
|
|
if (null === $value) { |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// handle empty strings as null |
|
47
|
|
|
if ($property->isNullable() && '' === $value) { |
|
48
|
|
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// perform decryption, if enabled |
|
52
|
|
|
if ($property->isEncrypted()) { |
|
53
|
|
|
$value = Crypto::decrypt($value, self::$encryptionKey); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$type = $property->getType(); |
|
57
|
|
|
if (!$type) { |
|
|
|
|
|
|
58
|
|
|
return $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$m = 'to_'.$property->getType(); |
|
62
|
|
|
|
|
63
|
|
|
return self::$m($value); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Casts a value to a string. |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $value |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function to_string($value): string |
|
72
|
|
|
{ |
|
73
|
|
|
return (string) $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Casts a value to an integer. |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $value |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function to_integer($value): int |
|
82
|
|
|
{ |
|
83
|
|
|
return (int) $value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Casts a value to a float. |
|
88
|
|
|
* |
|
89
|
|
|
* @param mixed $value |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function to_float($value): float |
|
92
|
|
|
{ |
|
93
|
|
|
return (float) $value; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Casts a value to a boolean. |
|
98
|
|
|
* |
|
99
|
|
|
* @param mixed $value |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function to_boolean($value): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Casts a date value as a UNIX timestamp. |
|
108
|
|
|
* |
|
109
|
|
|
* @param mixed $value |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function to_date($value): int |
|
112
|
|
|
{ |
|
113
|
|
|
if (!is_numeric($value)) { |
|
114
|
|
|
return strtotime($value); |
|
115
|
|
|
} else { |
|
116
|
|
|
return $value + 0; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Casts a value to an array. |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $value |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function to_array($value): array |
|
126
|
|
|
{ |
|
127
|
|
|
// decode JSON strings into an array |
|
128
|
|
|
if (is_string($value)) { |
|
129
|
|
|
return (array) json_decode($value, true); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return (array) $value; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Casts a value to an object. |
|
137
|
|
|
* |
|
138
|
|
|
* @param mixed $value |
|
139
|
|
|
* |
|
140
|
|
|
* @return object |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function to_object($value): \stdClass |
|
143
|
|
|
{ |
|
144
|
|
|
// decode JSON strings into an object |
|
145
|
|
|
if (is_string($value)) { |
|
146
|
|
|
return (object) json_decode($value); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return (object) $value; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Sets the encryption key to be used when encryption is enabled for a property. |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function setEncryptionKey(Key $key): void |
|
156
|
|
|
{ |
|
157
|
|
|
self::$encryptionKey = $key; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Gets the encryption key, if used. |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function getEncryptionKey(): ?Key |
|
164
|
|
|
{ |
|
165
|
|
|
return self::$encryptionKey; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: