diff --git a/src/Search.php b/src/Search.php index 5d12282e70fc4d929a0e9037e74f41e69dcf5dbe..54e6f05becd0d1ba234605233f89dd28bdffcdaa 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 86bf72feeb7b625d6f22fcc4e58e8a10977b9055..c7ef15e379064bb8d3b57cd8a12a23df804d39e6 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 bd02dac8520b6ff52e6d292ced201d7880871277..80db1e9be84bb2d692cb04c024088898614411f7 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 0000000000000000000000000000000000000000..b706090e3c94a0ebeaffb9f1124c2ffdee97a5d7 --- /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 0000000000000000000000000000000000000000..6cd24a8e90641369e8469af214f5f1ba3cef8edc --- /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 0000000000000000000000000000000000000000..bb6811b914278ff996c85420495c216e1dd8e4c8 --- /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 0000000000000000000000000000000000000000..bcbb9c3d0b89fd2a64a5099053c4af19a802620c --- /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 0000000000000000000000000000000000000000..e5a84d2e38d323d24360eb790be89cade8e99ba9 --- /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()); + } +}