Issues (17)

lib/Query/Term/TermsQuery.php (1 issue)

1
<?php
2
3
namespace olvlvl\ElasticsearchDSL\Query\Term;
4
5
use olvlvl\ElasticsearchDSL\Query\QueryAbstract;
6
7
/**
8
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-terms-query.html
9
 */
10
class TermsQuery extends QueryAbstract
11
{
12
	const NAME = 'terms';
13
14
	/**
15
	 * @var string
16
	 */
17
	private $field;
18
19
	/**
20
	 * @var array
21
	 */
22
	private $values;
23
24
	/**
25
	 * @param string $field
26
	 * @param array $values
27
	 */
28
	public function __construct(string $field, array $values)
29
	{
30
		$this->field = $field;
31
		$this->values = $values;
32
	}
33
34
	/**
35
	 * @param null|string $index
36
	 *
37
	 * @return $this
38
	 */
39
	public function index(?string $index)
40
	{
41
		$this->options[__FUNCTION__] = $index;
42
43
		return $this;
44
	}
45
46
	/**
47
	 * @param null|string $type
48
	 *
49
	 * @return $this
50
	 */
51
	public function type(?string $type)
52
	{
53
		$this->options[__FUNCTION__] = $type;
54
55
		return $this;
56
	}
57
58
	/**
59
	 * @param null|string $id
60
	 *
61
	 * @return $this
62
	 */
63
	public function id(?string $id)
64
	{
65
		$this->options[__FUNCTION__] = $id;
66
67
		return $this;
68
	}
69
70
	/**
71
	 * @param null|string $path
72
	 *
73
	 * @return $this
74
	 */
75
	public function path(?string $path)
76
	{
77
		$this->options[__FUNCTION__] = $path;
78
79
		return $this;
80
	}
81
82
	/**
83
	 * @param null|string $routing
84
	 *
85
	 * @return $this
86
	 */
87
	public function routing(?string $routing)
88
	{
89
		$this->options[__FUNCTION__] = $routing;
90
91
		return $this;
92
	}
93
94
	/**
95
	 * @inheritdoc
96
	 */
97
	public function jsonSerialize()
98
	{
99
		$options = parent::jsonSerialize();
100
101
		if ($options) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
102
			return [ self::NAME => $options ];
103
		}
104
105
		return [ self::NAME => [ $this->field => $this->values ] ];
106
	}
107
}
108