TypeMap.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  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->defaults($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. * If called with no arguments it will return the currently configured types.
  55. *
  56. * ### Example
  57. *
  58. * ```
  59. * $query->defaults(['created' => 'datetime', 'is_visible' => 'boolean']);
  60. * ```
  61. *
  62. * This method will replace all the existing type maps with the ones provided.
  63. *
  64. * @param array $defaults associative array where keys are field names and values
  65. * are the correspondent type.
  66. * @return $this|array
  67. */
  68. public function defaults(array $defaults = null)
  69. {
  70. if ($defaults === null) {
  71. return $this->_defaults;
  72. }
  73. $this->_defaults = $defaults;
  74. return $this;
  75. }
  76. /**
  77. * Add additional default types into the type map.
  78. *
  79. * If a key already exists it will not be overwritten.
  80. *
  81. * @param array $types The additional types to add.
  82. * @return void
  83. */
  84. public function addDefaults(array $types)
  85. {
  86. $this->_defaults = $this->_defaults + $types;
  87. }
  88. /**
  89. * Sets a map of fields and their associated types for single-use.
  90. *
  91. * If called with no arguments it will return the currently configured types.
  92. *
  93. * ### Example
  94. *
  95. * ```
  96. * $query->types(['created' => 'time']);
  97. * ```
  98. *
  99. * This method will replace all the existing type maps with the ones provided.
  100. *
  101. * @param array $types associative array where keys are field names and values
  102. * are the correspondent type.
  103. * @return $this|array
  104. */
  105. public function types(array $types = null)
  106. {
  107. if ($types === null) {
  108. return $this->_types;
  109. }
  110. $this->_types = $types;
  111. return $this;
  112. }
  113. /**
  114. * Returns the type of the given column. If there is no single use type is configured,
  115. * the column type will be looked for inside the default mapping. If neither exist,
  116. * null will be returned.
  117. *
  118. * @param string $column The type for a given column
  119. * @return null|string
  120. */
  121. public function type($column)
  122. {
  123. if (isset($this->_types[$column])) {
  124. return $this->_types[$column];
  125. }
  126. if (isset($this->_defaults[$column])) {
  127. return $this->_defaults[$column];
  128. }
  129. return null;
  130. }
  131. /**
  132. * Returns an array of all types mapped types
  133. *
  134. * @return array
  135. */
  136. public function toArray()
  137. {
  138. return $this->_types + $this->_defaults;
  139. }
  140. }