-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from mvantwillert/master
Add SortOrder class to allow missing to be set for sorting
- Loading branch information
Showing
6 changed files
with
189 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\Syntax; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
class SortOrder | ||
{ | ||
public const ASCENDING = 'asc'; | ||
|
||
public const DESCENDING = 'desc'; | ||
|
||
public const MISSING_FIRST = '_first'; | ||
|
||
public const MISSING_LAST = '_last'; | ||
|
||
private string $order; | ||
|
||
private ?string $missing; | ||
|
||
private function __construct(string $order, ?string $missing) | ||
{ | ||
$this->order = $order; | ||
$this->missing = $missing; | ||
Assert::inArray($order, [self::ASCENDING, self::DESCENDING]); | ||
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); | ||
} | ||
|
||
public function build(): array|string | ||
{ | ||
if (is_null($this->missing)) { | ||
return $this->order; | ||
} | ||
|
||
return [ | ||
'missing' => $this->missing, | ||
'order' => $this->order | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Tests\Unit\Domain\Syntax; | ||
|
||
use Illuminate\Testing\Assert; | ||
use JeroenG\Explorer\Domain\Syntax\SortOrder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SortOrderTest extends TestCase | ||
{ | ||
public function test_it_uses_default_missing_when_creating_sort_order(): void | ||
{ | ||
$sort = SortOrder::for(SortOrder::DESCENDING); | ||
|
||
Assert::assertSame([ | ||
'missing' => 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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters