BetweenExpression.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Expression;
  16. use Cake\Database\ExpressionInterface;
  17. use Cake\Database\ValueBinder;
  18. /**
  19. * An expression object that represents a SQL BETWEEN snippet
  20. *
  21. * @internal
  22. */
  23. class BetweenExpression implements ExpressionInterface, FieldInterface
  24. {
  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 mixed $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 $type The data type name to bind the values with.
  51. */
  52. public function __construct($field, $from, $to, $type = null)
  53. {
  54. $this->_field = $field;
  55. $this->_from = $from;
  56. $this->_to = $to;
  57. $this->_type = $type;
  58. }
  59. /**
  60. * Converts the expression to its string representation
  61. *
  62. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  63. * @return string
  64. */
  65. public function sql(ValueBinder $generator)
  66. {
  67. $parts = [
  68. 'from' => $this->_from,
  69. 'to' => $this->_to
  70. ];
  71. $field = $this->_field;
  72. if ($field instanceof ExpressionInterface) {
  73. $field = $field->sql($generator);
  74. }
  75. foreach ($parts as $name => $part) {
  76. if ($part instanceof ExpressionInterface) {
  77. $parts[$name] = $part->sql($generator);
  78. continue;
  79. }
  80. $parts[$name] = $this->_bindValue($part, $generator, $this->_type);
  81. }
  82. return sprintf('%s BETWEEN %s AND %s', $field, $parts['from'], $parts['to']);
  83. }
  84. /**
  85. * {@inheritDoc}
  86. *
  87. */
  88. public function traverse(callable $callable)
  89. {
  90. foreach ([$this->_field, $this->_from, $this->_to] as $part) {
  91. if ($part instanceof ExpressionInterface) {
  92. $callable($part);
  93. }
  94. }
  95. }
  96. /**
  97. * Registers a value in the placeholder generator and returns the generated placeholder
  98. *
  99. * @param mixed $value The value to bind
  100. * @param \Cake\Database\ValueBinder $generator The value binder to use
  101. * @param string $type The type of $value
  102. * @return string generated placeholder
  103. */
  104. protected function _bindValue($value, $generator, $type)
  105. {
  106. $placeholder = $generator->placeholder('c');
  107. $generator->bind($placeholder, $value, $type);
  108. return $placeholder;
  109. }
  110. /**
  111. * Do a deep clone of this expression.
  112. *
  113. * @return void
  114. */
  115. public function __clone()
  116. {
  117. foreach (['_field', '_from', '_to'] as $part) {
  118. if ($this->{$part} instanceof ExpressionInterface) {
  119. $this->{$part} = clone $this->{$part};
  120. }
  121. }
  122. }
  123. }