Completed
Push — master ( 72c2fc...074e1b )
by Simonas
04:21
created

HistogramAggregation::setOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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\Aggregation\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
17
/**
18
 * Class representing Histogram aggregation.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html
21
 */
22
class HistogramAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    const DIRECTION_ASC = 'asc';
27
    const DIRECTION_DESC = 'desc';
28
29
    /**
30
     * @var int
31
     */
32
    protected $interval;
33
34
    /**
35
     * @var int
36
     */
37
    protected $minDocCount;
38
39
    /**
40
     * @var array
41
     */
42
    protected $extendedBounds;
43
44
    /**
45
     * @var string
46
     */
47
    protected $orderMode;
48
49
    /**
50
     * @var string
51
     */
52
    protected $orderDirection;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $keyed;
58
59
    /**
60
     * Inner aggregations container init.
61
     *
62
     * @param string $name
63
     * @param string $field
64
     * @param int    $interval
65
     * @param int    $minDocCount
66
     * @param string $orderMode
67
     * @param string $orderDirection
68
     * @param int    $extendedBoundsMin
69
     * @param int    $extendedBoundsMax
70
     * @param bool   $keyed
71
     */
72
    public function __construct(
73
        $name,
74
        $field = null,
75
        $interval = null,
76
        $minDocCount = null,
77
        $orderMode = null,
78
        $orderDirection = self::DIRECTION_ASC,
79
        $extendedBoundsMin = null,
80
        $extendedBoundsMax = null,
81
        $keyed = null
82
    ) {
83
        parent::__construct($name);
84
85
        $this->setField($field);
86
        $this->setInterval($interval);
87
        $this->setMinDocCount($minDocCount);
88
        $this->setOrder($orderMode, $orderDirection);
89
        $this->setExtendedBounds($extendedBoundsMin, $extendedBoundsMax);
90
        $this->setKeyed($keyed);
0 ignored issues
show
Bug introduced by
It seems like $keyed defined by parameter $keyed on line 81 can also be of type null; however, ONGR\ElasticsearchDSL\Ag...Aggregation::setKeyed() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function isKeyed()
97
    {
98
        return $this->keyed;
99
    }
100
101
    /**
102
     * Get response as a hash instead keyed by the buckets keys.
103
     *
104
     * @param bool $keyed
105
     */
106
    public function setKeyed($keyed)
107
    {
108
        $this->keyed = $keyed;
109
    }
110
111
    /**
112
     * Sets buckets ordering.
113
     *
114
     * @param string $mode
115
     * @param string $direction
116
     */
117
    public function setOrder($mode, $direction = self::DIRECTION_ASC)
118
    {
119
        $this->orderMode = $mode;
120
        $this->orderDirection = $direction;
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getOrder()
127
    {
128
        if ($this->orderMode && $this->orderDirection) {
129
            return [$this->orderMode => $this->orderDirection];
130
        } else {
131
            return null;
132
        }
133
    }
134
135
    /**
136
     * @return int
137
     */
138
    public function getInterval()
139
    {
140
        return $this->interval;
141
    }
142
143
    /**
144
     * @param int $interval
145
     */
146
    public function setInterval($interval)
147
    {
148
        $this->interval = $interval;
149
    }
150
151
    /**
152
     * @return int
153
     */
154
    public function getMinDocCount()
155
    {
156
        return $this->minDocCount;
157
    }
158
159
    /**
160
     * Set limit for document count buckets should have.
161
     *
162
     * @param int $minDocCount
163
     */
164
    public function setMinDocCount($minDocCount)
165
    {
166
        $this->minDocCount = $minDocCount;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getExtendedBounds()
173
    {
174
        return $this->extendedBounds;
175
    }
176
177
    /**
178
     * @param int $min
179
     * @param int $max
180
     */
181
    public function setExtendedBounds($min = null, $max = null)
182
    {
183
        $bounds = array_filter(
184
            [
185
                'min' => $min,
186
                'max' => $max,
187
            ]
188
        );
189
        $this->extendedBounds = $bounds;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function getType()
196
    {
197
        return 'histogram';
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function getArray()
204
    {
205
        $out = array_filter(
206
            [
207
                'field' => $this->getField(),
208
                'interval' => $this->getInterval(),
209
                'min_doc_count' => $this->getMinDocCount(),
210
                'extended_bounds' => $this->getExtendedBounds(),
211
                'keyed' => $this->isKeyed(),
212
                'order' => $this->getOrder(),
213
            ],
214
            function ($val) {
215
                return ($val || is_numeric($val));
216
            }
217
        );
218
        $this->checkRequiredParameters($out, ['field', 'interval']);
219
220
        return $out;
221
    }
222
223
    /**
224
     * Checks if all required parameters are set.
225
     *
226
     * @param array $data
227
     * @param array $required
228
     *
229
     * @throws \LogicException
230
     */
231
    protected function checkRequiredParameters($data, $required)
232
    {
233
        if (count(array_intersect_key(array_flip($required), $data)) !== count($required)) {
234
            throw new \LogicException('Histogram aggregation must have field and interval set.');
235
        }
236
    }
237
}
238