Type.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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
  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 array
  30. */
  31. protected static $_types = [
  32. 'biginteger' => 'Cake\Database\Type\IntegerType',
  33. 'binary' => 'Cake\Database\Type\BinaryType',
  34. 'date' => 'Cake\Database\Type\DateType',
  35. 'float' => 'Cake\Database\Type\FloatType',
  36. 'decimal' => 'Cake\Database\Type\FloatType',
  37. 'integer' => 'Cake\Database\Type\IntegerType',
  38. 'time' => 'Cake\Database\Type\TimeType',
  39. 'datetime' => 'Cake\Database\Type\DateTimeType',
  40. 'timestamp' => 'Cake\Database\Type\DateTimeType',
  41. 'uuid' => 'Cake\Database\Type\UuidType',
  42. ];
  43. /**
  44. * List of basic type mappings, used to avoid having to instantiate a class
  45. * for doing conversion on these
  46. *
  47. * @var array
  48. */
  49. protected static $_basicTypes = [
  50. 'string' => ['callback' => ['\Cake\Database\Type', 'strval']],
  51. 'text' => ['callback' => ['\Cake\Database\Type', 'strval']],
  52. 'boolean' => [
  53. 'callback' => ['\Cake\Database\Type', 'boolval'],
  54. 'pdo' => PDO::PARAM_BOOL
  55. ],
  56. ];
  57. /**
  58. * Contains a map of type object instances to be reused if needed
  59. *
  60. * @var array
  61. */
  62. protected static $_builtTypes = [];
  63. /**
  64. * Identifier name for this type
  65. *
  66. * @var string
  67. */
  68. protected $_name = null;
  69. /**
  70. * Constructor
  71. *
  72. * @param string $name The name identifying this type
  73. */
  74. public function __construct($name = null)
  75. {
  76. $this->_name = $name;
  77. }
  78. /**
  79. * Returns a Type object capable of converting a type identified by $name
  80. *
  81. * @param string $name type identifier
  82. * @throws \InvalidArgumentException If type identifier is unknown
  83. * @return Type
  84. */
  85. public static function build($name)
  86. {
  87. if (isset(static::$_builtTypes[$name])) {
  88. return static::$_builtTypes[$name];
  89. }
  90. if (isset(static::$_basicTypes[$name])) {
  91. return static::$_builtTypes[$name] = new static($name);
  92. }
  93. if (!isset(static::$_types[$name])) {
  94. throw new InvalidArgumentException(sprintf('Unknown type "%s"', $name));
  95. }
  96. return static::$_builtTypes[$name] = new static::$_types[$name]($name);
  97. }
  98. /**
  99. * Returns a Type object capable of converting a type identified by $name
  100. *
  101. * @param string $name The type identifier you want to set.
  102. * @param \Cake\Database\Type $instance The type instance you want to set.
  103. * @return void
  104. */
  105. public static function set($name, Type $instance)
  106. {
  107. static::$_builtTypes[$name] = $instance;
  108. }
  109. /**
  110. * Registers a new type identifier and maps it to a fully namespaced classname,
  111. * If called with no arguments it will return current types map array
  112. * If $className is omitted it will return mapped class for $type
  113. *
  114. * @param string|array|null $type if string name of type to map, if array list of arrays to be mapped
  115. * @param string|null $className The classname to register.
  116. * @return array|string|null if $type is null then array with current map, if $className is null string
  117. * configured class name for give $type, null otherwise
  118. */
  119. public static function map($type = null, $className = null)
  120. {
  121. if ($type === null) {
  122. return self::$_types;
  123. }
  124. if (!is_string($type)) {
  125. self::$_types = $type;
  126. return null;
  127. }
  128. if ($className === null) {
  129. return isset(self::$_types[$type]) ? self::$_types[$type] : null;
  130. }
  131. self::$_types[$type] = $className;
  132. }
  133. /**
  134. * Clears out all created instances and mapped types classes, useful for testing
  135. *
  136. * @return void
  137. */
  138. public static function clear()
  139. {
  140. self::$_types = [];
  141. self::$_builtTypes = [];
  142. }
  143. /**
  144. * Returns type identifier name for this object
  145. *
  146. * @return string
  147. */
  148. public function getName()
  149. {
  150. return $this->_name;
  151. }
  152. /**
  153. * Returns the base type name that this class is inheriting.
  154. * This is useful when extending base type for adding extra functionality
  155. * but still want the rest of the framework to use the same assumptions it would
  156. * do about the base type it inherits from.
  157. *
  158. * @return string
  159. */
  160. public function getBaseType()
  161. {
  162. return $this->_name;
  163. }
  164. /**
  165. * Casts given value from a PHP type to one acceptable by database
  166. *
  167. * @param mixed $value value to be converted to database equivalent
  168. * @param Driver $driver object from which database preferences and configuration will be extracted
  169. * @return mixed
  170. */
  171. public function toDatabase($value, Driver $driver)
  172. {
  173. return $this->_basicTypeCast($value);
  174. }
  175. /**
  176. * Casts given value from a database type to PHP equivalent
  177. *
  178. * @param mixed $value value to be converted to PHP equivalent
  179. * @param Driver $driver object from which database preferences and configuration will be extracted
  180. * @return mixed
  181. */
  182. public function toPHP($value, Driver $driver)
  183. {
  184. return $this->_basicTypeCast($value);
  185. }
  186. /**
  187. * Checks whether this type is a basic one and can be converted using a callback
  188. * If it is, returns converted value
  189. *
  190. * @param mixed $value value to be converted to PHP equivalent
  191. * @return mixed
  192. */
  193. protected function _basicTypeCast($value)
  194. {
  195. if ($value === null) {
  196. return null;
  197. }
  198. if (!empty(self::$_basicTypes[$this->_name])) {
  199. $typeInfo = self::$_basicTypes[$this->_name];
  200. if (isset($typeInfo['callback'])) {
  201. return $typeInfo['callback']($value);
  202. }
  203. }
  204. return $value;
  205. }
  206. /**
  207. * Casts give value to Statement equivalent
  208. *
  209. * @param mixed $value value to be converted to PHP equivalent
  210. * @param Driver $driver object from which database preferences and configuration will be extracted
  211. * @return mixed
  212. */
  213. public function toStatement($value, Driver $driver)
  214. {
  215. if ($value === null) {
  216. return PDO::PARAM_NULL;
  217. }
  218. if (!empty(self::$_basicTypes[$this->_name])) {
  219. $typeInfo = self::$_basicTypes[$this->_name];
  220. return isset($typeInfo['pdo']) ? $typeInfo['pdo'] : PDO::PARAM_STR;
  221. }
  222. return PDO::PARAM_STR;
  223. }
  224. /**
  225. * Type converter for boolean values.
  226. *
  227. * Will convert string true/false into booleans.
  228. *
  229. * @param mixed $value The value to convert to a boolean.
  230. * @return bool
  231. */
  232. public static function boolval($value)
  233. {
  234. if (is_string($value) && !is_numeric($value)) {
  235. return strtolower($value) === 'true' ? true : false;
  236. }
  237. return !empty($value);
  238. }
  239. /**
  240. * Type converter for string values.
  241. *
  242. * Will convert values into strings
  243. *
  244. * @param mixed $value The value to convert to a string.
  245. * @return bool
  246. */
  247. public static function strval($value)
  248. {
  249. if (is_array($value)) {
  250. $value = '';
  251. }
  252. return strval($value);
  253. }
  254. /**
  255. * Generate a new primary key value for a given type.
  256. *
  257. * This method can be used by types to create new primary key values
  258. * when entities are inserted.
  259. *
  260. * @return mixed A new primary key value.
  261. * @see \Cake\Database\Type\UuidType
  262. */
  263. public function newId()
  264. {
  265. return null;
  266. }
  267. /**
  268. * Marshalls flat data into PHP objects.
  269. *
  270. * Most useful for converting request data into PHP objects
  271. * that make sense for the rest of the ORM/Database layers.
  272. *
  273. * @param mixed $value The value to convert.
  274. * @return mixed Converted value.
  275. */
  276. public function marshal($value)
  277. {
  278. return $this->_basicTypeCast($value);
  279. }
  280. }