diff options
| author | Sam Light <samlight1994@gmail.com> | 2026-07-27 22:28:18 +0100 |
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2026-07-27 22:28:18 +0100 |
| commit | 1dc1088933c906d4676056d53395d34ce0a68d01 (patch) | |
| tree | b3c39b1e58305e2b0985678b720991883372667f /src/Support | |
| parent | 827a53363e7a03329d6e5972d9d9be99d17c889e (diff) | |
Diffstat (limited to 'src/Support')
| -rw-r--r-- | src/Support/SortDirections.php | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Support/SortDirections.php b/src/Support/SortDirections.php new file mode 100644 index 0000000..def0f53 --- /dev/null +++ b/src/Support/SortDirections.php @@ -0,0 +1,44 @@ +<?php + +declare(strict_types=1); + +namespace Lightscale\LaralightTables\Support; + +use SortDirection; + +/** + * Converts between SortDirection and the 'asc'/'desc' strings used by the + * query builder and the Livewire wire/URL boundary. + * + * SortDirection is a pure enum, so it carries no value of its own. + */ +final class SortDirections +{ + public const ASC = 'asc'; + + public const DESC = 'desc'; + + /** + * @return 'asc'|'desc' + */ + public static function value(SortDirection $dir): string + { + return match ($dir) { + SortDirection::Ascending => self::ASC, + SortDirection::Descending => self::DESC, + }; + } + + public static function tryFrom(SortDirection|string|null $value): ?SortDirection + { + if ($value instanceof SortDirection) { + return $value; + } + + return match ($value === null ? null : strtolower($value)) { + self::ASC => SortDirection::Ascending, + self::DESC => SortDirection::Descending, + default => null, + }; + } +} |
