/
Type.php
241 lines (220 loc) · 6.51 KB
/
Type.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Database;
use Cake\Database\Driver;
use PDO;
/**
* Encapsulates all conversion functions for values coming from database into PHP and
* going from PHP into database.
*/
class Type {
/**
* List of supported database types. A human readable
* identifier is used as key and a complete namespaced class name as value
* representing the class that will do actual type conversions.
*
* @var array
*/
protected static $_types = [
'binary' => 'Cake\Database\Type\BinaryType',
'date' => 'Cake\Database\Type\DateType',
'datetime' => 'Cake\Database\Type\DateTimeType',
'timestamp' => 'Cake\Database\Type\DateTimeType',
'time' => 'Cake\Database\Type\TimeType',
'uuid' => 'Cake\Database\Type\UuidType',
];
/**
* List of basic type mappings, used to avoid having to instantiate a class
* for doing conversion on these
*
* @var array
*/
protected static $_basicTypes = [
'float' => ['callback' => 'floatval'],
'decimal' => ['callback' => 'floatval'],
'integer' => ['callback' => 'intval', 'pdo' => PDO::PARAM_INT],
'biginteger' => ['callback' => 'intval', 'pdo' => PDO::PARAM_INT],
'string' => ['callback' => 'strval'],
'text' => ['callback' => 'strval'],
'boolean' => [
'callback' => ['\Cake\Database\Type', 'boolval'],
'pdo' => PDO::PARAM_BOOL
],
];
/**
* Contains a map of type object instances to be reused if needed
*
* @var array
*/
protected static $_builtTypes = [];
/**
* Identifier name for this type
*
* @var string
*/
protected $_name = null;
/**
* Constructor
*
* @param string $name The name identifying this type
*/
public function __construct($name = null) {
$this->_name = $name;
}
/**
* Returns a Type object capable of converting a type identified by $name
*
* @param string $name type identifier
* @throws \InvalidArgumentException If type identifier is unknown
* @return Type
*/
public static function build($name) {
if (isset(static::$_builtTypes[$name])) {
return static::$_builtTypes[$name];
}
if (isset(static::$_basicTypes[$name])) {
return static::$_builtTypes[$name] = new static($name);
}
if (!isset(static::$_types[$name])) {
throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $name));
}
return static::$_builtTypes[$name] = new static::$_types[$name]($name);
}
/**
* Registers a new type identifier and maps it to a fully namespaced classname,
* If called with no arguments it will return current types map array
* If $className is omitted it will return mapped class for $type
*
* @param string|array $type if string name of type to map, if array list of arrays to be mapped
* @param string $className
* @return array|string|null if $type is null then array with current map, if $className is null string
* configured class name for give $type, null otherwise
*/
public static function map($type = null, $className = null) {
if ($type === null) {
return self::$_types;
}
if (!is_string($type)) {
self::$_types = $type;
return;
}
if ($className === null) {
return isset(self::$_types[$type]) ? self::$_types[$type] : null;
}
self::$_types[$type] = $className;
}
/**
* Clears out all created instances and mapped types classes, useful for testing
*
* @return void
*/
public static function clear() {
self::$_types = [];
self::$_builtTypes = [];
}
/**
* Returns type identifier name for this object
*
* @return string
*/
public function getName() {
return $this->_name;
}
/**
* Casts given value from a PHP type to one acceptable by database
*
* @param mixed $value value to be converted to database equivalent
* @param Driver $driver object from which database preferences and configuration will be extracted
* @return mixed
*/
public function toDatabase($value, Driver $driver) {
return $this->_basicTypeCast($value, $driver);
}
/**
* Casts given value from a database type to PHP equivalent
*
* @param mixed $value value to be converted to PHP equivalent
* @param Driver $driver object from which database preferences and configuration will be extracted
* @return mixed
*/
public function toPHP($value, Driver $driver) {
return $this->_basicTypeCast($value, $driver);
}
/**
* Checks whether this type is a basic one and can be converted using a callback
* If it is, returns converted value
*
* @param mixed $value value to be converted to PHP equivalent
* @param Driver $driver object from which database preferences and configuration will be extracted
* @return mixed
*/
protected function _basicTypeCast($value, Driver $driver) {
if (is_null($value)) {
return null;
}
if (!empty(self::$_basicTypes[$this->_name])) {
$typeInfo = self::$_basicTypes[$this->_name];
if (isset($typeInfo['callback'])) {
return $typeInfo['callback']($value);
}
}
return $value;
}
/**
* Casts give value to Statement equivalent
*
* @param mixed $value value to be converted to PHP equivalent
* @param Driver $driver object from which database preferences and configuration will be extracted
* @return mixed
*/
public function toStatement($value, Driver $driver) {
if (is_null($value)) {
return PDO::PARAM_NULL;
}
if (!empty(self::$_basicTypes[$this->_name])) {
$typeInfo = self::$_basicTypes[$this->_name];
return isset($typeInfo['pdo']) ? $typeInfo['pdo'] : PDO::PARAM_STR;
}
return PDO::PARAM_STR;
}
/**
* Type converter for boolean values.
*
* Will convert string true/false into booleans.
*
* @param mixed $value The value to convert to a boolean.
* @return boolean
*/
public static function boolval($value) {
if (is_string($value) && !is_numeric($value)) {
return strtolower($value) === 'true' ? true : false;
}
return !empty($value);
}
/**
* Generate a new primary key value for a given type.
*
* This method can be used by types to create new primary key values
* when entities are inserted.
*
* @return mixed A new primary key value.
* @see \Cake\Database\Type\UuidType
*/
public function newId() {
return null;
}
}