From 478013a83eb31517caf88138d7335baf1d7ebbc9 Mon Sep 17 00:00:00 2001 From: Melvin van Twillert Date: Wed, 14 Feb 2024 13:37:57 +0100 Subject: [PATCH] Type order variable to SortOrder --- src/Domain/Syntax/Sort.php | 20 ++++--- src/Domain/Syntax/SortOrder.php | 19 +++++-- .../Query/QueryProperties/SortingTest.php | 14 ++--- tests/Unit/Domain/Syntax/SortOrderTest.php | 54 +++++++++++++++++++ 4 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 tests/Unit/Domain/Syntax/SortOrderTest.php diff --git a/src/Domain/Syntax/Sort.php b/src/Domain/Syntax/Sort.php index 16a1221..6d47f5d 100644 --- a/src/Domain/Syntax/Sort.php +++ b/src/Domain/Syntax/Sort.php @@ -4,33 +4,31 @@ namespace JeroenG\Explorer\Domain\Syntax; -use Webmozart\Assert\Assert; - class Sort { + /** @deprecated Use SortOrder::ASCENDING instead */ public const ASCENDING = 'asc'; - + + /** @deprecated Use SortOrder::DESCENDING instead */ public const DESCENDING = 'desc'; private string $field; - private string|array $order; + private SortOrder $order; - public function __construct(string $field, string|SortOrder $order = self::ASCENDING) + public function __construct(string $field, string|SortOrder $order = SortOrder::ASCENDING) { - $this->field = $field; - if (is_string($order)){ - $this->order = $order; - Assert::inArray($order, [self::ASCENDING, self::DESCENDING]); + if (is_string($order)) { + $this->order = SortOrder::fromString($order); } else { - $this->order = $order->asArray(); + $this->order = $order; } } public function build(): array { - return [$this->field => $this->order]; + return [$this->field => $this->order->build()]; } } diff --git a/src/Domain/Syntax/SortOrder.php b/src/Domain/Syntax/SortOrder.php index 8a5c9d8..b5ec807 100644 --- a/src/Domain/Syntax/SortOrder.php +++ b/src/Domain/Syntax/SortOrder.php @@ -18,23 +18,32 @@ class SortOrder private string $order; - private string $missing; + private ?string $missing; - private function __construct(string $order, string $missing ) + private function __construct(string $order, ?string $missing) { $this->order = $order; $this->missing = $missing; Assert::inArray($order, [self::ASCENDING, self::DESCENDING]); - Assert::inArray($missing, [self::MISSING_FIRST, self::MISSING_LAST]); + Assert::nullOrInArray($missing, [self::MISSING_FIRST, self::MISSING_LAST]); + } + + public static function fromString(string $order): self + { + return new self($order, null); } public static function for(string $order = self::ASCENDING, string $missing = self::MISSING_LAST): self { - return new self($order,$missing); + return new self($order, $missing); } - public function asArray(): array + public function build(): array|string { + if (is_null($this->missing)) { + return $this->order; + } + return [ 'missing' => $this->missing, 'order' => $this->order diff --git a/tests/Unit/Domain/Query/QueryProperties/SortingTest.php b/tests/Unit/Domain/Query/QueryProperties/SortingTest.php index cb3bd89..d905545 100644 --- a/tests/Unit/Domain/Query/QueryProperties/SortingTest.php +++ b/tests/Unit/Domain/Query/QueryProperties/SortingTest.php @@ -14,7 +14,7 @@ final class SortingTest extends TestCase public function test_it_builds_sorting(): void { $sort = Sorting::for( - new Sort(':fld:', Sort::DESCENDING), + new Sort(':fld:', SortOrder::DESCENDING), ); self::assertSame([ 'sort' => [ [ ':fld:' => 'desc' ]]],$sort->build()); @@ -32,16 +32,16 @@ public function test_it_builds_sorting_from_sort_order(): void public function test_it_combines(): void { $a = Sorting::for( - new Sort(':fld1:', Sort::DESCENDING), - new Sort(':fld2:', Sort::DESCENDING), + new Sort(':fld1:', SortOrder::DESCENDING), + new Sort(':fld2:', SortOrder::DESCENDING), ); $b = Sorting::for( - new Sort(':fld3:', Sort::DESCENDING), - new Sort(':fld4:', Sort::DESCENDING), + new Sort(':fld3:', SortOrder::DESCENDING), + new Sort(':fld4:', SortOrder::DESCENDING), ); $c = Sorting::for( - new Sort(':fld5:', Sort::DESCENDING), - new Sort(':fld6:', SortOrder::for(SortOrder::DESCENDING, SortOrder::MISSING_LAST)), + new Sort(':fld5:', SortOrder::DESCENDING), + new Sort(':fld6:', SortOrder::for(SortOrder::DESCENDING)), ); $d = Sorting::for( new Sort(':fld7:', SortOrder::for(SortOrder::DESCENDING, SortOrder::MISSING_FIRST)), diff --git a/tests/Unit/Domain/Syntax/SortOrderTest.php b/tests/Unit/Domain/Syntax/SortOrderTest.php new file mode 100644 index 0000000..b6e7a64 --- /dev/null +++ b/tests/Unit/Domain/Syntax/SortOrderTest.php @@ -0,0 +1,54 @@ + SortOrder::MISSING_LAST, + 'order' => SortOrder::DESCENDING + ], $sort->build()); + } + + /** + * @dataProvider provideSortOrderStrings + */ + public function test_sort_order_can_be_created_from_sort_string(string $expectedResult, string $sortString): void + { + $subject = SortOrder::fromString($sortString); + Assert::assertSame($expectedResult, $subject->build()); + } + + /** + * @dataProvider provideMissingSortOrderStrings + */ + public function test_sort_order_can_be_created_from_sort_string_and_missing(array $expectedResult, string $sortString, string $missing): void + { + $subject = SortOrder::for($sortString, $missing); + Assert::assertSame($expectedResult, $subject->build()); + } + + public function provideSortOrderStrings(): iterable + { + yield 'asc' => ['asc', 'asc']; + yield 'desc' => ['desc', 'desc']; + } + + public function provideMissingSortOrderStrings(): iterable + { + yield 'asc order with _last missing' => [['missing' => '_last', 'order' => 'asc'], 'asc', '_last']; + yield 'desc order with _last missing' => [['missing' => '_last', 'order' => 'desc'], 'desc', '_last']; + yield 'asc order with _first missing' => [['missing' => '_first', 'order' => 'asc'], 'asc', '_first']; + yield 'desc order with _first missing' => [['missing' => '_first', 'order' => 'desc'], 'desc', '_first']; + } + +}