Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
66 changes: 66 additions & 0 deletions src/Reader/OrderHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader;

use function implode;
use function preg_split;
use function substr;
use function trim;

/**
* @psalm-import-type TOrder from Sort
*/
final class OrderHelper
{
/**
* Create fields order array from an order string.
*
* The string consists of comma-separated field names.
* If the name is prefixed with `-`, field order is descending.
* Otherwise, the order is ascending.
*
* @param string $orderString Logical fields order as comma-separated string.
*
* @return array Logical fields order as array.
*
* @psalm-return TOrder
*/
public static function stringToArray(string $orderString): array
{
$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 $order;
}

/**
* Create an order string based on logical fields order array.
*
* The string consists of comma-separated field names.
* If the name is prefixed with `-`, field order is descending.
* Otherwise, the order is ascending.
*
* @param array $order Logical fields order as array.
*
* @return string An order string.
*/
public static function arrayToString(array $order): string
{
$parts = [];
foreach ($order as $field => $direction) {
$parts[] = ($direction === 'desc' ? '-' : '') . $field;
}

return implode(',', $parts);
}
}
27 changes: 4 additions & 23 deletions src/Reader/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down