Type.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 Cake\Database\Driver;
  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. * Registers a new type identifier and maps it to a fully namespaced classname,
  100. * If called with no arguments it will return current types map array
  101. * If $className is omitted it will return mapped class for $type
  102. *
  103. * @param string|array|null $type if string name of type to map, if array list of arrays to be mapped
  104. * @param string|null $className The classname to register.
  105. * @return array|string|null if $type is null then array with current map, if $className is null string
  106. * configured class name for give $type, null otherwise
  107. */
  108. public static function map($type = null, $className = null)
  109. {
  110. if ($type === null) {
  111. return self::$_types;
  112. }
  113. if (!is_string($type)) {
  114. self::$_types = $type;
  115. return;
  116. }
  117. if ($className === null) {
  118. return isset(self::$_types[$type]) ? self::$_types[$type] : null;
  119. }
  120. self::$_types[$type] = $className;
  121. }
  122. /**
  123. * Clears out all created instances and mapped types classes, useful for testing
  124. *
  125. * @return void
  126. */
  127. public static function clear()
  128. {
  129. self::$_types = [];
  130. self::$_builtTypes = [];
  131. }
  132. /**
  133. * Returns type identifier name for this object
  134. *
  135. * @return string
  136. */
  137. public function getName()
  138. {
  139. return $this->_name;
  140. }
  141. /**
  142. * Casts given value from a PHP type to one acceptable by database
  143. *
  144. * @param mixed $value value to be converted to database equivalent
  145. * @param Driver $driver object from which database preferences and configuration will be extracted
  146. * @return mixed
  147. */
  148. public function toDatabase($value, Driver $driver)
  149. {
  150. return $this->_basicTypeCast($value);
  151. }
  152. /**
  153. * Casts given value from a database type to PHP equivalent
  154. *
  155. * @param mixed $value value to be converted to PHP equivalent
  156. * @param Driver $driver object from which database preferences and configuration will be extracted
  157. * @return mixed
  158. */
  159. public function toPHP($value, Driver $driver)
  160. {
  161. return $this->_basicTypeCast($value);
  162. }
  163. /**
  164. * Checks whether this type is a basic one and can be converted using a callback
  165. * If it is, returns converted value
  166. *
  167. * @param mixed $value value to be converted to PHP equivalent
  168. * @return mixed
  169. */
  170. protected function _basicTypeCast($value)
  171. {
  172. if ($value === null) {
  173. return null;
  174. }
  175. if (!empty(self::$_basicTypes[$this->_name])) {
  176. $typeInfo = self::$_basicTypes[$this->_name];
  177. if (isset($typeInfo['callback'])) {
  178. return $typeInfo['callback']($value);
  179. }
  180. }
  181. return $value;
  182. }
  183. /**
  184. * Casts give value to Statement equivalent
  185. *
  186. * @param mixed $value value to be converted to PHP equivalent
  187. * @param Driver $driver object from which database preferences and configuration will be extracted
  188. * @return mixed
  189. */
  190. public function toStatement($value, Driver $driver)
  191. {
  192. if ($value === null) {
  193. return PDO::PARAM_NULL;
  194. }
  195. if (!empty(self::$_basicTypes[$this->_name])) {
  196. $typeInfo = self::$_basicTypes[$this->_name];
  197. return isset($typeInfo['pdo']) ? $typeInfo['pdo'] : PDO::PARAM_STR;
  198. }
  199. return PDO::PARAM_STR;
  200. }
  201. /**
  202. * Type converter for boolean values.
  203. *
  204. * Will convert string true/false into booleans.
  205. *
  206. * @param mixed $value The value to convert to a boolean.
  207. * @return bool
  208. */
  209. public static function boolval($value)
  210. {
  211. if (is_string($value) && !is_numeric($value)) {
  212. return strtolower($value) === 'true' ? true : false;
  213. }
  214. return !empty($value);
  215. }
  216. /**
  217. * Type converter for string values.
  218. *
  219. * Will convert values into strings
  220. *
  221. * @param mixed $value The value to convert to a string.
  222. * @return bool
  223. */
  224. public static function strval($value)
  225. {
  226. if (is_array($value)) {
  227. $value = '';
  228. }
  229. return strval($value);
  230. }
  231. /**
  232. * Generate a new primary key value for a given type.
  233. *
  234. * This method can be used by types to create new primary key values
  235. * when entities are inserted.
  236. *
  237. * @return mixed A new primary key value.
  238. * @see \Cake\Database\Type\UuidType
  239. */
  240. public function newId()
  241. {
  242. return null;
  243. }
  244. /**
  245. * Marshalls flat data into PHP objects.
  246. *
  247. * Most useful for converting request data into PHP objects
  248. * that make sense for the rest of the ORM/Database layers.
  249. *
  250. * @param mixed $value The value to convert.
  251. * @return mixed Converted value.
  252. */
  253. public function marshal($value)
  254. {
  255. return $this->_basicTypeCast($value);
  256. }
  257. }