OrderedSerializer::filterOrderable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Serializer;
13
14
use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface;
15
use Symfony\Component\Serializer\Serializer;
16
17
/**
18
 * Custom serializer which orders data before normalization.
19
 */
20
class OrderedSerializer extends Serializer
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 View Code Duplication
    public function normalize($data, $format = null, array $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        return parent::normalize(
28
            is_array($data) ? $this->order($data) : $data,
29
            $format,
30
            $context
31
        );
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 View Code Duplication
    public function denormalize($data, $type, $format = null, array $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        return parent::denormalize(
40
            is_array($data) ? $this->order($data) : $data,
41
            $type,
42
            $format,
43
            $context
44
        );
45
    }
46
47
    /**
48
     * Orders objects if can be done.
49
     *
50
     * @param array $data Data to order.
51
     *
52
     * @return array
53
     */
54
    private function order(array $data)
55
    {
56
        $filteredData = $this->filterOrderable($data);
57
58
        if (!empty($filteredData)) {
59
            uasort(
60
                $filteredData,
61
                function (OrderedNormalizerInterface $a, OrderedNormalizerInterface $b) {
62
                    return $a->getOrder() > $b->getOrder();
63
                }
64
            );
65
66
            return array_merge($filteredData, array_diff_key($data, $filteredData));
67
        }
68
69
        return $data;
70
    }
71
72
    /**
73
     * Filters out data which can be ordered.
74
     *
75
     * @param array $array Data to filter out.
76
     *
77
     * @return array
78
     */
79
    private function filterOrderable($array)
80
    {
81
        return array_filter(
82
            $array,
83
            function ($value) {
84
                return $value instanceof OrderedNormalizerInterface;
85
            }
86
        );
87
    }
88
}
89