ViewsSearchQuery.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Drupal\search;
  3. use Drupal\Core\Database\Query\Condition;
  4. /**
  5. * Extends the core SearchQuery to be able to gets its protected values.
  6. */
  7. class ViewsSearchQuery extends SearchQuery {
  8. /**
  9. * Returns the conditions property.
  10. *
  11. * @return array
  12. * The query conditions.
  13. */
  14. public function &conditions() {
  15. return $this->conditions;
  16. }
  17. /**
  18. * Returns the words property.
  19. *
  20. * @return array
  21. * The positive search keywords.
  22. */
  23. public function words() {
  24. return $this->words;
  25. }
  26. /**
  27. * Returns the simple property.
  28. *
  29. * @return bool
  30. * TRUE if it is a simple query, and FALSE if it is complicated (phrases
  31. * or LIKE).
  32. */
  33. public function simple() {
  34. return $this->simple;
  35. }
  36. /**
  37. * Returns the matches property.
  38. *
  39. * @return int
  40. * The number of matches needed.
  41. */
  42. public function matches() {
  43. return $this->matches;
  44. }
  45. /**
  46. * Executes and returns the protected parseSearchExpression method.
  47. */
  48. public function publicParseSearchExpression() {
  49. return $this->parseSearchExpression();
  50. }
  51. /**
  52. * Replaces the original condition with a custom one from views recursively.
  53. *
  54. * @param string $search
  55. * The searched value.
  56. * @param string $replace
  57. * The value which replaces the search value.
  58. * @param array $condition
  59. * The query conditions array in which the string is replaced. This is an
  60. * item from a \Drupal\Core\Database\Query\Condition::conditions array,
  61. * which must have a 'field' element.
  62. */
  63. public function conditionReplaceString($search, $replace, &$condition) {
  64. if ($condition['field'] instanceof Condition) {
  65. $conditions =& $condition['field']->conditions();
  66. foreach ($conditions as $key => &$subcondition) {
  67. if (is_numeric($key)) {
  68. // As conditions can be nested, the function has to be called
  69. // recursively.
  70. $this->conditionReplaceString($search, $replace, $subcondition);
  71. }
  72. }
  73. }
  74. else {
  75. $condition['field'] = str_replace($search, $replace, $condition['field']);
  76. }
  77. }
  78. }