Passed
Push — master ( 53568f...6b64e2 )
by PHPinnacle
03:12
created

HasChild::compile()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 16
nop 0
dl 0
loc 24
ccs 0
cts 19
cp 0
crap 30
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of PHPinnacle/Elastics.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace PHPinnacle\Elastics\Query;
14
15
use PHPinnacle\Elastics\Query;
16
use PHPinnacle\Elastics\Traits;
17
18
class HasChild implements Query
19
{
20
    use
21
        Traits\IgnoreUnmapped,
22
        Traits\ScoreMode
23
    ;
24
25
    /**
26
     * @var string
27
     */
28
    private $type;
29
30
    /**
31
     * @var Query
32
     */
33
    private $query;
34
35
    /**
36
     * @var int
37
     */
38
    private $minChildren;
39
40
    /**
41
     * @var int
42
     */
43
    private $maxChildren;
44
45
    /**
46
     * @param string $type
47
     * @param Query  $query
48
     */
49
    public function __construct(string $type, Query $query)
50
    {
51
        $this->type  = $type;
52
        $this->query = $query;
53
    }
54
55
    /**
56
     * @param int $min
57
     *
58
     * @return self
59
     */
60
    public function minChildren(int $min): self
61
    {
62
        $this->minChildren = $min;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param int $max
69
     *
70
     * @return self
71
     */
72
    public function maxChildren(int $max): self
73
    {
74
        $this->maxChildren = $max;
75
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function name(): string
83
    {
84
        return 'has_child';
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function compile(): array
91
    {
92
        $query = [
93
            'type'  => $this->type,
94
            'query' => $this->query,
95
        ];
96
97
        if ($this->scoreMode !== null) {
98
            $query['score_mode'] = $this->scoreMode;
99
        }
100
101
        if ($this->ignoreUnmapped !== null) {
102
            $query['ignore_unmapped'] = $this->ignoreUnmapped;
103
        }
104
105
        if ($this->minChildren !== null) {
106
            $query['min_children'] = $this->minChildren;
107
        }
108
109
        if ($this->maxChildren !== null) {
110
            $query['max_children'] = $this->maxChildren;
111
        }
112
113
        return $query;
114
    }
115
}
116