BetweenExpression.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * An expression object that represents a SQL BETWEEN snippet
  21. */
  22. class BetweenExpression implements ExpressionInterface, FieldInterface
  23. {
  24. use ExpressionTypeCasterTrait;
  25. use FieldTrait;
  26. /**
  27. * The first value in the expression
  28. *
  29. * @var mixed
  30. */
  31. protected $_from;
  32. /**
  33. * The second value in the expression
  34. *
  35. * @var mixed
  36. */
  37. protected $_to;
  38. /**
  39. * The data type for the from and to arguments
  40. *
  41. * @var mixed
  42. */
  43. protected $_type;
  44. /**
  45. * Constructor
  46. *
  47. * @param string|\Cake\Database\ExpressionInterface $field The field name to compare for values in between the range.
  48. * @param mixed $from The initial value of the range.
  49. * @param mixed $to The ending value in the comparison range.
  50. * @param string|null $type The data type name to bind the values with.
  51. */
  52. public function __construct($field, $from, $to, $type = null)
  53. {
  54. if ($type !== null) {
  55. $from = $this->_castToExpression($from, $type);
  56. $to = $this->_castToExpression($to, $type);
  57. }
  58. $this->_field = $field;
  59. $this->_from = $from;
  60. $this->_to = $to;
  61. $this->_type = $type;
  62. }
  63. /**
  64. * Converts the expression to its string representation
  65. *
  66. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  67. * @return string
  68. */
  69. public function sql(ValueBinder $generator)
  70. {
  71. $parts = [
  72. 'from' => $this->_from,
  73. 'to' => $this->_to
  74. ];
  75. $field = $this->_field;
  76. if ($field instanceof ExpressionInterface) {
  77. $field = $field->sql($generator);
  78. }
  79. foreach ($parts as $name => $part) {
  80. if ($part instanceof ExpressionInterface) {
  81. $parts[$name] = $part->sql($generator);
  82. continue;
  83. }
  84. $parts[$name] = $this->_bindValue($part, $generator, $this->_type);
  85. }
  86. return sprintf('%s BETWEEN %s AND %s', $field, $parts['from'], $parts['to']);
  87. }
  88. /**
  89. * {@inheritDoc}
  90. *
  91. */
  92. public function traverse(callable $callable)
  93. {
  94. foreach ([$this->_field, $this->_from, $this->_to] as $part) {
  95. if ($part instanceof ExpressionInterface) {
  96. $callable($part);
  97. }
  98. }
  99. }
  100. /**
  101. * Registers a value in the placeholder generator and returns the generated placeholder
  102. *
  103. * @param mixed $value The value to bind
  104. * @param \Cake\Database\ValueBinder $generator The value binder to use
  105. * @param string $type The type of $value
  106. * @return string generated placeholder
  107. */
  108. protected function _bindValue($value, $generator, $type)
  109. {
  110. $placeholder = $generator->placeholder('c');
  111. $generator->bind($placeholder, $value, $type);
  112. return $placeholder;
  113. }
  114. /**
  115. * Do a deep clone of this expression.
  116. *
  117. * @return void
  118. */
  119. public function __clone()
  120. {
  121. foreach (['_field', '_from', '_to'] as $part) {
  122. if ($this->{$part} instanceof ExpressionInterface) {
  123. $this->{$part} = clone $this->{$part};
  124. }
  125. }
  126. }
  127. }