summaryrefslogtreecommitdiff
path: root/src/Support/SortDirections.php
diff options
context:
space:
mode:
authorSam Light <samlight1994@gmail.com>2026-07-27 22:28:18 +0100
committerSam Light <samlight1994@gmail.com>2026-07-27 22:28:18 +0100
commit1dc1088933c906d4676056d53395d34ce0a68d01 (patch)
treeb3c39b1e58305e2b0985678b720991883372667f /src/Support/SortDirections.php
parent827a53363e7a03329d6e5972d9d9be99d17c889e (diff)
changed to use sort directionHEADv3.0.0master
Diffstat (limited to 'src/Support/SortDirections.php')
-rw-r--r--src/Support/SortDirections.php44
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,
+ };
+ }
+}