Skip to content

Commit 9592c4c

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-88642: Sort Order Field Values
1 parent ebbee92 commit 9592c4c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/internal/Magento/Framework/Api/SortOrder.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function __construct(array $data = [])
3030
if (null !== $this->getDirection()) {
3131
$this->validateDirection($this->getDirection());
3232
}
33+
if ($this->getField() !== null) {
34+
$this->validateField($this->getField());
35+
}
3336
}
3437

3538
/**
@@ -50,6 +53,8 @@ public function getField()
5053
*/
5154
public function setField($field)
5255
{
56+
$this->validateField($field);
57+
5358
return $this->setData(SortOrder::FIELD, $field);
5459
}
5560

@@ -127,4 +132,23 @@ private function normalizeDirectionInput($direction)
127132
{
128133
return strtoupper($direction);
129134
}
135+
136+
/**
137+
* Check if given value can be used as sorting field.
138+
*
139+
* @param string $field
140+
* @return void
141+
* @throws InputException
142+
*/
143+
private function validateField(string $field): void
144+
{
145+
if (preg_match('/[^a-z0-9\_]/i', $field)) {
146+
throw new InputException(
147+
new Phrase(
148+
'Sort order field %1 contains restricted symbols',
149+
[$field]
150+
)
151+
);
152+
}
153+
}
130154
}

lib/internal/Magento/Framework/Api/Test/Unit/SortOrderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,14 @@ public function testItValidatesADirectionAssignedDuringInstantiation()
8686
SortOrder::DIRECTION => 'not-asc-or-desc'
8787
]);
8888
}
89+
90+
/**
91+
* @expectedException \Magento\Framework\Exception\InputException
92+
*/
93+
public function testValidateField()
94+
{
95+
$this->sortOrder = new SortOrder([
96+
SortOrder::FIELD => 'invalid field (value);'
97+
]);
98+
}
8999
}

0 commit comments

Comments
 (0)