Repository: ruflin/Elastica (php)

Issue:
Call to a member function toArray() on array
When i run a query then i get this error `Call to a member function toArray() on array`

My Setup:
----
- symfony/symfony v3.3.8
- friendsofsymfony/elastica-bundle dev-master 96c79eb
- elasticsearch/elasticsearch v6.0.0
- ruflin/elastica 6.0.0
- Elastic-Server Version: 6.0.0

Error:
----
```txt
Call to a member function toArray() on array
```

**Entry-point:** (use of FOSElasticaBundle)
```php
$adapter = $finder->createPaginatorAdapter($aqb->getQuery());
$aggData = $adapter->getAggregations();
```

**stack-trace:**
```txt
Symfony\Component\Debug\Exception\FatalThrowableError:
Call to a member function toArray() on array

  at vendor/ruflin/elastica/lib/Elastica/Query.php:333
  at Elastica\Query->toArray()
     (vendor/ruflin/elastica/lib/Elastica/Search.php:455)
  at Elastica\Search->search()
     (vendor/ruflin/elastica/lib/Elastica/Type.php:356)
  at Elastica\Type->search(object(Query))
     (vendor/friendsofsymfony/elastica-bundle/src/Paginator/RawPaginatorAdapter.php:107)
  at FOS\ElasticaBundle\Paginator\RawPaginatorAdapter->getAggregations()
     (src/ggm/ClassifiedAdBundle/Controller/AdvertController.php:455)
  at ggm\ClassifiedAdBundle\Controller\AdvertController->overviewAction(object(Request), 'literatur')
  at call_user_func_array(array(object(AdvertController), 'overviewAction'), array(object(Request), 'literatur'))
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:153)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:171)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (web/app_dev.php:31)
  at require('/home/kokoloko/projects/php/portal-webapp-2/web/app_dev.php')
     (vendor/symfony/symfony/src/Symfony/Bundle/WebServerBundle/Resources/router.php:42)
```

Problem
----

The problem is caused by the change of this line in `Elastica/lib/Elastica/Query.php`:
https://github.com/ruflin/Elastica/blob/master/lib/Elastica/Query.php#L333

(see commit: https://github.com/ruflin/Elastica/commit/136e36680eaad95fe5fa0a922ad4673d64e34213)

Pull request:
Fix raw post_filter
Taken from https://github.com/ruflin/Elastica/pull/1471 (Fixes #1429)

It adds extra test to avoid regression.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fdb06040..aee3ce98 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 * Fixed `Elastica\Connection\StrategyInterface` instance checks [#1921](https://github.com/ruflin/Elastica/pull/1921)
 * Fixed various PHPDoc annotations [#1922](https://github.com/ruflin/Elastica/pull/1922)
 * Fixed numeric index names are returned as `int` in `Elastica\Status::getIndexNames()` [#1928](https://github.com/ruflin/Elastica/pull/1928)
+* Fixed using raw array in `post_filter` [#1950](https://github.com/ruflin/Elastica/pull/1950)
 ### Security
 
 ## [7.1.0](https://github.com/ruflin/Elastica/compare/7.0.0...7.1.0)
diff --git a/src/Query.php b/src/Query.php
index 8b4d3cc4..bc206bae 100644
--- a/src/Query.php
+++ b/src/Query.php
@@ -282,7 +282,7 @@ class Query extends Param
             $this->setQuery(new MatchAll());
         }
 
-        if (isset($this->_params['post_filter']) && 0 === \count(($this->_params['post_filter'])->toArray())) {
+        if (isset($this->_params['post_filter']) && 0 === \count($this->_params['post_filter'])) {
             unset($this->_params['post_filter']);
         }
 


Test patch:
diff --git a/tests/QueryTest.php b/tests/QueryTest.php
index 2b215dd6..7d4b940a 100644
--- a/tests/QueryTest.php
+++ b/tests/QueryTest.php
@@ -389,6 +389,32 @@ class QueryTest extends BaseTest
         $this->assertEquals($query->toArray(), $anotherQuery->toArray());
     }
 
+    /**
+     * @group unit
+     */
+    public function testRawPostFilter(): void
+    {
+        $query = (new Query())
+            ->setQuery(new Term(['field' => 'value']))
+            ->setParam('post_filter', [
+                'term' => ['field' => 'value'],
+            ])
+        ;
+
+        $this->assertSame([
+            'query' => [
+                'term' => [
+                    'field' => 'value',
+                ],
+            ],
+            'post_filter' => [
+                'term' => [
+                    'field' => 'value',
+                ],
+            ],
+        ], $query->toArray());
+    }
+
     /**
      * @group functional
      */