ConjunctionConditionBuilder.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ConjunctionConditionBuilder builds objects of abstract class [[ConjunctionCondition]]
  13. *
  14. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  15. * @since 2.0.14
  16. */
  17. class ConjunctionConditionBuilder 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|ConjunctionCondition $condition 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 $condition, array &$params = [])
  29. {
  30. $parts = $this->buildExpressionsFrom($condition, $params);
  31. if (empty($parts)) {
  32. return '';
  33. }
  34. if (count($parts) === 1) {
  35. return reset($parts);
  36. }
  37. return '(' . implode(") {$condition->getOperator()} (", $parts) . ')';
  38. }
  39. /**
  40. * Builds expressions, that are stored in $condition
  41. *
  42. * @param ExpressionInterface|ConjunctionCondition $condition the expression to be built.
  43. * @param array $params the binding parameters.
  44. * @return string[]
  45. */
  46. private function buildExpressionsFrom(ExpressionInterface $condition, &$params = [])
  47. {
  48. $parts = [];
  49. foreach ($condition->getExpressions() as $condition) {
  50. if (is_array($condition)) {
  51. $condition = $this->queryBuilder->buildCondition($condition, $params);
  52. }
  53. if ($condition instanceof ExpressionInterface) {
  54. $condition = $this->queryBuilder->buildExpression($condition, $params);
  55. }
  56. if ($condition !== '') {
  57. $parts[] = $condition;
  58. }
  59. }
  60. return $parts;
  61. }
  62. }