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