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    public function __construct(string $name, string $field)
20    {
21        $this->setName($name);
22        $this->setField($field);
23    }
24
25    /**
26     * Suggest text must be set either globally or per suggestion.
27     *
28     * @return $this
29     */
30    public function setText(string $text): self
31    {
32        return $this->_setRawParam('text', $text);
33    }
34
35    /**
36     * Suggest prefix must be set either globally or per suggestion.
37     *
38     * @return $this
39     */
40    public function setPrefix(string $prefix): self
41    {
42        return $this->_setRawParam('prefix', $prefix);
43    }
44
45    /**
46     * Suggest regex must be set either globally or per suggestion.
47     *
48     * @return $this
49     */
50    public function setRegex(string $regex): self
51    {
52        return $this->_setRawParam('regex', $regex);
53    }
54
55    /**
56     * Expects one of the next params: max_determinized_states - defaults to 10000,
57     * flags are ALL (default), ANYSTRING, COMPLEMENT, EMPTY, INTERSECTION, INTERVAL, or NONE.
58     *
59     * @phpstan-param array{
60     *     max_determinized_states?: int,
61     *     flags: 'ALL'|'ANYSTRING'|'COMPLEMENT'|'EMPTY'|'INTERSECTION'|'INTERVAL'|'NONE'
62     * } $value
63     *
64     * @return $this
65     */
66    public function setRegexOptions(array $value): self
67    {
68        return $this->setParam('regex', $value);
69    }
70
71    /**
72     * @return $this
73     */
74    public function setField(string $field): self
75    {
76        return $this->setParam('field', $field);
77    }
78
79    /**
80     * @return $this
81     */
82    public function setSize(int $size): self
83    {
84        return $this->setParam('size', $size);
85    }
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(int $size): self
93    {
94        return $this->setParam('shard_size', $size);
95    }
96
97    /**
98     * Sets the name of the suggest. It is automatically set by
99     * the constructor.
100     *
101     * @param string $name The name of the suggest
102     *
103     * @throws InvalidException If name is empty
104     *
105     * @return $this
106     */
107    public function setName(string $name): self
108    {
109        if ('' === $name) {
110            throw new InvalidException('Suggest name has to be set');
111        }
112        $this->_name = $name;
113
114        return $this;
115    }
116
117    /**
118     * Retrieve the name of this suggestion.
119     */
120    public function getName(): string
121    {
122        return $this->_name;
123    }
124}
125