Passed
Pull Request — master (#1)
by Christopher
05:53
created

Geo::geoDistance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Es\Dsl\Dsl\Query;
3
4
use ONGR\ElasticsearchDSL\Query\Geo\GeoBoundingBoxQuery;
5
use ONGR\ElasticsearchDSL\Query\Geo\GeoDistanceQuery;
6
use ONGR\ElasticsearchDSL\Query\Geo\GeoPolygonQuery;
7
use ONGR\ElasticsearchDSL\Query\Geo\GeoShapeQuery;
8
use Triadev\Es\Dsl\Dsl\AbstractDsl;
9
use Triadev\Es\Dsl\Model\Location;
10
11
class Geo extends AbstractDsl
12
{
13
    /**
14
     * Geo bounding box
15
     *
16
     * @param string $field
17
     * @param Location[] $locations
18
     * @param array $params
19
     * @return Geo
20
     */
21
    public function geoBoundingBox(string $field, array $locations, array $params = []): Geo
22
    {
23
        $l = [];
24
        
25
        foreach ($locations as $location) {
26
            if ($location instanceof Location) {
27
                $l[] = [
28
                    'lat' => $location->getLatitude(),
29
                    'lon' => $location->getLongitude()
30
                ];
31
            }
32
        }
33
        
34
        return $this->append(new GeoBoundingBoxQuery($field, $l, $params));
35
    }
36
    
37
    /**
38
     * Geo distance
39
     *
40
     * @param string $field
41
     * @param string $distance
42
     * @param Location $location
43
     * @return Geo
44
     */
45
    public function geoDistance(string $field, string $distance, Location $location): Geo
46
    {
47
        return $this->append(new GeoDistanceQuery(
48
            $field,
49
            $distance,
50
            [
51
                'lat' => $location->getLatitude(),
52
                'lon' => $location->getLongitude()
53
            ]
54
        ));
55
    }
56
    
57
    /**
58
     * Geo polygon
59
     *
60
     * @param string $field
61
     * @param Location[] $points
62
     * @return Geo
63
     */
64
    public function geoPolygon(string $field, array $points): Geo
65
    {
66
        $p = [];
67
        
68
        foreach ($points as $point) {
69
            if ($point instanceof Location) {
70
                $p[] = [
71
                    'lat' => $point->getLatitude(),
72
                    'lon' => $point->getLongitude()
73
                ];
74
            }
75
        }
76
        
77
        return $this->append(new GeoPolygonQuery($field, $p));
78
    }
79
    
80
    /**
81
     * Geo shape
82
     *
83
     * @param array $params
84
     * @return Geo
85
     */
86
    public function geoShape(array $params = []): Geo
87
    {
88
        return $this->append(new GeoShapeQuery($params));
89
    }
90
}
91