Final Class Yiisoft\Data\Reader\OrderHelper
| Inheritance | Yiisoft\Data\Reader\OrderHelper |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| arrayToString() | Create an order string based on a logical field order array. | Yiisoft\Data\Reader\OrderHelper |
| stringToArray() | Create a field order array from an order string. | Yiisoft\Data\Reader\OrderHelper |
Method Details
Create an order string based on a logical field 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.
| public static string arrayToString ( array $order ) | ||
| $order | array |
Logical fields order as an 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);
}
Create a field 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.
| public static array stringToArray ( string $orderString ) | ||
| $orderString | string |
Logical fields order as comma-separated string. |
| return | array |
Logical fields order as an array. |
|---|---|---|
public static function stringToArray(string $orderString): array
{
$order = [];
/**
* @var string[] $parts We use correct regexp here, so `preg_split` never returns false.
*/
$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;
}
Signup or Login in order to comment.