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