BetweenConditionBuilder.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db\conditions;
  8. use yii\db\ExpressionBuilderInterface;
  9. use yii\db\ExpressionBuilderTrait;
  10. use yii\db\ExpressionInterface;
  11. /**
  12. * Class BetweenConditionBuilder builds objects of [[BetweenCondition]]
  13. *
  14. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  15. * @since 2.0.14
  16. */
  17. class BetweenConditionBuilder implements ExpressionBuilderInterface
  18. {
  19. use ExpressionBuilderTrait;
  20. /**
  21. * Method builds the raw SQL from the $expression that will not be additionally
  22. * escaped or quoted.
  23. *
  24. * @param ExpressionInterface|BetweenCondition $expression the expression to be built.
  25. * @param array $params the binding parameters.
  26. * @return string the raw SQL that will not be additionally escaped or quoted.
  27. */
  28. public function build(ExpressionInterface $expression, array &$params = [])
  29. {
  30. $operator = $expression->getOperator();
  31. $column = $expression->getColumn();
  32. if (strpos($column, '(') === false) {
  33. $column = $this->queryBuilder->db->quoteColumnName($column);
  34. }
  35. $phName1 = $this->createPlaceholder($expression->getIntervalStart(), $params);
  36. $phName2 = $this->createPlaceholder($expression->getIntervalEnd(), $params);
  37. return "$column $operator $phName1 AND $phName2";
  38. }
  39. /**
  40. * Attaches $value to $params array and returns placeholder.
  41. *
  42. * @param mixed $value
  43. * @param array $params passed by reference
  44. * @return string
  45. */
  46. protected function createPlaceholder($value, &$params)
  47. {
  48. if ($value instanceof ExpressionInterface) {
  49. return $this->queryBuilder->buildExpression($value, $params);
  50. }
  51. return $this->queryBuilder->bindParam($value, $params);
  52. }
  53. }