summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Filter/GeoDistanceRange.php
blob: 230e5ebf44980b04f08c501667fe97734cfd440d (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

namespace Elastica\Filter;

use Elastica\Exception\InvalidException;

/**
 * Geo distance filter
 *
 * @category Xodoa
 * @package Elastica
 * @author munkie
 * @link http://www.elasticsearch.org/guide/reference/query-dsl/geo-distance-range-filter.html
 */
class GeoDistanceRange extends AbstractGeoDistance
{
    const RANGE_FROM = 'from';
    const RANGE_TO = 'to';
    const RANGE_LT = 'lt';
    const RANGE_LTE = 'lte';
    const RANGE_GT = 'gt';
    const RANGE_GTE = 'gte';

    const RANGE_INCLUDE_LOWER = 'include_lower';
    const RANGE_INCLUDE_UPPER = 'include_upper';

    /**
     * @var array
     */
    protected $_ranges = array();

    /**
     * @param string       $key
     * @param array|string $location
     * @param array        $ranges
     * @internal param string $distance
     */
    public function __construct($key, $location, array $ranges = array())
    {
        parent::__construct($key, $location);

        if (!empty($ranges)) {
            $this->setRanges($ranges);
        }
    }

    /**
     * @param  array                                  $ranges
     * @return \Elastica\Filter\GeoDistanceRange
     */
    public function setRanges(array $ranges)
    {
        $this->_ranges = array();

        foreach ($ranges as $key => $value) {
            $this->setRange($key, $value);
        }

        return $this;
    }

    /**
     * @param  string                                 $key
     * @param  mixed                                  $value
     * @return \Elastica\Filter\GeoDistanceRange
     * @throws \Elastica\Exception\InvalidException
     */
    public function setRange($key, $value)
    {
        switch ($key) {
            case self::RANGE_TO:
            case self::RANGE_FROM:
            case self::RANGE_GT:
            case self::RANGE_GTE:
            case self::RANGE_LT:
            case self::RANGE_LTE:
                break;
            case self::RANGE_INCLUDE_LOWER:
            case self::RANGE_INCLUDE_UPPER:
                $value = (boolean) $value;
                break;
            default:
                throw new InvalidException('Invalid range parameter given: ' . $key);
        }
        $this->_ranges[$key] = $value;

        return $this;
    }

    /**
     * @return array
     */
    public function toArray()
    {
        foreach ($this->_ranges as $key => $value) {
            $this->setParam($key, $value);
        }

        return parent::toArray();
    }
}