/src/Database/Expression/CaseExpression.php

https://github.com/LubosRemplik/cakephp · PHP · 249 lines · 127 code · 31 blank · 91 comment · 23 complexity · 726b0682355a5570838da4b0fdbb2115 MD5 · raw file

  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\Expression;
  16. use Cake\Database\ExpressionInterface;
  17. use Cake\Database\Type\ExpressionTypeCasterTrait;
  18. use Cake\Database\ValueBinder;
  19. /**
  20. * This class represents a SQL Case statement
  21. */
  22. class CaseExpression implements ExpressionInterface
  23. {
  24. use ExpressionTypeCasterTrait;
  25. /**
  26. * A list of strings or other expression objects that represent the conditions of
  27. * the case statement. For example one key of the array might look like "sum > :value"
  28. *
  29. * @var array
  30. */
  31. protected $_conditions = [];
  32. /**
  33. * Values that are associated with the conditions in the $_conditions array.
  34. * Each value represents the 'true' value for the condition with the corresponding key.
  35. *
  36. * @var array
  37. */
  38. protected $_values = [];
  39. /**
  40. * The `ELSE` value for the case statement. If null then no `ELSE` will be included.
  41. *
  42. * @var string|\Cake\Database\ExpressionInterface|array|null
  43. */
  44. protected $_elseValue;
  45. /**
  46. * Constructs the case expression
  47. *
  48. * @param array|\Cake\Database\ExpressionInterface $conditions The conditions to test. Must be a ExpressionInterface
  49. * instance, or an array of ExpressionInterface instances.
  50. * @param array|\Cake\Database\ExpressionInterface $values associative array of values to be associated with the conditions
  51. * passed in $conditions. If there are more $values than $conditions, the last $value is used as the `ELSE` value
  52. * @param array $types associative array of types to be associated with the values
  53. * passed in $values
  54. */
  55. public function __construct($conditions = [], $values = [], $types = [])
  56. {
  57. if (!empty($conditions)) {
  58. $this->add($conditions, $values, $types);
  59. }
  60. if (is_array($conditions) && is_array($values) && count($values) > count($conditions)) {
  61. end($values);
  62. $key = key($values);
  63. $this->elseValue($values[$key], isset($types[$key]) ? $types[$key] : null);
  64. }
  65. }
  66. /**
  67. * Adds one or more conditions and their respective true values to the case object.
  68. * Conditions must be a one dimensional array or a QueryExpression.
  69. * The trueValues must be a similar structure, but may contain a string value.
  70. *
  71. * @param array|\Cake\Database\ExpressionInterface $conditions Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.
  72. * @param array|\Cake\Database\ExpressionInterface $values associative array of values of each condition
  73. * @param array $types associative array of types to be associated with the values
  74. *
  75. * @return $this
  76. */
  77. public function add($conditions = [], $values = [], $types = [])
  78. {
  79. if (!is_array($conditions)) {
  80. $conditions = [$conditions];
  81. }
  82. if (!is_array($values)) {
  83. $values = [$values];
  84. }
  85. if (!is_array($types)) {
  86. $types = [$types];
  87. }
  88. $this->_addExpressions($conditions, $values, $types);
  89. return $this;
  90. }
  91. /**
  92. * Iterates over the passed in conditions and ensures that there is a matching true value for each.
  93. * If no matching true value, then it is defaulted to '1'.
  94. *
  95. * @param array|\Cake\Database\ExpressionInterface $conditions Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.
  96. * @param array|\Cake\Database\ExpressionInterface $values associative array of values of each condition
  97. * @param array $types associative array of types to be associated with the values
  98. *
  99. * @return void
  100. */
  101. protected function _addExpressions($conditions, $values, $types)
  102. {
  103. $rawValues = array_values($values);
  104. $keyValues = array_keys($values);
  105. foreach ($conditions as $k => $c) {
  106. $numericKey = is_numeric($k);
  107. if ($numericKey && empty($c)) {
  108. continue;
  109. }
  110. if (!$c instanceof ExpressionInterface) {
  111. continue;
  112. }
  113. $this->_conditions[] = $c;
  114. $value = isset($rawValues[$k]) ? $rawValues[$k] : 1;
  115. if ($value === 'literal') {
  116. $value = $keyValues[$k];
  117. $this->_values[] = $value;
  118. continue;
  119. }
  120. if ($value === 'identifier') {
  121. $value = new IdentifierExpression($keyValues[$k]);
  122. $this->_values[] = $value;
  123. continue;
  124. }
  125. $type = isset($types[$k]) ? $types[$k] : null;
  126. if ($type !== null && !$value instanceof ExpressionInterface) {
  127. $value = $this->_castToExpression($value, $type);
  128. }
  129. if ($value instanceof ExpressionInterface) {
  130. $this->_values[] = $value;
  131. continue;
  132. }
  133. $this->_values[] = ['value' => $value, 'type' => $type];
  134. }
  135. }
  136. /**
  137. * Sets the default value
  138. *
  139. * @param \Cake\Database\ExpressionInterface|string|array|null $value Value to set
  140. * @param string|null $type Type of value
  141. *
  142. * @return void
  143. */
  144. public function elseValue($value = null, $type = null)
  145. {
  146. if (is_array($value)) {
  147. end($value);
  148. $value = key($value);
  149. }
  150. if ($value !== null && !$value instanceof ExpressionInterface) {
  151. $value = $this->_castToExpression($value, $type);
  152. }
  153. if (!$value instanceof ExpressionInterface) {
  154. $value = ['value' => $value, 'type' => $type];
  155. }
  156. $this->_elseValue = $value;
  157. }
  158. /**
  159. * Compiles the relevant parts into sql
  160. *
  161. * @param array|string|\Cake\Database\ExpressionInterface $part The part to compile
  162. * @param \Cake\Database\ValueBinder $generator Sql generator
  163. *
  164. * @return string
  165. */
  166. protected function _compile($part, ValueBinder $generator)
  167. {
  168. if ($part instanceof ExpressionInterface) {
  169. $part = $part->sql($generator);
  170. } elseif (is_array($part)) {
  171. $placeholder = $generator->placeholder('param');
  172. $generator->bind($placeholder, $part['value'], $part['type']);
  173. $part = $placeholder;
  174. }
  175. return $part;
  176. }
  177. /**
  178. * Converts the Node into a SQL string fragment.
  179. *
  180. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  181. *
  182. * @return string
  183. */
  184. public function sql(ValueBinder $generator)
  185. {
  186. $parts = [];
  187. $parts[] = 'CASE';
  188. foreach ($this->_conditions as $k => $part) {
  189. $value = $this->_values[$k];
  190. $parts[] = 'WHEN ' . $this->_compile($part, $generator) . ' THEN ' . $this->_compile($value, $generator);
  191. }
  192. if ($this->_elseValue !== null) {
  193. $parts[] = 'ELSE';
  194. $parts[] = $this->_compile($this->_elseValue, $generator);
  195. }
  196. $parts[] = 'END';
  197. return implode(' ', $parts);
  198. }
  199. /**
  200. * {@inheritDoc}
  201. *
  202. */
  203. public function traverse(callable $visitor)
  204. {
  205. foreach (['_conditions', '_values'] as $part) {
  206. foreach ($this->{$part} as $c) {
  207. if ($c instanceof ExpressionInterface) {
  208. $visitor($c);
  209. $c->traverse($visitor);
  210. }
  211. }
  212. }
  213. if ($this->_elseValue instanceof ExpressionInterface) {
  214. $visitor($this->_elseValue);
  215. $this->_elseValue->traverse($visitor);
  216. }
  217. }
  218. }