From 7a094bef3992cb7179538d72dffc9fe407dd518e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt>
Date: Wed, 25 Feb 2015 13:49:06 +0200
Subject: [PATCH] Refactored code

---
 Filter/HasChildFilter.php    | 44 +++++++++-------------------------
 Filter/HasParentFilter.php   | 46 ++++++++++--------------------------
 Query/ConstantScoreQuery.php | 16 ++++++++-----
 Query/FunctionScoreQuery.php | 22 +++++++++++------
 Query/MatchQuery.php         | 10 ++++----
 Query/MultiMatchQuery.php    | 20 ++++++++--------
 6 files changed, 63 insertions(+), 95 deletions(-)

diff --git a/Filter/HasChildFilter.php b/Filter/HasChildFilter.php
index cb8afae..5310923 100644
--- a/Filter/HasChildFilter.php
+++ b/Filter/HasChildFilter.php
@@ -21,19 +21,14 @@ class HasChildFilter implements BuilderInterface
 {
     use ParametersTrait;
 
-    const INNER_QUERY = 'query';
-    const INNER_FILTER = 'filter';
+    const USE_QUERY = 'query';
+    const USE_FILTER = 'filter';
 
     /**
      * @var string
      */
     private $type;
 
-    /**
-     * @var BuilderInterface
-     */
-    private $filter;
-
     /**
      * @var BuilderInterface
      */
@@ -41,27 +36,17 @@ class HasChildFilter implements BuilderInterface
 
     /**
      * @param string           $type
-     * @param BuilderInterface $block
+     * @param BuilderInterface $query
      * @param array            $parameters
-     * @param string           $inner
+     * @param string           $dslType
      *
      * @throws \InvalidArgumentException
      */
-    public function __construct($type, BuilderInterface $block, array $parameters = [], $inner = self::INNER_FILTER)
+    public function __construct($type, BuilderInterface $query, array $parameters = [], $dslType = self::USE_FILTER)
     {
         $this->type = $type;
-
-        switch ($inner) {
-            case 'filter':
-                $this->filter = $block;
-                break;
-            case 'query':
-                $this->query = $block;
-                break;
-            default:
-                throw new \InvalidArgumentException('Not supported argument type');
-        }
-
+        $this->dslType = $dslType;
+        $this->query = $query;
         $this->setParameters($parameters);
     }
 
@@ -78,17 +63,10 @@ class HasChildFilter implements BuilderInterface
      */
     public function toArray()
     {
-        $query = [ 'type' => $this->type ];
-
-        $queries = ['filter', 'query'];
-
-        foreach ($queries as $type) {
-            if ($this->{$type}) {
-                $query[$type] = [
-                    $this->{$type}->getType() => $this->{$type}->toArray(),
-                ];
-            }
-        }
+        $query = [
+            'type' => $this->type,
+            $this->dslType => [$this->query->getType() => $this->query->toArray()],
+        ];
 
         $output = $this->processArray($query);
 
diff --git a/Filter/HasParentFilter.php b/Filter/HasParentFilter.php
index d81dce2..d151c41 100644
--- a/Filter/HasParentFilter.php
+++ b/Filter/HasParentFilter.php
@@ -21,19 +21,14 @@ class HasParentFilter implements BuilderInterface
 {
     use ParametersTrait;
 
-    const INNER_QUERY = 'query';
-    const INNER_FILTER = 'filter';
+    const USE_QUERY = 'query';
+    const USE_FILTER = 'filter';
 
     /**
      * @var string
      */
     private $parentType;
 
-    /**
-     * @var BuilderInterface
-     */
-    private $filter;
-
     /**
      * @var BuilderInterface
      */
@@ -41,31 +36,21 @@ class HasParentFilter implements BuilderInterface
 
     /**
      * @param string           $parentType
-     * @param BuilderInterface $block
+     * @param BuilderInterface $query
      * @param array            $parameters
-     * @param string           $inner
+     * @param string           $dslType
      *
      * @throws \InvalidArgumentException
      */
     public function __construct(
         $parentType,
-        BuilderInterface $block,
+        BuilderInterface $query,
         array $parameters = [],
-        $inner = self::INNER_FILTER
+        $dslType = self::USE_FILTER
     ) {
         $this->parentType = $parentType;
-
-        switch ($inner) {
-            case 'filter':
-                $this->filter = $block;
-                break;
-            case 'query':
-                $this->query = $block;
-                break;
-            default:
-                throw new \InvalidArgumentException('Not supported argument type');
-        }
-
+        $this->dslType = $dslType;
+        $this->query = $query;
         $this->setParameters($parameters);
     }
 
@@ -82,17 +67,10 @@ class HasParentFilter implements BuilderInterface
      */
     public function toArray()
     {
-        $query = [ 'parent_type' => $this->parentType ];
-
-        $queries = ['filter', 'query'];
-
-        foreach ($queries as $type) {
-            if ($this->{$type}) {
-                $query[$type] = [
-                    $this->{$type}->getType() => $this->{$type}->toArray(),
-                ];
-            }
-        }
+        $query = [
+            'parent_type' => $this->parentType,
+            $this->dslType => [$this->query->getType() => $this->query->toArray()],
+        ];
 
         $output = $this->processArray($query);
 
diff --git a/Query/ConstantScoreQuery.php b/Query/ConstantScoreQuery.php
index 33990de..3eb19c1 100644
--- a/Query/ConstantScoreQuery.php
+++ b/Query/ConstantScoreQuery.php
@@ -21,6 +21,9 @@ class ConstantScoreQuery implements BuilderInterface
 {
     use ParametersTrait;
 
+    const USE_QUERY = 'query';
+    const USE_FILTER = 'filter';
+
     /**
      * @var string
      */
@@ -29,16 +32,17 @@ class ConstantScoreQuery implements BuilderInterface
     /**
      * @var BuilderInterface
      */
-    private $filterOrQuery;
+    private $query;
 
     /**
-     * @param BuilderInterface $filterOrQuery
+     * @param BuilderInterface $query
      * @param array            $parameters
+     * @param string           $dslType
      */
-    public function __construct(BuilderInterface $filterOrQuery, array $parameters = [])
+    public function __construct(BuilderInterface $query, array $parameters = [], $dslType = self::USE_FILTER)
     {
-        $this->dslType = array_slice(explode('\\', get_class($filterOrQuery)), -2, 1)[0];
-        $this->filterOrQuery = $filterOrQuery;
+        $this->dslType = $dslType;
+        $this->query = $query;
         $this->setParameters($parameters);
     }
 
@@ -57,7 +61,7 @@ class ConstantScoreQuery implements BuilderInterface
     {
         $query = [
             strtolower($this->dslType) => [
-                $this->filterOrQuery->getType() => $this->filterOrQuery->toArray(),
+                $this->query->getType() => $this->query->toArray(),
             ],
         ];
 
diff --git a/Query/FunctionScoreQuery.php b/Query/FunctionScoreQuery.php
index 7584eb8..f8f0541 100644
--- a/Query/FunctionScoreQuery.php
+++ b/Query/FunctionScoreQuery.php
@@ -21,6 +21,9 @@ class FunctionScoreQuery implements BuilderInterface
 {
     use ParametersTrait;
 
+    const USE_QUERY = 'query';
+    const USE_FILTER = 'filter';
+
     /**
      * @var string
      */
@@ -29,7 +32,7 @@ class FunctionScoreQuery implements BuilderInterface
     /**
      * @var BuilderInterface
      */
-    private $filterOrQuery;
+    private $query;
 
     /**
      * @var array[]
@@ -37,14 +40,19 @@ class FunctionScoreQuery implements BuilderInterface
     private $functions;
 
     /**
-     * @param BuilderInterface $filterOrQuery
+     * @param BuilderInterface $query
      * @param array            $functions
      * @param array            $parameters
+     * @param string           $dslType
      */
-    public function __construct(BuilderInterface $filterOrQuery, array $functions, array $parameters = [])
-    {
-        $this->dslType = array_slice(explode('\\', get_class($filterOrQuery)), -2, 1)[0];
-        $this->filterOrQuery = $filterOrQuery;
+    public function __construct(
+        BuilderInterface $query,
+        array $functions,
+        array $parameters = [],
+        $dslType = self::USE_FILTER
+    ) {
+        $this->dslType = $dslType;
+        $this->query = $query;
         $this->functions = $functions;
         $this->setParameters($parameters);
     }
@@ -64,7 +72,7 @@ class FunctionScoreQuery implements BuilderInterface
     {
         $query = [
             strtolower($this->dslType) => [
-                $this->filterOrQuery->getType() => $this->filterOrQuery->toArray(),
+                $this->query->getType() => $this->query->toArray(),
             ],
             'functions' => [$this->functions],
         ];
diff --git a/Query/MatchQuery.php b/Query/MatchQuery.php
index 7bc82d6..e6a0330 100644
--- a/Query/MatchQuery.php
+++ b/Query/MatchQuery.php
@@ -24,22 +24,22 @@ class MatchQuery implements BuilderInterface
     /**
      * @var string
      */
-    private $query;
+    private $field;
 
     /**
      * @var string
      */
-    private $field;
+    private $query;
 
     /**
-     * @param string $query
      * @param string $field
+     * @param string $query
      * @param array  $parameters
      */
-    public function __construct($query, $field, array $parameters = [])
+    public function __construct($field, $query, array $parameters = [])
     {
-        $this->query = $query;
         $this->field = $field;
+        $this->query = $query;
         $this->setParameters($parameters);
     }
 
diff --git a/Query/MultiMatchQuery.php b/Query/MultiMatchQuery.php
index 48695fd..d221b26 100644
--- a/Query/MultiMatchQuery.php
+++ b/Query/MultiMatchQuery.php
@@ -19,9 +19,14 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
 class MultiMatchQuery implements BuilderInterface
 {
     /**
-     * @var string
+     * @param array  $fields
+     * @param string $query
      */
-    private $query;
+    public function __construct(array $fields, $query)
+    {
+        $this->fields = $fields;
+        $this->query = $query;
+    }
 
     /**
      * @var array
@@ -29,14 +34,9 @@ class MultiMatchQuery implements BuilderInterface
     private $fields = [];
 
     /**
-     * @param string $query
-     * @param array  $fields
+     * @var string
      */
-    public function __construct($query, array $fields)
-    {
-        $this->query = $query;
-        $this->fields = $fields;
-    }
+    private $query;
 
     /**
      * {@inheritdoc}
@@ -52,8 +52,8 @@ class MultiMatchQuery implements BuilderInterface
     public function toArray()
     {
         return [
-            'query' => $this->query,
             'fields' => $this->fields,
+            'query' => $this->query,
         ];
     }
 }
-- 
GitLab