HasParentQuery::toArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
c 0
b 0
f 0
rs 9.9332
cc 3
nc 3
nop 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\Traits\HasType;
5
6
/**
7
 * The has_parent query accepts a query and a parent type. The query is executed in the parent document space, which is
8
 * specified by the parent type. This query returns child documents which associated parents have matched. For the rest
9
 * has_parent query has the same options and works in the same manner as the has_child query.
10
 *
11
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html
12
 */
13
class HasParentQuery extends AbstractQuery
14
{
15
    use HasType;
16
17
    /**
18
     * @inheritdoc
19
     * @throws InvalidArgument
20
     */
21
    public function toArray()
22
    {
23
        $query = $this->getQuery();
24
25
        if ($query === null) {
26
            throw new InvalidArgument('Query must be set');
27
        }
28
        
29
        $hasParent = [
30
            'parent_type'  => $this->getType(),
31
            'query'        => $query->toArray(),
32
        ];
33
34
        $scoreMode = $this->getScoreMode();
35
        if (null !== $scoreMode) {
36
            $hasParent['score_mode'] = $scoreMode;
37
        }
38
39
        return ['has_parent' => $hasParent];
40
    }
41
}
42