From 0c5d46f4bd992f1b2653a84d6f9ea8811f3726b4 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 3 Apr 2024 16:00:15 +0300 Subject: [PATCH 1/3] Add `OrderHelper` --- CHANGELOG.md | 1 + src/Reader/OrderHelper.php | 87 ++++++++++++++++++++++++++++++++++++++ src/Reader/Sort.php | 27 ++---------- 3 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 src/Reader/OrderHelper.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 41a77911..7978b95f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - Chg #166: Remove `EqualsEmpty` filter (@vjik) - Bug #173: Make `Like` filter case-sensitive (@arogachev) - New #173: Add `ILike` filter (case-insensitive variation of `Like` filter) (@arogachev) +- New #176: Add `OrderHelper` (@vjik) ## 1.0.1 January 25, 2023 diff --git a/src/Reader/OrderHelper.php b/src/Reader/OrderHelper.php new file mode 100644 index 00000000..199913a9 --- /dev/null +++ b/src/Reader/OrderHelper.php @@ -0,0 +1,87 @@ + $direction) { + $parts[] = ($direction === 'desc' ? '-' : '') . $field; + } + + return implode(',', $parts); + } + + /** + * Replace field name in logical fields order array. + * + * @param array $order Logical fields order as array. + * @param string $from Field name to replace. + * @param string $to Field name to replace with. + */ + public static function replaceFieldName(array &$order, string $from, string $to): void + { + if ($from === $to) { + return; + } + + if (!isset($order[$from])) { + return; + } + + $order[$to] = $order[$from]; + unset($order[$from]); + } +} diff --git a/src/Reader/Sort.php b/src/Reader/Sort.php index ca5ed950..bf2d5ab7 100644 --- a/src/Reader/Sort.php +++ b/src/Reader/Sort.php @@ -8,13 +8,9 @@ use function array_key_exists; use function array_merge; -use function implode; use function is_array; use function is_int; use function is_string; -use function preg_split; -use function substr; -use function trim; /** * Sort represents data sorting settings: @@ -226,18 +222,9 @@ public static function any(array $config = []): self */ public function withOrderString(string $orderString): self { - $order = []; - $parts = preg_split('/\s*,\s*/', trim($orderString), -1, PREG_SPLIT_NO_EMPTY); - - foreach ($parts as $part) { - if (str_starts_with($part, '-')) { - $order[substr($part, 1)] = 'desc'; - } else { - $order[$part] = 'asc'; - } - } - - return $this->withOrder($order); + return $this->withOrder( + OrderHelper::stringToArray($orderString) + ); } /** @@ -289,13 +276,7 @@ public function getOrder(): array */ public function getOrderAsString(): string { - $parts = []; - - foreach ($this->currentOrder as $field => $direction) { - $parts[] = ($direction === 'desc' ? '-' : '') . $field; - } - - return implode(',', $parts); + return OrderHelper::arrayToString($this->currentOrder); } /** From 222ed592b941a5020a50d1c0520a8c1bc7c5b08f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 3 Apr 2024 16:07:11 +0300 Subject: [PATCH 2/3] improve --- src/Reader/OrderHelper.php | 14 +++++----- tests/Reader/OrderHelperTest.php | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 tests/Reader/OrderHelperTest.php diff --git a/src/Reader/OrderHelper.php b/src/Reader/OrderHelper.php index 199913a9..2fde26cc 100644 --- a/src/Reader/OrderHelper.php +++ b/src/Reader/OrderHelper.php @@ -71,17 +71,17 @@ public static function arrayToString(array $order): string * @param string $from Field name to replace. * @param string $to Field name to replace with. */ - public static function replaceFieldName(array &$order, string $from, string $to): void + public static function replaceFieldName(array $order, string $from, string $to): array { - if ($from === $to) { - return; + if (!isset($order[$from])) { + return $order; } - if (!isset($order[$from])) { - return; + $result = []; + foreach ($order as $field => $direction) { + $result[$field === $from ? $to : $field] = $direction; } - $order[$to] = $order[$from]; - unset($order[$from]); + return $result; } } diff --git a/tests/Reader/OrderHelperTest.php b/tests/Reader/OrderHelperTest.php new file mode 100644 index 00000000..ed37aa37 --- /dev/null +++ b/tests/Reader/OrderHelperTest.php @@ -0,0 +1,48 @@ + [ + ['new-name' => 'asc', 'age' => 'desc'], + ['name' => 'asc', 'age' => 'desc'], + 'name', + 'new-name', + ]; + yield 'not-exist' => [ + ['age' => 'desc', 'name' => 'asc'], + ['age' => 'desc', 'name' => 'asc'], + 'a', + 'b', + ]; + yield 'empty' => [ + [], + [], + 'name', + 'new-name', + ]; + yield 'equal-names' => [ + ['name' => 'asc', 'age' => 'desc'], + ['name' => 'asc', 'age' => 'desc'], + 'name', + 'name', + ]; + } + + #[DataProvider('dataReplaceFieldName')] + public function testReplaceFieldName(array $expected, array $order, string $from, string $to): void + { + $result = OrderHelper::replaceFieldName($order, $from, $to); + + $this->assertSame($expected, $result); + } +} From e267d359537f52b156126f21d0842e4f6132f655 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 3 Apr 2024 16:09:58 +0300 Subject: [PATCH 3/3] fix --- src/Reader/OrderHelper.php | 21 -------------- tests/Reader/OrderHelperTest.php | 48 -------------------------------- 2 files changed, 69 deletions(-) delete mode 100644 tests/Reader/OrderHelperTest.php diff --git a/src/Reader/OrderHelper.php b/src/Reader/OrderHelper.php index 2fde26cc..d35b05d9 100644 --- a/src/Reader/OrderHelper.php +++ b/src/Reader/OrderHelper.php @@ -63,25 +63,4 @@ public static function arrayToString(array $order): string return implode(',', $parts); } - - /** - * Replace field name in logical fields order array. - * - * @param array $order Logical fields order as array. - * @param string $from Field name to replace. - * @param string $to Field name to replace with. - */ - public static function replaceFieldName(array $order, string $from, string $to): array - { - if (!isset($order[$from])) { - return $order; - } - - $result = []; - foreach ($order as $field => $direction) { - $result[$field === $from ? $to : $field] = $direction; - } - - return $result; - } } diff --git a/tests/Reader/OrderHelperTest.php b/tests/Reader/OrderHelperTest.php deleted file mode 100644 index ed37aa37..00000000 --- a/tests/Reader/OrderHelperTest.php +++ /dev/null @@ -1,48 +0,0 @@ - [ - ['new-name' => 'asc', 'age' => 'desc'], - ['name' => 'asc', 'age' => 'desc'], - 'name', - 'new-name', - ]; - yield 'not-exist' => [ - ['age' => 'desc', 'name' => 'asc'], - ['age' => 'desc', 'name' => 'asc'], - 'a', - 'b', - ]; - yield 'empty' => [ - [], - [], - 'name', - 'new-name', - ]; - yield 'equal-names' => [ - ['name' => 'asc', 'age' => 'desc'], - ['name' => 'asc', 'age' => 'desc'], - 'name', - 'name', - ]; - } - - #[DataProvider('dataReplaceFieldName')] - public function testReplaceFieldName(array $expected, array $order, string $from, string $to): void - { - $result = OrderHelper::replaceFieldName($order, $from, $to); - - $this->assertSame($expected, $result); - } -}