@@ -25,13 +25,17 @@ class SortOrder extends AbstractSimpleObject
25
25
* Initialize object and validate sort direction
26
26
*
27
27
* @param array $data
28
+ * @throws InputException
28
29
*/
29
30
public function __construct (array $ data = [])
30
31
{
31
32
parent ::__construct ($ data );
32
33
if (null !== $ this ->getDirection ()) {
33
34
$ this ->validateDirection ($ this ->getDirection ());
34
35
}
36
+ if ($ this ->getField () !== null ) {
37
+ $ this ->validateField ($ this ->getField ());
38
+ }
35
39
}
36
40
37
41
/**
@@ -48,10 +52,14 @@ public function getField()
48
52
* Set sorting field.
49
53
*
50
54
* @param string $field
55
+ * @throws InputException
56
+ *
51
57
* @return $this
52
58
*/
53
59
public function setField ($ field )
54
60
{
61
+ $ this ->validateField ($ field );
62
+
55
63
return $ this ->setData (SortOrder::FIELD , $ field );
56
64
}
57
65
@@ -69,6 +77,8 @@ public function getDirection()
69
77
* Set sorting direction.
70
78
*
71
79
* @param string $direction
80
+ * @throws InputException
81
+ *
72
82
* @return $this
73
83
*/
74
84
public function setDirection ($ direction )
@@ -81,10 +91,10 @@ public function setDirection($direction)
81
91
* Validate direction argument ASC or DESC
82
92
*
83
93
* @param mixed $direction
84
- * @return null
94
+ * @return void
85
95
* @throws InputException
86
96
*/
87
- private function validateDirection ($ direction )
97
+ private function validateDirection ($ direction ): void
88
98
{
89
99
$ this ->validateDirectionIsString ($ direction );
90
100
$ this ->validateDirectionIsAscOrDesc ($ direction );
@@ -93,9 +103,9 @@ private function validateDirection($direction)
93
103
/**
94
104
* @param string $direction
95
105
* @throws InputException
96
- * @return null
106
+ * @return void
97
107
*/
98
- private function validateDirectionIsString ($ direction )
108
+ private function validateDirectionIsString ($ direction ): void
99
109
{
100
110
if (!is_string ($ direction )) {
101
111
throw new InputException (new Phrase (
@@ -108,9 +118,9 @@ private function validateDirectionIsString($direction)
108
118
/**
109
119
* @param string $direction
110
120
* @throws InputException
111
- * @return null
121
+ * @return void
112
122
*/
113
- private function validateDirectionIsAscOrDesc ($ direction )
123
+ private function validateDirectionIsAscOrDesc ($ direction ): void
114
124
{
115
125
$ normalizedDirection = $ this ->normalizeDirectionInput ($ direction );
116
126
if (!in_array ($ normalizedDirection , [SortOrder::SORT_ASC , SortOrder::SORT_DESC ], true )) {
@@ -129,4 +139,23 @@ private function normalizeDirectionInput($direction)
129
139
{
130
140
return strtoupper ($ direction );
131
141
}
142
+
143
+ /**
144
+ * Check if given value can be used as sorting field.
145
+ *
146
+ * @param string $field
147
+ * @return void
148
+ * @throws InputException
149
+ */
150
+ private function validateField (string $ field ): void
151
+ {
152
+ if (preg_match ('/[^a-z0-9\_]/i ' , $ field )) {
153
+ throw new InputException (
154
+ new Phrase (
155
+ 'Sort order field %1 contains restricted symbols ' ,
156
+ [$ field ]
157
+ )
158
+ );
159
+ }
160
+ }
132
161
}
0 commit comments