Skip to content

Commit 2029520

Browse files
authored
Merge pull request #1 from gozowy/php8
pulled in PRs from upstream branch which is no longer supported
2 parents b4c3c89 + 11417d6 commit 2029520

29 files changed

+290
-84
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
sudo: false
22
language: php
33
php:
4-
- 7.1
54
- 7.2
65
- 7.3
76
- 7.4snapshot
7+
- 8.0
88
matrix:
99
allow_failures:
1010
- php: 7.4snapshot

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^7.0",
15-
"symfony/serializer": "^3.0|^4.0",
16-
"paragonie/random_compat": "*"
14+
"php": "^7.2 || ^8.0",
15+
"symfony/serializer": "^3.0|^4.0"
1716
},
1817
"require-dev": {
1918
"elasticsearch/elasticsearch": "^7.0",
20-
"phpunit/phpunit": "~6.0",
19+
"phpunit/phpunit": "^8.0",
2120
"squizlabs/php_codesniffer": "^3.0"
2221
},
2322
"suggest": {

src/Aggregation/Bucketing/RangeAggregation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function ($v) {
9090
}
9191
);
9292

93-
if (!empty($key)) {
93+
if ('' !== $key) {
9494
$range['key'] = $key;
9595
}
9696

src/Sort/GeoDistanceSort.php

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchDSL\Sort;
13+
14+
use ONGR\ElasticsearchDSL\BuilderInterface;
15+
use ONGR\ElasticsearchDSL\ParametersTrait;
16+
17+
/**
18+
* Holds all the values required for basic sorting.
19+
*/
20+
class GeoDistanceSort implements BuilderInterface
21+
{
22+
use ParametersTrait;
23+
24+
const ASC = 'asc';
25+
const DESC = 'desc';
26+
27+
const ARC = 'arc';
28+
const PLANE = 'plane';
29+
30+
/**
31+
* @var string
32+
*/
33+
private $field;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $order;
39+
40+
/**
41+
* @var string
42+
*/
43+
private $distanceType;
44+
45+
/**
46+
* @var string
47+
*/
48+
private $location;
49+
50+
/**
51+
* @var BuilderInterface
52+
*/
53+
private $nestedFilter;
54+
55+
/**
56+
* @param string $field Field name.
57+
* @param string|array $location The location to measure the distance from. The possible representations can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html
58+
* @param string $order Order direction.
59+
* @param array $params Params that can be set to field sort.
60+
*/
61+
public function __construct($field, $location, $order = null, $params = [])
62+
{
63+
$this->field = $field;
64+
$this->order = $order;
65+
$this->location = $location;
66+
$this->setParameters($params);
67+
}
68+
69+
/**
70+
* @return string
71+
*/
72+
public function getField()
73+
{
74+
return $this->field;
75+
}
76+
77+
/**
78+
* @param string $field
79+
*
80+
* @return $this
81+
*/
82+
public function setField($field)
83+
{
84+
$this->field = $field;
85+
86+
return $this;
87+
}
88+
89+
/**
90+
* @return string
91+
*/
92+
public function getOrder()
93+
{
94+
return $this->order;
95+
}
96+
97+
/**
98+
* @param string $order
99+
*
100+
* @return $this
101+
*/
102+
public function setOrder($order)
103+
{
104+
$this->order = $order;
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* @return BuilderInterface
111+
*/
112+
public function getNestedFilter()
113+
{
114+
return $this->nestedFilter;
115+
}
116+
117+
/**
118+
* @param BuilderInterface $nestedFilter
119+
*
120+
* @return $this
121+
*/
122+
public function setNestedFilter(BuilderInterface $nestedFilter)
123+
{
124+
$this->nestedFilter = $nestedFilter;
125+
126+
return $this;
127+
}
128+
129+
/**
130+
* @return string
131+
*/
132+
public function getDistanceType()
133+
{
134+
return $this->distanceType;
135+
}
136+
137+
/**
138+
* @param string $distanceType
139+
* @return GeoSort
140+
*/
141+
public function setDistanceType($distanceType)
142+
{
143+
$this->distanceType = $distanceType;
144+
145+
return $this;
146+
}
147+
148+
/**
149+
* @return string
150+
*/
151+
public function getLocation()
152+
{
153+
return $this->location;
154+
}
155+
156+
/**
157+
* @param string $location
158+
* @return GeoSort
159+
*/
160+
public function setLocation($location)
161+
{
162+
$this->location = $location;
163+
164+
return $this;
165+
}
166+
167+
/**
168+
* Returns element type.
169+
*
170+
* @return string
171+
*/
172+
public function getType()
173+
{
174+
return 'sort';
175+
}
176+
177+
/**
178+
* {@inheritdoc}
179+
*/
180+
public function toArray()
181+
{
182+
if ($this->field) {
183+
$this->addParameter($this->field, $this->location);
184+
}
185+
186+
if ($this->order) {
187+
$this->addParameter('order', $this->order);
188+
}
189+
190+
if ($this->nestedFilter) {
191+
$this->addParameter('nested', $this->nestedFilter->toArray());
192+
}
193+
194+
$output = [
195+
'_geo_distance' => !$this->getParameters() ? new \stdClass() : $this->getParameters(),
196+
];
197+
198+
return $output;
199+
}
200+
}

tests/Functional/AbstractElasticsearchTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class AbstractElasticsearchTestCase extends TestCase
3131
/**
3232
* {@inheritdoc}
3333
*/
34-
protected function setUp()
34+
protected function setUp(): void
3535
{
3636
parent::setUp();
3737

@@ -109,7 +109,7 @@ protected function getDataArray()
109109
/**
110110
* {@inheritdoc}
111111
*/
112-
protected function tearDown()
112+
protected function tearDown(): void
113113
{
114114
parent::tearDown();
115115
$this->deleteIndex();

tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ class ChildrenAggregationTest extends \PHPUnit\Framework\TestCase
2020
{
2121
/**
2222
* Tests if ChildrenAggregation#getArray throws exception when expected.
23-
*
24-
* @expectedException \LogicException
2523
*/
2624
public function testGetArrayException()
2725
{
26+
$this->expectException(\LogicException::class);
2827
$aggregation = new ChildrenAggregation('foo');
2928
$aggregation->getArray();
3029
}

tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ class DateHistogramAggregationTest extends \PHPUnit\Framework\TestCase
2020
{
2121
/**
2222
* Tests if ChildrenAggregation#getArray throws exception when expected.
23-
*
24-
* @expectedException \LogicException
2523
*/
2624
public function testGetArrayException()
2725
{
26+
$this->expectException(\LogicException::class);
2827
$aggregation = new DateHistogramAggregation('foo');
2928
$aggregation->getArray();
3029
}

tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@ class DateRangeAggregationTest extends \PHPUnit\Framework\TestCase
1717
{
1818
/**
1919
* Test if exception is thrown.
20-
*
21-
* @expectedException \LogicException
22-
* @expectedExceptionMessage Date range aggregation must have field, format set and range added.
2320
*/
2421
public function testIfExceptionIsThrownWhenNoParametersAreSet()
2522
{
23+
$this->expectException(\LogicException::class);
24+
$this->expectExceptionMessage('Date range aggregation must have field, format set and range added.');
2625
$agg = new DateRangeAggregation('test_agg');
2726
$agg->getArray();
2827
}
2928

3029
/**
3130
* Test if exception is thrown when both range parameters are null.
32-
*
33-
* @expectedException \LogicException
34-
* @expectedExceptionMessage Either from or to must be set. Both cannot be null.
3531
*/
3632
public function testIfExceptionIsThrownWhenBothRangesAreNull()
3733
{
34+
$this->expectException(\LogicException::class);
35+
$this->expectExceptionMessage('Either from or to must be set. Both cannot be null.');
3836
$agg = new DateRangeAggregation('test_agg');
3937
$agg->addRange(null, null);
4038
}

tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,22 @@ public function testToArray($aggregation, $expectedResult)
101101

102102
/**
103103
* Test for setField().
104-
*
105-
* @expectedException \LogicException
106-
* @expectedExceptionMessage doesn't support `field` parameter
107104
*/
108105
public function testSetField()
109106
{
107+
$this->expectException(\LogicException::class);
108+
$this->expectExceptionMessage("doesn't support `field` parameter");
110109
$aggregation = new FilterAggregation('test_agg');
111110
$aggregation->setField('test_field');
112111
}
113112

114113
/**
115114
* Test for toArray() without setting a filter.
116-
*
117-
* @expectedException \LogicException
118-
* @expectedExceptionMessage has no filter added
119115
*/
120116
public function testToArrayNoFilter()
121117
{
118+
$this->expectException(\LogicException::class);
119+
$this->expectExceptionMessage('has no filter added');
122120
$aggregation = new FilterAggregation('test_agg');
123121
$aggregation->toArray();
124122
}

tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ class FiltersAggregationTest extends \PHPUnit\Framework\TestCase
2121
{
2222
/**
2323
* Test if exception is thrown when not anonymous filter is without name.
24-
*
25-
* @expectedException \LogicException
26-
* @expectedExceptionMessage In not anonymous filters filter name must be set.
2724
*/
2825
public function testIfExceptionIsThrown()
2926
{
27+
$this->expectException(\LogicException::class);
28+
$this->expectExceptionMessage('In not anonymous filters filter name must be set.');
3029
$mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock();
3130
$aggregation = new FiltersAggregation('test_agg');
3231
$aggregation->addFilter($mock);

0 commit comments

Comments
 (0)