SpanFirst.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * SpanFirst query.
  6. *
  7. * @author Alessandro Chitolina <alekitto@gmail.com>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html
  10. */
  11. class SpanFirst extends AbstractSpanQuery
  12. {
  13. /**
  14. * Constructs a SpanFirst query object.
  15. *
  16. * @param \Elastica\Query\AbstractQuery|array $match OPTIONAL
  17. * @param int $end OPTIONAL
  18. */
  19. public function __construct($match = null, $end = null)
  20. {
  21. if (!is_null($match)) {
  22. $this->setMatch($match);
  23. }
  24. if (!is_null($match)) {
  25. $this->setEnd($end);
  26. }
  27. }
  28. /**
  29. * Set the query to be wrapped into the span multi query.
  30. *
  31. * @param \Elastica\Query\AbstractSpanQuery|array $args Matching query
  32. *
  33. * @throws \Elastica\Exception\InvalidException If not valid query
  34. *
  35. * @return $this
  36. */
  37. public function setMatch($args)
  38. {
  39. return $this->_setQuery('match', $args);
  40. }
  41. /**
  42. * Set the maximum end position for the match query.
  43. *
  44. * @param int $end
  45. *
  46. * @return $this
  47. */
  48. public function setEnd($end)
  49. {
  50. $this->setParam('end', $end);
  51. return $this;
  52. }
  53. /**
  54. * Sets a query to the current object.
  55. *
  56. * @param string $type Query type
  57. * @param \Elastica\Query\AbstractQuery|array $args Query
  58. *
  59. * @throws \Elastica\Exception\InvalidException If not valid query
  60. *
  61. * @return $this
  62. */
  63. protected function _setQuery($type, $args)
  64. {
  65. if (!is_array($args) && !($args instanceof AbstractSpanQuery)) {
  66. throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractSpanQuery');
  67. }
  68. return $this->setParam($type, $args);
  69. }
  70. }