Mapping::normalize()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
rs 9.2222
cc 6
nc 12
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Mgid\Component\Pagination\Mapping;
4
5
use Mgid\Component\Pagination\InputInterface;
6
use Mgid\Component\Pagination\Normalizer\NormalizerInterface;
7
8
final class Mapping implements MappingInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private string $delimiter = '.';
14
15
    /**
16
     * @var string|null
17
     */
18
    private ?string $rootAlias = null;
19
20
    /**
21
     * @var array<string,string>
22
     */
23
    private array $associations = [];
24
25
    /**
26
     * @var ConstraintInterface
27
     */
28
    private ConstraintInterface $constraint;
29
30
    public function __construct()
31
    {
32
        $this->constraint = new Constraint();
33
    }
34
35
    /**
36
     * @return ConstraintInterface
37
     */
38
    public function getConstraint(): ConstraintInterface
39
    {
40
        return $this->constraint;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function setDelimiter(string $delimiter): void
47
    {
48
        $this->delimiter = $delimiter;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function setRootAlias(?string $rootAlias): void
55
    {
56
        $this->rootAlias = $rootAlias;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function setAssociations(array $associations): void
63
    {
64
        $this->associations = $associations;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function normalize(InputInterface $input, NormalizerInterface $normalizer): void
71
    {
72
        $orders = $filters = [];
73
74
        foreach ($input->getOrders() as $fieldName => $order) {
75
            if ($this->constraint->isAllowedOrder($fieldName)) {
76
                $orders[$this->resolve($fieldName)] = $order;
77
            }
78
        }
79
80
        foreach ($input->getFilters() as $fieldName => $operators) {
81
            if ($this->constraint->isAllowedFilter($fieldName)) {
82
                foreach ($operators as $operator => $value) {
83
                    $filters[$this->resolve($fieldName)][$operator] = $normalizer->normalize($value, $operator);
84
                }
85
            }
86
        }
87
88
        $input->setOrders($orders);
89
        $input->setFilters($filters);
90
    }
91
92
    /**
93
     * @param string $fieldName
94
     *
95
     * @return string
96
     */
97
    private function resolve(string $fieldName): string
98
    {
99
        if (isset($this->associations[$fieldName])) {
100
            return $this->associations[$fieldName];
101
        }
102
103
        if (null !== $this->rootAlias) {
104
            return \implode($this->delimiter, [$this->rootAlias, $fieldName]);
105
        }
106
107
        return $fieldName;
108
    }
109
}
110