BetweenExpression.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 {
  24. /**
  25. * The first value in the expression
  26. *
  27. * @var mixed
  28. */
  29. protected $_field;
  30. /**
  31. * The first value in the expression
  32. *
  33. * @var mixed
  34. */
  35. protected $_from;
  36. /**
  37. * The second value in the expression
  38. *
  39. * @var mixed
  40. */
  41. protected $_to;
  42. /**
  43. * The data type for the from and to arguments
  44. *
  45. * @var mixed
  46. */
  47. protected $_type;
  48. /**
  49. * Constructor
  50. *
  51. * @param mixed $value the value to use as the operand for the expression
  52. * @param int $mode either UnaryExpression::PREFIX or UnaryExpression::POSTFIX
  53. */
  54. public function __construct($field, $from, $to, $type = null) {
  55. $this->_field = $field;
  56. $this->_from = $from;
  57. $this->_to = $to;
  58. $this->_type = $type;
  59. }
  60. /**
  61. * Converts the expression to its string representation
  62. *
  63. * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  64. * @return string
  65. */
  66. public function sql(ValueBinder $generator) {
  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 ($field 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. foreach ([$this->_field, $this->_from, $this->_to] as $part) {
  90. if ($part instanceof ExpressionInterface) {
  91. $callable($this->_value);
  92. }
  93. }
  94. }
  95. /**
  96. * Registers a value in the placeholder generator and returns the generated placeholder
  97. *
  98. * @param mixed $value The value to bind
  99. * @param \Cake\Database\ValueBinder $generator The value binder to use
  100. * @param string $type The type of $value
  101. * @return string generated placeholder
  102. */
  103. protected function _bindValue($value, $generator, $type) {
  104. $placeholder = $generator->placeholder('c');
  105. $generator->bind($placeholder, $value, $type);
  106. return $placeholder;
  107. }
  108. }