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

CardinalityAggregation::getArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 10
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\Aggregation\Metric;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait;
16
use ONGR\ElasticsearchDSL\ScriptAwareTrait;
17
18
/**
19
 * Difference values counter.
20
 *
21
 * @link http://goo.gl/tG7ciG
22
 */
23
class CardinalityAggregation extends AbstractAggregation
24
{
25
    use MetricTrait;
26
    use ScriptAwareTrait;
27
28
    /**
29
     * @var int
30
     */
31
    private $precisionThreshold;
32
33
    /**
34
     * @var bool
35
     */
36
    private $rehash;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getArray()
42
    {
43
        $out = array_filter(
44
            [
45
                'field' => $this->getField(),
46
                'script' => $this->getScript(),
47
                'precision_threshold' => $this->getPrecisionThreshold(),
48
                'rehash' => $this->isRehash(),
49
            ],
50
            function ($val) {
51
                return ($val || is_bool($val));
52
            }
53
        );
54
55
        $this->checkRequiredFields($out);
56
57
        return $out;
58
    }
59
60
    /**
61
     * Precision threshold.
62
     *
63
     * @param int $precision Precision Threshold.
64
     */
65
    public function setPrecisionThreshold($precision)
66
    {
67
        $this->precisionThreshold = $precision;
68
    }
69
70
    /**
71
     * @return int
72
     */
73
    public function getPrecisionThreshold()
74
    {
75
        return $this->precisionThreshold;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    public function isRehash()
82
    {
83
        return $this->rehash;
84
    }
85
86
    /**
87
     * @param bool $rehash
88
     */
89
    public function setRehash($rehash)
90
    {
91
        $this->rehash = $rehash;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getType()
98
    {
99
        return 'cardinality';
100
    }
101
102
    /**
103
     * Checks if required fields are set.
104
     *
105
     * @param array $fields
106
     *
107
     * @throws \LogicException
108
     */
109
    private function checkRequiredFields($fields)
110
    {
111
        if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) {
112
            throw new \LogicException('Cardinality aggregation must have field or script set.');
113
        }
114
    }
115
}
116