Type.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database;
  16. use InvalidArgumentException;
  17. use PDO;
  18. /**
  19. * Encapsulates all conversion functions for values coming from database into PHP and
  20. * going from PHP into database.
  21. */
  22. class Type implements TypeInterface
  23. {
  24. /**
  25. * List of supported database types. A human readable
  26. * identifier is used as key and a complete namespaced class name as value
  27. * representing the class that will do actual type conversions.
  28. *
  29. * @var string[]|\Cake\Database\Type[]
  30. */
  31. protected static $_types = [
  32. 'tinyinteger' => 'Cake\Database\Type\IntegerType',
  33. 'smallinteger' => 'Cake\Database\Type\IntegerType',
  34. 'integer' => 'Cake\Database\Type\IntegerType',
  35. 'biginteger' => 'Cake\Database\Type\IntegerType',
  36. 'binary' => 'Cake\Database\Type\BinaryType',
  37. 'binaryuuid' => 'Cake\Database\Type\BinaryUuidType',
  38. 'boolean' => 'Cake\Database\Type\BoolType',
  39. 'date' => 'Cake\Database\Type\DateType',
  40. 'datetime' => 'Cake\Database\Type\DateTimeType',
  41. 'decimal' => 'Cake\Database\Type\DecimalType',
  42. 'float' => 'Cake\Database\Type\FloatType',
  43. 'json' => 'Cake\Database\Type\JsonType',
  44. 'string' => 'Cake\Database\Type\StringType',
  45. 'text' => 'Cake\Database\Type\StringType',
  46. 'time' => 'Cake\Database\Type\TimeType',
  47. 'timestamp' => 'Cake\Database\Type\DateTimeType',
  48. 'uuid' => 'Cake\Database\Type\UuidType',
  49. ];
  50. /**
  51. * List of basic type mappings, used to avoid having to instantiate a class
  52. * for doing conversion on these.
  53. *
  54. * @var array
  55. * @deprecated 3.1 All types will now use a specific class
  56. */
  57. protected static $_basicTypes = [
  58. 'string' => ['callback' => [Type::class, 'strval']],
  59. 'text' => ['callback' => [Type::class, 'strval']],
  60. 'boolean' => [
  61. 'callback' => [Type::class, 'boolval'],
  62. 'pdo' => PDO::PARAM_BOOL
  63. ],
  64. ];
  65. /**
  66. * Contains a map of type object instances to be reused if needed.
  67. *
  68. * @var \Cake\Database\Type[]
  69. */
  70. protected static $_builtTypes = [];
  71. /**
  72. * Identifier name for this type
  73. *
  74. * @var string|null
  75. */
  76. protected $_name;
  77. /**
  78. * Constructor
  79. *
  80. * @param string|null $name The name identifying this type
  81. */
  82. public function __construct($name = null)
  83. {
  84. $this->_name = $name;
  85. }
  86. /**
  87. * Returns a Type object capable of converting a type identified by name.
  88. *
  89. * @param string $name type identifier
  90. * @throws \InvalidArgumentException If type identifier is unknown
  91. * @return \Cake\Database\Type
  92. */
  93. public static function build($name)
  94. {
  95. if (isset(static::$_builtTypes[$name])) {
  96. return static::$_builtTypes[$name];
  97. }
  98. if (!isset(static::$_types[$name])) {
  99. throw new InvalidArgumentException(sprintf('Unknown type "%s"', $name));
  100. }
  101. if (is_string(static::$_types[$name])) {
  102. return static::$_builtTypes[$name] = new static::$_types[$name]($name);
  103. }
  104. return static::$_builtTypes[$name] = static::$_types[$name];
  105. }
  106. /**
  107. * Returns an arrays with all the mapped type objects, indexed by name.
  108. *
  109. * @return array
  110. */
  111. public static function buildAll()
  112. {
  113. $result = [];
  114. foreach (static::$_types as $name => $type) {
  115. $result[$name] = isset(static::$_builtTypes[$name]) ? static::$_builtTypes[$name] : static::build($name);
  116. }
  117. return $result;
  118. }
  119. /**
  120. * Returns a Type object capable of converting a type identified by $name
  121. *
  122. * @param string $name The type identifier you want to set.
  123. * @param \Cake\Database\Type $instance The type instance you want to set.
  124. * @return void
  125. */
  126. public static function set($name, Type $instance)
  127. {
  128. static::$_builtTypes[$name] = $instance;
  129. }
  130. /**
  131. * Registers a new type identifier and maps it to a fully namespaced classname,
  132. * If called with no arguments it will return current types map array
  133. * If $className is omitted it will return mapped class for $type
  134. *
  135. * Deprecated 3.6.2:
  136. * - The usage of $type as string[]|\Cake\Database\Type[] is deprecated.
  137. * Use Type::setMap() with string[] instead.
  138. * - Passing $className as \Cake\Database\Type instance is deprecated, use
  139. * class name string only.
  140. * - Using this method as getter is deprecated. Use Type::getMap() instead.
  141. *
  142. * @param string|string[]|\Cake\Database\Type[]|null $type If string name of type to map, if array list of arrays to be mapped
  143. * @param string|\Cake\Database\Type|null $className The classname or object instance of it to register.
  144. * @return array|string|null If $type is null then array with current map, if $className is null string
  145. * configured class name for give $type, null otherwise
  146. */
  147. public static function map($type = null, $className = null)
  148. {
  149. if ($type === null) {
  150. deprecationWarning(
  151. 'Using `Type::map()` as getter is deprecated. ' .
  152. 'Use `Type::getMap()` instead.'
  153. );
  154. return static::$_types;
  155. }
  156. if (is_array($type)) {
  157. deprecationWarning(
  158. 'Using `Type::map()` to set complete types map is deprecated. ' .
  159. 'Use `Type::setMap()` instead.'
  160. );
  161. static::$_types = $type;
  162. return null;
  163. }
  164. if ($className === null) {
  165. deprecationWarning(
  166. 'Using `Type::map()` as getter is deprecated. ' .
  167. 'Use `Type::getMap()` instead.'
  168. );
  169. return isset(static::$_types[$type]) ? static::$_types[$type] : null;
  170. }
  171. if (!is_string($className)) {
  172. deprecationWarning(
  173. 'Passing $className as object to Type::map() is deprecated. ' .
  174. 'Use Type::set() instead.'
  175. );
  176. }
  177. static::$_types[$type] = $className;
  178. unset(static::$_builtTypes[$type]);
  179. }
  180. /**
  181. * Set type to classname mapping.
  182. *
  183. * @param string[] $map List of types to be mapped.
  184. * @return void
  185. * @since 3.6.2
  186. */
  187. public static function setMap(array $map)
  188. {
  189. static::$_types = $map;
  190. static::$_builtTypes = [];
  191. }
  192. /**
  193. * Get mapped class name or instance for type(s).
  194. *
  195. * @param string|null $type Type name to get mapped class for or null to get map array.
  196. * @return array|string|\Cake\Database\TypeInterface|null Configured class name or instance for give $type or map array.
  197. * @since 3.6.2
  198. */
  199. public static function getMap($type = null)
  200. {
  201. if ($type === null) {
  202. return static::$_types;
  203. }
  204. return isset(static::$_types[$type]) ? static::$_types[$type] : null;
  205. }
  206. /**
  207. * Clears out all created instances and mapped types classes, useful for testing
  208. *
  209. * @return void
  210. */
  211. public static function clear()
  212. {
  213. static::$_types = [];
  214. static::$_builtTypes = [];
  215. }
  216. /**
  217. * {@inheritDoc}
  218. */
  219. public function getName()
  220. {
  221. return $this->_name;
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. public function getBaseType()
  227. {
  228. return $this->_name;
  229. }
  230. /**
  231. * {@inheritDoc}
  232. */
  233. public function toDatabase($value, Driver $driver)
  234. {
  235. return $this->_basicTypeCast($value);
  236. }
  237. /**
  238. * Casts given value from a database type to PHP equivalent
  239. *
  240. * @param mixed $value Value to be converted to PHP equivalent
  241. * @param \Cake\Database\Driver $driver Object from which database preferences and configuration will be extracted
  242. * @return mixed
  243. */
  244. public function toPHP($value, Driver $driver)
  245. {
  246. return $this->_basicTypeCast($value);
  247. }
  248. /**
  249. * Checks whether this type is a basic one and can be converted using a callback
  250. * If it is, returns converted value
  251. *
  252. * @param mixed $value Value to be converted to PHP equivalent
  253. * @return mixed
  254. * @deprecated 3.1 All types should now be a specific class
  255. */
  256. protected function _basicTypeCast($value)
  257. {
  258. deprecationWarning('Type::_basicTypeCast() is deprecated.');
  259. if ($value === null) {
  260. return null;
  261. }
  262. if (!empty(static::$_basicTypes[$this->_name])) {
  263. $typeInfo = static::$_basicTypes[$this->_name];
  264. if (isset($typeInfo['callback'])) {
  265. return $typeInfo['callback']($value);
  266. }
  267. }
  268. return $value;
  269. }
  270. /**
  271. * {@inheritDoc}
  272. */
  273. public function toStatement($value, Driver $driver)
  274. {
  275. if ($value === null) {
  276. return PDO::PARAM_NULL;
  277. }
  278. return PDO::PARAM_STR;
  279. }
  280. /**
  281. * Type converter for boolean values.
  282. *
  283. * Will convert string true/false into booleans.
  284. *
  285. * @param mixed $value The value to convert to a boolean.
  286. * @return bool
  287. * @deprecated 3.1.8 This method is now unused.
  288. */
  289. public static function boolval($value)
  290. {
  291. deprecationWarning('Type::boolval() is deprecated.');
  292. if (is_string($value) && !is_numeric($value)) {
  293. return strtolower($value) === 'true';
  294. }
  295. return !empty($value);
  296. }
  297. /**
  298. * Type converter for string values.
  299. *
  300. * Will convert values into strings
  301. *
  302. * @param mixed $value The value to convert to a string.
  303. * @return string
  304. * @deprecated 3.1.8 This method is now unused.
  305. */
  306. public static function strval($value)
  307. {
  308. deprecationWarning('Type::strval() is deprecated.');
  309. if (is_array($value)) {
  310. $value = '';
  311. }
  312. return (string)$value;
  313. }
  314. /**
  315. * {@inheritDoc}
  316. */
  317. public function newId()
  318. {
  319. return null;
  320. }
  321. /**
  322. * {@inheritDoc}
  323. */
  324. public function marshal($value)
  325. {
  326. return $this->_basicTypeCast($value);
  327. }
  328. /**
  329. * Returns an array that can be used to describe the internal state of this
  330. * object.
  331. *
  332. * @return array
  333. */
  334. public function __debugInfo()
  335. {
  336. return [
  337. 'name' => $this->_name,
  338. ];
  339. }
  340. }