1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 
<?php


namespace Manticoresearch\Query;

use Manticoresearch\Exceptions\RuntimeException;
use Manticoresearch\Query;

class Distance extends Query
{
    public function __construct($args = [])
    {
        $this->_params['geo_distance'] = [];
        $this->_params['geo_distance']['distance_type'] = $args['type'] ?? 'adaptive';
        if (count($args) > 0) {
            if (!isset($args['location_anchor'])) {
                throw new RuntimeException('anchors not provided');
            }
            $this->_params['geo_distance']['location_anchor'] = $args['location_anchor'];
            if (!isset($args['location_source'])) {
                throw new RuntimeException('source attributes not provided');
            }
            if (is_array($args['location_source'])) {
                $args['location_source'] = implode(',', $args['location_source']);
            }
            $this->_params['geo_distance']['location_source'] = $args['location_source'];

            if (!isset($args['location_distance'])) {
                throw new RuntimeException('distance not provided');
            }
            $this->_params['geo_distance']['distance'] = $args['location_distance'];
        }
    }

    public function setDistance($distance)
    {
        $this->_params['geo_distance']['distance'] = $distance;
    }

    public function setSource($source)
    {
        if (is_array($source)) {
            $source = implode(',', $source);
        }
        $this->_params['geo_distance']['location_source'] = $source;
    }

    public function setAnchor($lat, $lon)
    {
        $this->_params['geo_distance']['location_anchor'] = ['lat' => $lat, 'lon' => $lon];
    }

    public function setDistanceType($algorithm)
    {
        $this->_params['geo_distance']['distance_type'] = $algorithm ?? 'adaptive';
    }
}