Skip to content

Commit b387e71

Browse files
authored
Extract code for low-level work with order from Sort to OrderHelper (#176)
1 parent fc7edca commit b387e71

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- Chg #166: Remove `EqualsEmpty` filter (@vjik)
2727
- Bug #173: Make `Like` filter case-sensitive (@arogachev)
2828
- New #173: Add `ILike` filter (case-insensitive variation of `Like` filter) (@arogachev)
29+
- New #176: Add `OrderHelper` (@vjik)
2930

3031
## 1.0.1 January 25, 2023
3132

src/Reader/OrderHelper.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Reader;
6+
7+
use function implode;
8+
use function preg_split;
9+
use function substr;
10+
use function trim;
11+
12+
/**
13+
* @psalm-import-type TOrder from Sort
14+
*/
15+
final class OrderHelper
16+
{
17+
/**
18+
* Create fields order array from an order string.
19+
*
20+
* The string consists of comma-separated field names.
21+
* If the name is prefixed with `-`, field order is descending.
22+
* Otherwise, the order is ascending.
23+
*
24+
* @param string $orderString Logical fields order as comma-separated string.
25+
*
26+
* @return array Logical fields order as array.
27+
*
28+
* @psalm-return TOrder
29+
*/
30+
public static function stringToArray(string $orderString): array
31+
{
32+
$order = [];
33+
$parts = preg_split('/\s*,\s*/', trim($orderString), -1, PREG_SPLIT_NO_EMPTY);
34+
35+
foreach ($parts as $part) {
36+
if (str_starts_with($part, '-')) {
37+
$order[substr($part, 1)] = 'desc';
38+
} else {
39+
$order[$part] = 'asc';
40+
}
41+
}
42+
43+
return $order;
44+
}
45+
46+
/**
47+
* Create an order string based on logical fields order array.
48+
*
49+
* The string consists of comma-separated field names.
50+
* If the name is prefixed with `-`, field order is descending.
51+
* Otherwise, the order is ascending.
52+
*
53+
* @param array $order Logical fields order as array.
54+
*
55+
* @return string An order string.
56+
*/
57+
public static function arrayToString(array $order): string
58+
{
59+
$parts = [];
60+
foreach ($order as $field => $direction) {
61+
$parts[] = ($direction === 'desc' ? '-' : '') . $field;
62+
}
63+
64+
return implode(',', $parts);
65+
}
66+
}

src/Reader/Sort.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88

99
use function array_key_exists;
1010
use function array_merge;
11-
use function implode;
1211
use function is_array;
1312
use function is_int;
1413
use function is_string;
15-
use function preg_split;
16-
use function substr;
17-
use function trim;
1814

1915
/**
2016
* Sort represents data sorting settings:
@@ -226,18 +222,9 @@ public static function any(array $config = []): self
226222
*/
227223
public function withOrderString(string $orderString): self
228224
{
229-
$order = [];
230-
$parts = preg_split('/\s*,\s*/', trim($orderString), -1, PREG_SPLIT_NO_EMPTY);
231-
232-
foreach ($parts as $part) {
233-
if (str_starts_with($part, '-')) {
234-
$order[substr($part, 1)] = 'desc';
235-
} else {
236-
$order[$part] = 'asc';
237-
}
238-
}
239-
240-
return $this->withOrder($order);
225+
return $this->withOrder(
226+
OrderHelper::stringToArray($orderString)
227+
);
241228
}
242229

243230
/**
@@ -289,13 +276,7 @@ public function getOrder(): array
289276
*/
290277
public function getOrderAsString(): string
291278
{
292-
$parts = [];
293-
294-
foreach ($this->currentOrder as $field => $direction) {
295-
$parts[] = ($direction === 'desc' ? '-' : '') . $field;
296-
}
297-
298-
return implode(',', $parts);
279+
return OrderHelper::arrayToString($this->currentOrder);
299280
}
300281

301282
/**

0 commit comments

Comments
 (0)