Completed
Pull Request — master (#348)
by
unknown
01:15
created

GeoShapeQuery::addPreIndexedShape()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 5
Ratio 16.13 %

Importance

Changes 0
Metric Value
dl 5
loc 31
rs 9.424
c 0
b 0
f 0
cc 2
nc 2
nop 7
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
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\Query\Geo;
15
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
use ONGR\ElasticsearchDSL\ParametersTrait;
18
19
/**
20
 * Represents Elasticsearch "geo_shape" query.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html
23
 */
24
class GeoShapeQuery implements BuilderInterface
25
{
26
    use ParametersTrait;
27
28
    public const INTERSECTS = 'intersects';
29
30
    public const DISJOINT = 'disjoint';
31
32
    public const WITHIN = 'within';
33
34
    public const CONTAINS = 'contains';
35
36
    private array $fields = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
37
38
    public function __construct(array $parameters = [])
39
    {
40
        $this->setParameters($parameters);
41
    }
42
43
    public function getType(): string
44
    {
45
        return 'geo_shape';
46
    }
47
48
    public function addShape(
49
        string $field,
50
        string $type,
51
        array $coordinates,
52
        string $relation = self::INTERSECTS,
53
        array $parameters = []
54
    ): void {
55
        $filter = array_merge(
56
            $parameters,
57
            [
58
                'type' => $type,
59
                'coordinates' => $coordinates,
60
            ]
61
        );
62
63
        $this->fields[$field] = [
64
            'shape' => $filter,
65
            'relation' => $relation,
66
        ];
67
    }
68
69
    public function addPreIndexedShape(
70
        string $field,
71
        string $id,
72
        string $type,
73
        string $index,
74
        string $path,
75
        string $relation = self::INTERSECTS,
76
        array $parameters = []
77
    ): void {
78
        $filter = array_merge(
79
            $parameters,
80
            [
81
                'id' => $id,
82
                'type' => $type,
83
                'index' => $index,
84
                'path' => $path,
85
            ]
86
        );
87
88
        $this->fields[$field] = [
89
            'indexed_shape' => $filter,
90
            'relation' => $relation,
91
        ];
92
    }
93
94
    public function toArray(): array
95
    {
96
        $output = $this->processArray($this->fields);
97
98
        return [$this->getType() => $output];
99
    }
100
}
101