summaryrefslogtreecommitdiff
path: root/src/Support/SortDirections.php
diff options
context:
space:
mode:
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,
+ };
+ }
+}