SpanNear.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * SpanNear query.
  6. *
  7. * @author Marek Hernik <marek.hernik@gmail.com>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html
  10. */
  11. class SpanNear extends AbstractSpanQuery
  12. {
  13. /**
  14. * Constructs a SpanNear query object.
  15. *
  16. * @param AbstractSpanQuery[] $clauses OPTIONAL
  17. * @param int $slop OPTIONAL maximum proximity
  18. * @param bool $inOrder OPTIONAL true if order of searched clauses is important
  19. */
  20. public function __construct($clauses = [], $slop = 1, $inOrder = false)
  21. {
  22. if (!empty($clauses)) {
  23. foreach ($clauses as $clause) {
  24. if (!is_subclass_of($clause, AbstractSpanQuery::class)) {
  25. throw new InvalidException(
  26. 'Invalid parameter. Has to be array or instance of Elastica\Query\SpanQuery'
  27. );
  28. }
  29. }
  30. }
  31. $this->setParams(['clauses' => $clauses]);
  32. $this->setSlop($slop);
  33. $this->setInOrder($inOrder);
  34. }
  35. /**
  36. * @param int $slop
  37. *
  38. * @return $this
  39. */
  40. public function setSlop($slop)
  41. {
  42. return $this->setParam('slop', $slop);
  43. }
  44. /**
  45. * @param bool $inOrder
  46. *
  47. * @return $this
  48. */
  49. public function setInOrder($inOrder)
  50. {
  51. return $this->setParam('in_order', $inOrder);
  52. }
  53. /**
  54. * Add clause part to query.
  55. *
  56. * @param AbstractSpanQuery $clause
  57. *
  58. * @throws InvalidException If not valid query
  59. *
  60. * @return $this
  61. */
  62. public function addClause($clause)
  63. {
  64. if (!is_subclass_of($clause, AbstractSpanQuery::class)) {
  65. throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\SpanQuery');
  66. }
  67. return $this->addParam('clauses', $clause);
  68. }
  69. }