MappedOrderByAdapter::checkFieldsMapType()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace KGzocha\Searcher\Criteria\Adapter;
5
6
use KGzocha\Searcher\Criteria\OrderByCriteriaInterface;
7
8
/**
9
 * This adapter can be used if end-user should not see the actual parameter values
10
 * that are used in sorting. In order to do so, please provide parameter $fieldsMap.
11
 * Fields map key should be a value that will be visible to end-user.
12
 * Fields map value will be visible to developer.
13
 * To get "mapped" value (for end-user) just use getOrderBy()
14
 * To get "real" value (for developer) use getMappedOrderBy().
15
 *
16
 * @author Krzysztof Gzocha <[email protected]>
17
 */
18
class MappedOrderByAdapter implements OrderByCriteriaInterface
19
{
20
    /**
21
     * @var OrderByCriteriaInterface
22
     */
23
    private $orderBy;
24
25
    /**
26
     * @var array|\ArrayAccess
27
     */
28
    private $fieldsMap;
29
30
    /**
31
     * @param OrderByCriteriaInterface $orderBy
32
     * @param array|\ArrayAccess       $fieldsMap keys will be visible to user,
33
     *                                            values to CriteriaBuilder
34
     */
35 11
    public function __construct(
36
        OrderByCriteriaInterface $orderBy,
37
        $fieldsMap
38
    ) {
39 11
        $this->checkFieldsMapType($fieldsMap);
40 10
        $this->orderBy = $orderBy;
41 10
        $this->fieldsMap = $fieldsMap;
42 10
    }
43
44
    /**
45
     * @return string|null Returns null if user will enter value that is not in fieldsMap
46
     */
47 2
    public function getMappedOrderBy()
48
    {
49 2
        if ($this->rawValueExistsInFieldsMap()) {
50 1
            return $this->fieldsMap[$this->getOrderBy()];
51
        }
52
53 1
        return null;
54
    }
55
56
    /**
57
     * @return array|\ArrayAccess
58
     */
59 1
    public function getFieldsMap()
60
    {
61 1
        return $this->fieldsMap;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 5
    public function getOrderBy()
68
    {
69 5
        return $this->orderBy->getOrderBy();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 8
    public function setOrderBy(string $orderBy = null)
76
    {
77 8
        return $this->orderBy->setOrderBy($orderBy);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 6
    public function shouldBeApplied(): bool
84
    {
85 6
        return $this->orderBy->shouldBeApplied() && $this->rawValueExistsInFieldsMap();
86
    }
87
88
    /**
89
     * @param mixed $fieldsMap
90
     *
91
     * @throws \InvalidArgumentException
92
     */
93 11
    private function checkFieldsMapType($fieldsMap)
94
    {
95 11
        if (!is_array($fieldsMap) && !$fieldsMap instanceof \ArrayAccess) {
96 1
            throw new \InvalidArgumentException(sprintf(
97
                'Parameter fieldsMap passed to %s should be an array or \ArrayAccess.'
98 1
                .' Given: %s',
99 1
                __CLASS__,
100 1
                is_object($fieldsMap) ? get_class($fieldsMap) : gettype($fieldsMap)
101
            ));
102
        }
103 10
    }
104
105
    /**
106
     * @return bool
107
     */
108 5
    private function rawValueExistsInFieldsMap(): bool
109
    {
110 5
        return array_key_exists($this->getOrderBy(), $this->fieldsMap);
111
    }
112
}
113