Completed
Push — master ( ec539d...346131 )
by Adrian
02:33 queued 11s
created

Distance::__construct()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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