These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Elastica\Aggregation; |
||
3 | |||
4 | use Elastica\Exception\InvalidException; |
||
5 | |||
6 | /** |
||
7 | * Class GeoDistance. |
||
8 | * |
||
9 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html |
||
10 | */ |
||
11 | class GeoDistance extends AbstractAggregation |
||
12 | { |
||
13 | const DISTANCE_TYPE_SLOPPY_ARC = 'sloppy_arc'; |
||
14 | const DISTANCE_TYPE_ARC = 'arc'; |
||
15 | const DISTANCE_TYPE_PLANE = 'plane'; |
||
16 | |||
17 | /** |
||
18 | * @param string $name the name if this aggregation |
||
19 | * @param string $field the field on which to perform this aggregation |
||
20 | * @param string|array $origin the point from which distances will be calculated |
||
21 | */ |
||
22 | public function __construct($name, $field, $origin) |
||
23 | { |
||
24 | parent::__construct($name); |
||
25 | $this->setField($field)->setOrigin($origin); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Set the field for this aggregation. |
||
30 | * |
||
31 | * @param string $field the name of the document field on which to perform this aggregation |
||
32 | * |
||
33 | * @return $this |
||
34 | */ |
||
35 | public function setField($field) |
||
36 | { |
||
37 | return $this->setParam('field', $field); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Set the origin point from which distances will be calculated. |
||
42 | * |
||
43 | * @param string|array $origin valid formats are array("lat" => 52.3760, "lon" => 4.894), "52.3760, 4.894", and array(4.894, 52.3760) |
||
44 | * |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function setOrigin($origin) |
||
48 | { |
||
49 | return $this->setParam('origin', $origin); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Add a distance range to this aggregation. |
||
54 | * |
||
55 | * @param int $fromValue a distance |
||
56 | * @param int $toValue a distance |
||
57 | * |
||
58 | * @throws \Elastica\Exception\InvalidException |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | View Code Duplication | public function addRange($fromValue = null, $toValue = null) |
|
0 ignored issues
–
show
|
|||
63 | { |
||
64 | if (is_null($fromValue) && is_null($toValue)) { |
||
65 | throw new InvalidException('Either fromValue or toValue must be set. Both cannot be null.'); |
||
66 | } |
||
67 | |||
68 | $range = []; |
||
69 | |||
70 | if (!is_null($fromValue)) { |
||
71 | $range['from'] = $fromValue; |
||
72 | } |
||
73 | |||
74 | if (!is_null($toValue)) { |
||
75 | $range['to'] = $toValue; |
||
76 | } |
||
77 | |||
78 | return $this->addParam('ranges', $range); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Set the unit of distance measure for this aggregation. |
||
83 | * |
||
84 | * @param string $unit defaults to km |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function setUnit($unit) |
||
89 | { |
||
90 | return $this->setParam('unit', $unit); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Set the method by which distances will be calculated. |
||
95 | * |
||
96 | * @param string $distanceType see DISTANCE_TYPE_* constants for options. Defaults to sloppy_arc. |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setDistanceType($distanceType) |
||
101 | { |
||
102 | return $this->setParam('distance_type', $distanceType); |
||
103 | } |
||
104 | } |
||
105 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.