Type.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * List of supported database types. A human readable
  25. * identifier is used as key and a complete namespaced class name as value
  26. * representing the class that will do actual type conversions.
  27. *
  28. * @var array
  29. */
  30. protected static $_types = [
  31. 'biginteger' => 'Cake\Database\Type\IntegerType',
  32. 'binary' => 'Cake\Database\Type\BinaryType',
  33. 'date' => 'Cake\Database\Type\DateType',
  34. 'float' => 'Cake\Database\Type\FloatType',
  35. 'decimal' => 'Cake\Database\Type\FloatType',
  36. 'integer' => 'Cake\Database\Type\IntegerType',
  37. 'time' => 'Cake\Database\Type\TimeType',
  38. 'datetime' => 'Cake\Database\Type\DateTimeType',
  39. 'timestamp' => 'Cake\Database\Type\DateTimeType',
  40. 'uuid' => 'Cake\Database\Type\UuidType',
  41. ];
  42. /**
  43. * List of basic type mappings, used to avoid having to instantiate a class
  44. * for doing conversion on these
  45. *
  46. * @var array
  47. */
  48. protected static $_basicTypes = [
  49. 'string' => ['callback' => 'strval'],
  50. 'text' => ['callback' => 'strval'],
  51. 'boolean' => [
  52. 'callback' => ['\Cake\Database\Type', 'boolval'],
  53. 'pdo' => PDO::PARAM_BOOL
  54. ],
  55. ];
  56. /**
  57. * Contains a map of type object instances to be reused if needed
  58. *
  59. * @var array
  60. */
  61. protected static $_builtTypes = [];
  62. /**
  63. * Identifier name for this type
  64. *
  65. * @var string
  66. */
  67. protected $_name = null;
  68. /**
  69. * Constructor
  70. *
  71. * @param string $name The name identifying this type
  72. */
  73. public function __construct($name = null) {
  74. $this->_name = $name;
  75. }
  76. /**
  77. * Returns a Type object capable of converting a type identified by $name
  78. *
  79. * @param string $name type identifier
  80. * @throws \InvalidArgumentException If type identifier is unknown
  81. * @return Type
  82. */
  83. public static function build($name) {
  84. if (isset(static::$_builtTypes[$name])) {
  85. return static::$_builtTypes[$name];
  86. }
  87. if (isset(static::$_basicTypes[$name])) {
  88. return static::$_builtTypes[$name] = new static($name);
  89. }
  90. if (!isset(static::$_types[$name])) {
  91. throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $name));
  92. }
  93. return static::$_builtTypes[$name] = new static::$_types[$name]($name);
  94. }
  95. /**
  96. * Registers a new type identifier and maps it to a fully namespaced classname,
  97. * If called with no arguments it will return current types map array
  98. * If $className is omitted it will return mapped class for $type
  99. *
  100. * @param string|array $type if string name of type to map, if array list of arrays to be mapped
  101. * @param string $className The classname to register.
  102. * @return array|string|null if $type is null then array with current map, if $className is null string
  103. * configured class name for give $type, null otherwise
  104. */
  105. public static function map($type = null, $className = null) {
  106. if ($type === null) {
  107. return self::$_types;
  108. }
  109. if (!is_string($type)) {
  110. self::$_types = $type;
  111. return;
  112. }
  113. if ($className === null) {
  114. return isset(self::$_types[$type]) ? self::$_types[$type] : null;
  115. }
  116. self::$_types[$type] = $className;
  117. }
  118. /**
  119. * Clears out all created instances and mapped types classes, useful for testing
  120. *
  121. * @return void
  122. */
  123. public static function clear() {
  124. self::$_types = [];
  125. self::$_builtTypes = [];
  126. }
  127. /**
  128. * Returns type identifier name for this object
  129. *
  130. * @return string
  131. */
  132. public function getName() {
  133. return $this->_name;
  134. }
  135. /**
  136. * Casts given value from a PHP type to one acceptable by database
  137. *
  138. * @param mixed $value value to be converted to database equivalent
  139. * @param Driver $driver object from which database preferences and configuration will be extracted
  140. * @return mixed
  141. */
  142. public function toDatabase($value, Driver $driver) {
  143. return $this->_basicTypeCast($value, $driver);
  144. }
  145. /**
  146. * Casts given value from a database type to PHP equivalent
  147. *
  148. * @param mixed $value value to be converted to PHP equivalent
  149. * @param Driver $driver object from which database preferences and configuration will be extracted
  150. * @return mixed
  151. */
  152. public function toPHP($value, Driver $driver) {
  153. return $this->_basicTypeCast($value, $driver);
  154. }
  155. /**
  156. * Checks whether this type is a basic one and can be converted using a callback
  157. * If it is, returns converted value
  158. *
  159. * @param mixed $value value to be converted to PHP equivalent
  160. * @param Driver $driver object from which database preferences and configuration will be extracted
  161. * @return mixed
  162. */
  163. protected function _basicTypeCast($value, Driver $driver) {
  164. if ($value === null) {
  165. return null;
  166. }
  167. if (!empty(self::$_basicTypes[$this->_name])) {
  168. $typeInfo = self::$_basicTypes[$this->_name];
  169. if (isset($typeInfo['callback'])) {
  170. return $typeInfo['callback']($value);
  171. }
  172. }
  173. return $value;
  174. }
  175. /**
  176. * Casts give value to Statement 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 toStatement($value, Driver $driver) {
  183. if ($value === null) {
  184. return PDO::PARAM_NULL;
  185. }
  186. if (!empty(self::$_basicTypes[$this->_name])) {
  187. $typeInfo = self::$_basicTypes[$this->_name];
  188. return isset($typeInfo['pdo']) ? $typeInfo['pdo'] : PDO::PARAM_STR;
  189. }
  190. return PDO::PARAM_STR;
  191. }
  192. /**
  193. * Type converter for boolean values.
  194. *
  195. * Will convert string true/false into booleans.
  196. *
  197. * @param mixed $value The value to convert to a boolean.
  198. * @return bool
  199. */
  200. public static function boolval($value) {
  201. if (is_string($value) && !is_numeric($value)) {
  202. return strtolower($value) === 'true' ? true : false;
  203. }
  204. return !empty($value);
  205. }
  206. /**
  207. * Generate a new primary key value for a given type.
  208. *
  209. * This method can be used by types to create new primary key values
  210. * when entities are inserted.
  211. *
  212. * @return mixed A new primary key value.
  213. * @see \Cake\Database\Type\UuidType
  214. */
  215. public function newId() {
  216. return null;
  217. }
  218. /**
  219. * Marshalls flat data into PHP objects.
  220. *
  221. * Most useful for converting request data into PHP objects
  222. * that make sense for the rest of the ORM/Database layers.
  223. *
  224. * @param mixed $value The value to convert.
  225. * @return mixed Converted value.
  226. */
  227. public function marshal($value) {
  228. return $value;
  229. }
  230. }