Passed
Push — 1.x ( 77e7f6...966118 )
by Adrian
02:40 queued 12s
created

Distance::setDistance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace Manticoresearch\Query;
5
6
use Manticoresearch\Exceptions\RuntimeException;
7
use Manticoresearch\Query;
8
9
class Distance extends Query
10
{
11
    public function __construct($args = [])
12
    {
13
        $this->_params['geo_distance'] = [];
14
        $this->_params['geo_distance']['distance_type'] = $args['type'] ?? 'adaptive';
15
        if (count($args) > 0) {
16
            if (!isset($args['location_anchor'])) {
17
                throw new RuntimeException('anchors not provided');
18
            }
19
            $this->_params['geo_distance']['location_anchor'] = $args['location_anchor'];
20
            if (!isset($args['location_source'])) {
21
                throw new RuntimeException('source attributes not provided');
22
            }
23
            if (is_array($args['location_source'])) {
24
                $args['location_source'] = implode(',', $args['location_source']);
25
            }
26
            $this->_params['geo_distance']['location_source'] = $args['location_source'];
27
28
            if (!isset($args['location_distance'])) {
29
                throw new RuntimeException('distance not provided');
30
            }
31
            $this->_params['geo_distance']['distance'] = $args['location_distance'];
32
        }
33
    }
34
35
    public function setDistance($distance)
36
    {
37
        $this->_params['geo_distance']['distance'] = $distance;
38
    }
39
40
    public function setSource($source)
41
    {
42
        if (is_array($source)) {
43
            $source = implode(',', $source);
44
        }
45
        $this->_params['geo_distance']['location_source'] = $source;
46
    }
47
48
    public function setAnchor($lat, $lon)
49
    {
50
        $this->_params['geo_distance']['location_anchor'] = ['lat' => $lat, 'lon' => $lon];
51
    }
52
53
    public function setDistanceType($algorithm)
54
    {
55
        $this->_params['geo_distance']['distance_type'] = $algorithm ?? 'adaptive';
56
    }
57
}
58