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