From 33734124ba10b79be82ec84d7d403dc37e1f12da Mon Sep 17 00:00:00 2001
From: Benjamin Cremer <bc@shopware.com>
Date: Tue, 30 Jun 2015 11:44:18 +0200
Subject: [PATCH] Fix missing dependencies for Search class

---
 src/Search.php                                |  4 +-
 src/SearchEndpoint/AbstractSearchEndpoint.php |  2 +-
 src/SearchEndpoint/QueryEndpoint.php          |  2 +-
 .../Normalizer/AbstractNormalizable.php       | 29 ++++++
 .../Normalizer/CustomReferencedNormalizer.php | 45 ++++++++++
 .../Normalizer/OrderedNormalizerInterface.php | 25 ++++++
 src/Serializer/OrderedSerializer.php          | 88 +++++++++++++++++++
 tests/SearchTest.php                          | 28 ++++++
 8 files changed, 219 insertions(+), 4 deletions(-)
 create mode 100644 src/Serializer/Normalizer/AbstractNormalizable.php
 create mode 100644 src/Serializer/Normalizer/CustomReferencedNormalizer.php
 create mode 100644 src/Serializer/Normalizer/OrderedNormalizerInterface.php
 create mode 100644 src/Serializer/OrderedSerializer.php
 create mode 100644 tests/SearchTest.php

diff --git a/src/Search.php b/src/Search.php
index 5d12282..54e6f05 100644
--- a/src/Search.php
+++ b/src/Search.php
@@ -15,11 +15,11 @@ use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
 use ONGR\ElasticsearchDSL\Highlight\Highlight;
 use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory;
 use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointInterface;
+use ONGR\ElasticsearchDSL\Serializer\Normalizer\CustomReferencedNormalizer;
+use ONGR\ElasticsearchDSL\Serializer\OrderedSerializer;
 use ONGR\ElasticsearchDSL\Sort\AbstractSort;
 use ONGR\ElasticsearchDSL\Sort\Sorts;
 use ONGR\ElasticsearchDSL\Suggester\AbstractSuggester;
