Completed
Pull Request — develop (#25)
by Sam
03:27 queued 34s
created

HasChildQuery::setQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Joining;
2
3
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument;
4
use Nord\Lumen\Elasticsearch\Search\Query\ScoreMode;
5
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasType;
6
7
/**
8
 * The has_child filter accepts a query and the child type to run against, and results in parent documents that have
9
 * child docs matching the query.
10
 *
11
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html
12
 */
13
class HasChildQuery extends AbstractQuery
14
{
15
    use HasType;
16
17
    /**
18
     * @var int
19
     */
20
    private $minChildren;
21
22
    /**
23
     * @var int
24
     */
25
    private $maxChildren;
26
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function toArray()
32
    {
33
        $hasChild = [
34
            'type'  => $this->getType(),
35
            'query' => $this->getQuery()->toArray(),
36
        ];
37
38
        $scoreMode = $this->getScoreMode();
39
        if (!is_null($scoreMode)) {
40
            $hasChild['score_mode'] = $scoreMode;
41
        }
42
43
        $minChildren = $this->getMinChildren();
44
        if (!is_null($minChildren)) {
45
            $hasChild['min_children'] = $minChildren;
46
        }
47
48
        $maxChildren = $this->getMaxChildren();
49
        if (!is_null($maxChildren)) {
50
            $hasChild['max_children'] = $maxChildren;
51
        }
52
53
        return ['has_child' => $hasChild];
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 View Code Duplication
    protected function getValidScoreModes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        return [
62
            ScoreMode::MODE_AVG,
63
            ScoreMode::MODE_SUM,
64
            ScoreMode::MODE_MIN,
65
            ScoreMode::MODE_MAX,
66
            ScoreMode::MODE_NONE,
67
        ];
68
    }
69
70
71
    /**
72
     * @param int $minChildren
73
     * @return HasChildQuery
74
     */
75
    public function setMinChildren($minChildren)
76
    {
77
        $this->assertMinChildren($minChildren);
78
        $this->minChildren = $minChildren;
79
        return $this;
80
    }
81
82
83
    /**
84
     * @return int
85
     */
86
    public function getMinChildren()
87
    {
88
        return $this->minChildren;
89
    }
90
91
92
    /**
93
     * @param int $maxChildren
94
     * @return HasChildQuery
95
     */
96
    public function setMaxChildren($maxChildren)
97
    {
98
        $this->assertMaxChildren($maxChildren);
99
        $this->maxChildren = $maxChildren;
100
        return $this;
101
    }
102
103
104
    /**
105
     * @return int
106
     */
107
    public function getMaxChildren()
108
    {
109
        return $this->maxChildren;
110
    }
111
112
113
    /**
114
     * @param int $minChildren
115
     * @throws InvalidArgument
116
     */
117
    protected function assertMinChildren($minChildren)
118
    {
119
        if (!is_int($minChildren)) {
120
            throw new InvalidArgument(sprintf(
121
                'HasChild Query `min_children` must be an integer, "%s" given.',
122
                gettype($minChildren)
123
            ));
124
        }
125
    }
126
127
128
    /**
129
     * @param int $maxChildren
130
     * @throws InvalidArgument
131
     */
132
    protected function assertMaxChildren($maxChildren)
133
    {
134
        if (!is_int($maxChildren)) {
135
            throw new InvalidArgument(sprintf(
136
                'HasChild Query `max_children` must be an integer, "%s" given.',
137
                gettype($maxChildren)
138
            ));
139
        }
140
    }
141
}
142