Completed
Push — master ( 6c9334...f3ba17 )
by Simonas
02:26
created

GeoShapeQuery::addPreIndexedShape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 6
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\Query\Geo;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Represents Elasticsearch "geo_shape" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html
21
 */
22
class GeoShapeQuery implements BuilderInterface
23
{
24
    use ParametersTrait;
25
26
    /**
27
     * @var array
28
     */
29
    private $fields = [];
30
31
    /**
32
     * @param array $parameters
33
     */
34
    public function __construct(array $parameters = [])
35
    {
36
        $this->setParameters($parameters);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getType()
43
    {
44
        return 'geo_shape';
45
    }
46
47
    /**
48
     * Add geo-shape provided filter.
49
     *
50
     * @param string $field       Field name.
51
     * @param string $type        Shape type.
52
     * @param array  $coordinates Shape coordinates.
53
     * @param array  $parameters  Additional parameters.
54
     */
55
    public function addShape($field, $type, array $coordinates, array $parameters = [])
56
    {
57
        $filter = array_merge(
58
            $parameters,
59
            [
60
                'type' => $type,
61
                'coordinates' => $coordinates,
62
            ]
63
        );
64
65
        $this->fields[$field]['shape'] = $filter;
66
    }
67
68
    /**
69
     * Add geo-shape pre-indexed filter.
70
     *
71
     * @param string $field      Field name.
72
     * @param string $id         The ID of the document that containing the pre-indexed shape.
73
     * @param string $type       Name of the index where the pre-indexed shape is.
74
     * @param string $index      Index type where the pre-indexed shape is.
75
     * @param string $path       The field specified as path containing the pre-indexed shape.
76
     * @param array  $parameters Additional parameters.
77
     */
78
    public function addPreIndexedShape($field, $id, $type, $index, $path, array $parameters = [])
79
    {
80
        $filter = array_merge(
81
            $parameters,
82
            [
83
                'id' => $id,
84
                'type' => $type,
85
                'index' => $index,
86
                'path' => $path,
87
            ]
88
        );
89
90
        $this->fields[$field]['indexed_shape'] = $filter;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function toArray()
97
    {
98
        $output = $this->processArray($this->fields);
99
100
        return [$this->getType() => $output];
101
    }
102
}
103