-use ONGR\ElasticsearchBundle\Serializer\Normalizer\CustomReferencedNormalizer;
-use ONGR\ElasticsearchBundle\Serializer\OrderedSerializer;
 use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
 
 /**
diff --git a/src/SearchEndpoint/AbstractSearchEndpoint.php b/src/SearchEndpoint/AbstractSearchEndpoint.php
index 86bf72f..c7ef15e 100644
--- a/src/SearchEndpoint/AbstractSearchEndpoint.php
+++ b/src/SearchEndpoint/AbstractSearchEndpoint.php
@@ -11,7 +11,7 @@
 
 namespace ONGR\ElasticsearchDSL\SearchEndpoint;
 
-use ONGR\ElasticsearchBundle\Serializer\Normalizer\AbstractNormalizable;
+use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable;
 
 /**
  * Abstract class used to define search endpoint with references.
diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php
index bd02dac..80db1e9 100644
--- a/src/SearchEndpoint/QueryEndpoint.php
+++ b/src/SearchEndpoint/QueryEndpoint.php
@@ -15,7 +15,7 @@ use ONGR\ElasticsearchDSL\BuilderInterface;
 use ONGR\ElasticsearchDSL\ParametersTrait;
 use ONGR\ElasticsearchDSL\Query\BoolQuery;
 use ONGR\ElasticsearchDSL\Query\FilteredQuery;
-use ONGR\ElasticsearchBundle\Serializer\Normalizer\OrderedNormalizerInterface;
+use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface;
 use Symfony\Component\OptionsResolver\OptionsResolver;
 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
 
diff --git a/src/Serializer/Normalizer/AbstractNormalizable.php b/src/Serializer/Normalizer/AbstractNormalizable.php
new file mode 100644
index 0000000..b706090
--- /dev/null
+++ b/src/Serializer/Normalizer/AbstractNormalizable.php
@@ -0,0 +1,29 @@
+<?php
+
+/*
+ * This file is part of the ONGR package.
+ *
+ * (c) NFQ Technologies UAB <info@nfq.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ONGR\ElasticsearchDSL\Serializer\Normalizer;
+
+use ONGR\ElasticsearchDSL\ParametersTrait;
+use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
+
+/**
+ * Custom abstract normalizer which can save references for other objects.
+ */
+abstract class AbstractNormalizable implements NormalizableInterface
+{
+    use ParametersTrait {
+        ParametersTrait::hasParameter as hasReference;
+        ParametersTrait::getParameter as getReference;
+        ParametersTrait::getParameters as getReferences;
+        ParametersTrait::addParameter as addReference;
+        ParametersTrait::setParameters as setReferences;
+    }
+}
diff --git a/src/Serializer/Normalizer/CustomReferencedNormalizer.php b/src/Serializer/Normalizer/CustomReferencedNormalizer.php
new file mode 100644
index 0000000..6cd24a8
--- /dev/null
+++ b/src/Serializer/Normalizer/CustomReferencedNormalizer.php
@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * This file is part of the ONGR package.
+ *
+ * (c) NFQ Technologies UAB <info@nfq.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ONGR\ElasticsearchDSL\Serializer\Normalizer;
+
+use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
+
+/**
+ * Normalizer used with referenced normalized objects.
+ */
+class CustomReferencedNormalizer extends CustomNormalizer
+{
+    /**
+     * @var array
+     */
+    private $references = [];
+
+    /**
+     * {@inheritdoc}
+     */
+    public function normalize($object, $format = null, array $context = [])
+    {
+        $object->setReferences($this->references);
+        $data = parent::normalize($object, $format, $context);
+        $this->references = array_merge($this->references, $object->getReferences());
+
+        return $data;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function supportsNormalization($data, $format = null)
+    {
+        return $data instanceof AbstractNormalizable;
+    }
+}
diff --git a/src/Serializer/Normalizer/OrderedNormalizerInterface.php b/src/Serializer/Normalizer/OrderedNormalizerInterface.php
new file mode 100644
index 0000000..bb6811b
--- /dev/null
+++ b/src/Serializer/Normalizer/OrderedNormalizerInterface.php
@@ -0,0 +1,25 @@
+<?php
+
+/*
+ * This file is part of the ONGR package.
+ *
+ * (c) NFQ Technologies UAB <info@nfq.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ONGR\ElasticsearchDSL\Serializer\Normalizer;
+
+/**
+ * This should be implemented by normalizable object that required to be processed in specific order.
+ */
+interface OrderedNormalizerInterface
+{
+    /**
+     * Returns normalization priority.
+     *
+     * @return int
+     */
+    public function getOrder();
+}
diff --git a/src/Serializer/OrderedSerializer.php b/src/Serializer/OrderedSerializer.php
new file mode 100644
index 0000000..bcbb9c3
--- /dev/null
+++ b/src/Serializer/OrderedSerializer.php
@@ -0,0 +1,88 @@
+<?php
+
+/*
+ * This file is part of the ONGR package.
+ *
+ * (c) NFQ Technologies UAB <info@nfq.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ONGR\ElasticsearchDSL\Serializer;
+
+use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface;
+use Symfony\Component\Serializer\Serializer;
+
+/**
+ * Custom serializer which orders data before normalization.
+ */
+class OrderedSerializer extends Serializer
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function normalize($data, $format = null, array $context = [])
+    {
+        return parent::normalize(
+            is_array($data) ? $this->order($data) : $data,
+            $format,
+            $context
+        );
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function denormalize($data, $type, $format = null, array $context = [])
+    {
+        return parent::denormalize(
+            is_array($data) ? $this->order($data) : $data,
+            $type,
+            $format,
+            $context
+        );
+    }
+
+    /**
+     * Orders objects if can be done.
+     *
+     * @param array $data Data to order.
+     *
+     * @return array
+     */
+    private function order(array $data)
+    {
+        $filteredData = $this->filterOrderable($data);
+
+        if (!empty($filteredData)) {
+            uasort(
+                $filteredData,
+                function (OrderedNormalizerInterface $a, OrderedNormalizerInterface $b) {
+                    return $a->getOrder() > $b->getOrder();
+                }
+            );
+
+            return array_merge($filteredData, array_diff_key($data, $filteredData));
+        }
+
+        return $data;
+    }
+
+    /**
+     * Filters out data which can be ordered.
+     *
+     * @param array $array Data to filter out.
+     *
+     * @return array
+     */
+    private function filterOrderable($array)
+    {
+        return array_filter(
+            $array,
+            function ($value) {
+                return $value instanceof OrderedNormalizerInterface;
+            }
+        );
+    }
+}
diff --git a/tests/SearchTest.php b/tests/SearchTest.php
new file mode 100644
index 0000000..e5a84d2
--- /dev/null
+++ b/tests/SearchTest.php
@@ -0,0 +1,28 @@
+<?php
+
+/*
+ * This file is part of the ONGR package.
+ *
+ * (c) NFQ Technologies UAB <info@nfq.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL;
+
+use ONGR\ElasticsearchDSL\Search;
+
+/**
+ * Test for Search.
+ */
+class SearchTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * Tests Search constructor.
+     */
+    public function testItCanBeInstantiated()
+    {
+        $this->assertInstanceOf('ONGR\ElasticsearchDSL\Search', new Search());
+    }
+}
-- 
GitLab