LikeConditionBuilder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\base\InvalidArgumentException;
  9. use yii\db\ExpressionBuilderInterface;
  10. use yii\db\ExpressionBuilderTrait;
  11. use yii\db\ExpressionInterface;
  12. /**
  13. * Class LikeConditionBuilder builds objects of [[LikeCondition]]
  14. *
  15. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  16. * @since 2.0.14
  17. */
  18. class LikeConditionBuilder implements ExpressionBuilderInterface
  19. {
  20. use ExpressionBuilderTrait;
  21. /**
  22. * @var array map of chars to their replacements in LIKE conditions.
  23. * By default it's configured to escape `%`, `_` and `\` with `\`.
  24. */
  25. protected $escapingReplacements = [
  26. '%' => '\%',
  27. '_' => '\_',
  28. '\\' => '\\\\',
  29. ];
  30. /**
  31. * @var string|null character used to escape special characters in LIKE conditions.
  32. * By default it's assumed to be `\`.
  33. */
  34. protected $escapeCharacter;
  35. /**
  36. * Method builds the raw SQL from the $expression that will not be additionally
  37. * escaped or quoted.
  38. *
  39. * @param ExpressionInterface|LikeCondition $expression the expression to be built.
  40. * @param array $params the binding parameters.
  41. * @return string the raw SQL that will not be additionally escaped or quoted.
  42. */
  43. public function build(ExpressionInterface $expression, array &$params = [])
  44. {
  45. $operator = $expression->getOperator();
  46. $column = $expression->getColumn();
  47. $values = $expression->getValue();
  48. $escape = $expression->getEscapingReplacements();
  49. if ($escape === null || $escape === []) {
  50. $escape = $this->escapingReplacements;
  51. }
  52. list($andor, $not, $operator) = $this->parseOperator($operator);
  53. if (!is_array($values)) {
  54. $values = [$values];
  55. }
  56. if (empty($values)) {
  57. return $not ? '' : '0=1';
  58. }
  59. if (strpos($column, '(') === false) {
  60. $column = $this->queryBuilder->db->quoteColumnName($column);
  61. }
  62. $escapeSql = $this->getEscapeSql();
  63. $parts = [];
  64. foreach ($values as $value) {
  65. if ($value instanceof ExpressionInterface) {
  66. $phName = $this->queryBuilder->buildExpression($value, $params);
  67. } else {
  68. $phName = $this->queryBuilder->bindParam(empty($escape) ? $value : ('%' . strtr($value, $escape) . '%'), $params);
  69. }
  70. $parts[] = "{$column} {$operator} {$phName}{$escapeSql}";
  71. }
  72. return implode($andor, $parts);
  73. }
  74. /**
  75. * @return string
  76. */
  77. private function getEscapeSql()
  78. {
  79. if ($this->escapeCharacter !== null) {
  80. return " ESCAPE '{$this->escapeCharacter}'";
  81. }
  82. return '';
  83. }
  84. /**
  85. * @param string $operator
  86. * @return array
  87. */
  88. protected function parseOperator($operator)
  89. {
  90. if (!preg_match('/^(AND |OR |)(((NOT |))I?LIKE)/', $operator, $matches)) {
  91. throw new InvalidArgumentException("Invalid operator '$operator'.");
  92. }
  93. $andor = ' ' . (!empty($matches[1]) ? $matches[1] : 'AND ');
  94. $not = !empty($matches[3]);
  95. $operator = $matches[2];
  96. return [$andor, $not, $operator];
  97. }
  98. }