Completed
Pull Request — master (#117)
by
unknown
02:42
created

Suggest::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Suggest;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
17
18
class Suggest implements BuilderInterface
19
{
20
    use ParametersTrait;
21
22
    const TERM = 'term';
23
    const COMPLETION = 'completion';
24
    const PHRASE = 'phrase';
25
    const CONTEXT = 'completion';
26
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * @var string
34
     */
35
    private $type;
36
37
    /**
38
     * @var string
39
     */
40
    private $field;
41
42
    /**
43
     * @var string
44
     */
45
    private $text;
46
47
    /**
48
     * TermSuggest constructor.
49
     * @param string $name
50
     * @param string $field
51
     * @param string $type
52
     * @param string $text
53
     * @param array $parameters
54
     */
55
    public function __construct($name, $field, $type, $text, $parameters = [])
56
    {
57
        $this->setName($name);
58
        $this->validateType($type);
59
        $this->setField($field);
60
        $this->setType($type);
61
        $this->setText($text);
62
        $this->setParameters($parameters);
63
    }
64
65
    /**
66
     * Returns element type.
67
     *
68
     * @return string
69
     */
70
    public function getType()
71
    {
72
        return $this->type;
73
    }
74
75
    /**
76
     * @param string $type
77
     */
78
    public function setType($type)
79
    {
80
        $this->type = $type;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getText()
87
    {
88
        return $this->text;
89
    }
90
91
    /**
92
     * @param string $text
93
     */
94
    public function setText($text)
95
    {
96
        $this->text = $text;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getField()
103
    {
104
        return $this->field;
105
    }
106
107
    /**
108
     * @param string $field
109
     */
110
    public function setField($field)
111
    {
112
        $this->field = $field;
113
    }
114
115
    /**
116
     * @param string $name
117
     */
118
    public function setName($name)
119
    {
120
        $this->name = $name;
121
    }
122
123
    /**
124
     * Returns suggest name
125
     *
126
     * @return string
127
     */
128
    public function getName()
129
    {
130
        return $this->name;
131
    }
132
133
    /**
134
     * Checks if the type is valid
135
     *
136
     * @param string $type
137
     *
138
     * @return bool
139
     *
140
     * @throws InvalidArgumentException
141
     */
142
    private function validateType($type)
143
    {
144
        if (in_array($type, [
145
            self::COMPLETION,
146
            self::CONTEXT,
147
            self::PHRASE,
148
            self::TERM
149
        ])) {
150
            return true;
151
        }
152
        throw new InvalidArgumentException(
153
            'You must provide a valid type to the Suggest()'
154
        );
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function toArray()
161
    {
162
        $output = [
163
            $this->getName() => [
164
                'text' => $this->getText(),
165
                $this->getType() => $this->processArray(['field' => $this->getField()]),
166
            ]
167
        ];
168
169
        return $output;
170
    }
171
}
172