AbstractSuggest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Elastica\Suggest;
  3. use Elastica\Exception\InvalidException;
  4. use Elastica\NameableInterface;
  5. use Elastica\Param;
  6. /**
  7. * Class AbstractSuggestion.
  8. */
  9. abstract class AbstractSuggest extends Param implements NameableInterface
  10. {
  11. /**
  12. * @var string the name of this suggestion
  13. */
  14. protected $_name;
  15. /**
  16. * @param string $name
  17. * @param string $field
  18. */
  19. public function __construct($name, $field)
  20. {
  21. $this->setName($name);
  22. $this->setField($field);
  23. }
  24. /**
  25. * Suggest text must be set either globally or per suggestion.
  26. *
  27. * @param string $text
  28. *
  29. * @return $this
  30. */
  31. public function setText($text)
  32. {
  33. return $this->_setRawParam('text', $text);
  34. }
  35. /**
  36. * Suggest prefix must be set either globally or per suggestion.
  37. *
  38. * @param string $text
  39. *
  40. * @return $this
  41. */
  42. public function setPrefix($prefix)
  43. {
  44. return $this->_setRawParam('prefix', $prefix);
  45. }
  46. /**
  47. * Suggest regex must be set either globally or per suggestion.
  48. *
  49. * @param string $text
  50. *
  51. * @return $this
  52. */
  53. public function setRegex($regex)
  54. {
  55. return $this->_setRawParam('regex', $regex);
  56. }
  57. /**
  58. * Expects one of the next params: max_determinized_states - defaults to 10000,
  59. * flags are ALL (default), ANYSTRING, COMPLEMENT, EMPTY, INTERSECTION, INTERVAL, or NONE.
  60. *
  61. * @param array $value
  62. *
  63. * @return $this
  64. */
  65. public function setRegexOptions(array $value)
  66. {
  67. return $this->setParam('regex', $value);
  68. }
  69. /**
  70. * @param string $field
  71. *
  72. * @return $this
  73. */
  74. public function setField($field)
  75. {
  76. return $this->setParam('field', $field);
  77. }
  78. /**
  79. * @param int $size
  80. *
  81. * @return $this
  82. */
  83. public function setSize($size)
  84. {
  85. return $this->setParam('size', $size);
  86. }
  87. /**
  88. * @param int $size maximum number of suggestions to be retrieved from each shard
  89. *
  90. * @return $this
  91. */
  92. public function setShardSize($size)
  93. {
  94. return $this->setParam('shard_size', $size);
  95. }
  96. /**
  97. * Sets the name of the suggest. It is automatically set by
  98. * the constructor.
  99. *
  100. * @param string $name The name of the suggest
  101. *
  102. * @throws \Elastica\Exception\InvalidException If name is empty
  103. *
  104. * @return $this
  105. */
  106. public function setName($name)
  107. {
  108. if (empty($name)) {
  109. throw new InvalidException('Suggest name has to be set');
  110. }
  111. $this->_name = $name;
  112. return $this;
  113. }
  114. /**
  115. * Retrieve the name of this suggestion.
  116. *
  117. * @return string
  118. */
  119. public function getName()
  120. {
  121. return $this->_name;
  122. }
  123. }