Skip to content

Commit

Permalink
Implement Countable on Objects in PHP7.2 - Backported from #1378 (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsche authored and ruflin committed Jul 2, 2018
1 parent d72afd0 commit 51faf4f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ php:
- 5.4
- 5.3
- 7.0
- 7.1
- 7.2
- hhvm

matrix:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file based on the

- Introduce getFullError() for ResponseSet
- Use the above to improve the output of getError() on Response
- In PHP 7.2 count() now raises a warning when an invalid parameter is passed. Only arrays and objects implementing the Countable interface should be passed. Backported from [#1378](https://github.com/ruflin/Elastica/pull/1378)


## [2.3.1](https://github.com/ruflin/Elastica/releases/tag/2.3.1) - 2015-10-17
Expand Down
10 changes: 10 additions & 0 deletions lib/Elastica/Filter/GeoPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ public function toArray()
),
);
}

/**
* @inheritdoc
*
* @return int
*/
public function count()
{
return count($this->_key);
}
}
12 changes: 11 additions & 1 deletion lib/Elastica/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @author Nicolas Ruflin <[email protected]>
*/
class Param implements ArrayableInterface
class Param implements ArrayableInterface, \Countable
{
/**
* Params.
Expand Down Expand Up @@ -188,4 +188,14 @@ public function getParams()
{
return $this->_params;
}

/**
* @inheritdoc
*
* @return int
*/
public function count()
{
return count($this->_params);
}
}
2 changes: 1 addition & 1 deletion lib/Elastica/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function toArray()
unset($this->_params['facets']);
}

if (isset($this->_params['post_filter']) && 0 === count($this->_params['post_filter'])) {
if (isset($this->_params['post_filter']) && 0 === count(($this->_params['post_filter'])->toArray())) {
unset($this->_params['post_filter']);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Elastica/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function getQuery()
*/
public function count()
{
return sizeof($this->_results);
return count($this->_results);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/lib/Elastica/Test/Filter/GeoPolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function testGeoPoint()

$query = new Query(new MatchAll());
$query->setPostFilter($geoFilter);
$this->assertEquals(1, $type->search($query)->count());
$a = $type->search($query);
$this->assertEquals(1, $a->count());

// Both points should be inside
$query = new Query();
Expand Down
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,6 @@ public function testGetDataEmpty()
Request::GET
)->getData();

$this->assertEquals(0, count($response));
$this->assertNull($response);
}
}

0 comments on commit 51faf4f

Please sign in to comment.