TypeMap.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  17. * Implements default and single-use mappings for columns to their associated types
  18. */
  19. class TypeMap
  20. {
  21. /**
  22. * Associative array with the default fields and the related types this query might contain.
  23. *
  24. * Used to avoid repetition when calling multiple functions inside this class that
  25. * may require a custom type for a specific field.
  26. *
  27. * @var array
  28. */
  29. protected $_defaults;
  30. /**
  31. * Associative array with the fields and the related types that override defaults this query might contain
  32. *
  33. * Used to avoid repetition when calling multiple functions inside this class that
  34. * may require a custom type for a specific field.
  35. *
  36. * @var array
  37. */
  38. protected $_types = [];
  39. /**
  40. * Creates an instance with the given defaults
  41. *
  42. * @param array $defaults The defaults to use.
  43. */
  44. public function __construct(array $defaults = [])
  45. {
  46. $this->setDefaults($defaults);
  47. }
  48. /**
  49. * Configures a map of default fields and their associated types to be
  50. * used as the default list of types for every function in this class
  51. * with a $types param. Useful to avoid repetition when calling the same
  52. * functions using the same fields and types.
  53. *
  54. * ### Example
  55. *
  56. * ```
  57. * $query->setDefaults(['created' => 'datetime', 'is_visible' => 'boolean']);
  58. * ```
  59. *
  60. * This method will replace all the existing type maps with the ones provided.
  61. *
  62. * @param array $defaults Associative array where keys are field names and values
  63. * are the correspondent type.
  64. * @return $this
  65. */
  66. public function setDefaults(array $defaults)
  67. {
  68. $this->_defaults = $defaults;
  69. return $this;
  70. }
  71. /**
  72. * Returns the currently configured types.
  73. *
  74. * @return array
  75. */
  76. public function getDefaults()
  77. {
  78. return $this->_defaults;
  79. }
  80. /**
  81. * Configures a map of default fields and their associated types to be
  82. * used as the default list of types for every function in this class
  83. * with a $types param. Useful to avoid repetition when calling the same
  84. * functions using the same fields and types.
  85. *
  86. * If called with no arguments it will return the currently configured types.
  87. *
  88. * ### Example
  89. *
  90. * ```
  91. * $query->defaults(['created' => 'datetime', 'is_visible' => 'boolean']);
  92. * ```
  93. *
  94. * This method will replace all the existing type maps with the ones provided.
  95. *
  96. * @deprecated 3.4.0 Use setDefaults()/getDefaults() instead.
  97. * @param array|null $defaults associative array where keys are field names and values
  98. * are the correspondent type.
  99. * @return $this|array
  100. */
  101. public function defaults(array $defaults = null)
  102. {
  103. deprecationWarning(
  104. 'TypeMap::defaults() is deprecated. ' .
  105. 'Use TypeMap::setDefaults()/getDefaults() instead.'
  106. );
  107. if ($defaults !== null) {
  108. return $this->setDefaults($defaults);
  109. }
  110. return $this->getDefaults();
  111. }
  112. /**
  113. * Add additional default types into the type map.
  114. *
  115. * If a key already exists it will not be overwritten.
  116. *
  117. * @param array $types The additional types to add.
  118. * @return void
  119. */
  120. public function addDefaults(array $types)
  121. {
  122. $this->_defaults += $types;
  123. }
  124. /**
  125. * Sets a map of fields and their associated types for single-use.
  126. *
  127. * ### Example
  128. *
  129. * ```
  130. * $query->setTypes(['created' => 'time']);
  131. * ```
  132. *
  133. * This method will replace all the existing type maps with the ones provided.
  134. *
  135. * @param array $types Associative array where keys are field names and values
  136. * are the correspondent type.
  137. * @return $this
  138. */
  139. public function setTypes(array $types)
  140. {
  141. $this->_types = $types;
  142. return $this;
  143. }
  144. /**
  145. * Gets a map of fields and their associated types for single-use.
  146. *
  147. * @return array
  148. */
  149. public function getTypes()
  150. {
  151. return $this->_types;
  152. }
  153. /**
  154. * Sets a map of fields and their associated types for single-use.
  155. *
  156. * If called with no arguments it will return the currently configured types.
  157. *
  158. * ### Example
  159. *
  160. * ```
  161. * $query->types(['created' => 'time']);
  162. * ```
  163. *
  164. * This method will replace all the existing type maps with the ones provided.
  165. *
  166. * @deprecated 3.4.0 Use setTypes()/getTypes() instead.
  167. * @param array|null $types associative array where keys are field names and values
  168. * are the correspondent type.
  169. * @return $this|array
  170. */
  171. public function types(array $types = null)
  172. {
  173. deprecationWarning(
  174. 'TypeMap::types() is deprecated. ' .
  175. 'Use TypeMap::setTypes()/getTypes() instead.'
  176. );
  177. if ($types !== null) {
  178. return $this->setTypes($types);
  179. }
  180. return $this->getTypes();
  181. }
  182. /**
  183. * Returns the type of the given column. If there is no single use type is configured,
  184. * the column type will be looked for inside the default mapping. If neither exist,
  185. * null will be returned.
  186. *
  187. * @param string $column The type for a given column
  188. * @return null|string
  189. */
  190. public function type($column)
  191. {
  192. if (isset($this->_types[$column])) {
  193. return $this->_types[$column];
  194. }
  195. if (isset($this->_defaults[$column])) {
  196. return $this->_defaults[$column];
  197. }
  198. return null;
  199. }
  200. /**
  201. * Returns an array of all types mapped types
  202. *
  203. * @return array
  204. */
  205. public function toArray()
  206. {
  207. return $this->_types + $this->_defaults;
  208. }
  209. }