Skip to content

Commit

Permalink
Type order variable to SortOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
Melvin van Twillert committed Feb 14, 2024
1 parent bcedf05 commit 7ad0364
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
20 changes: 9 additions & 11 deletions src/Domain/Syntax/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
}
}
19 changes: 14 additions & 5 deletions src/Domain/Syntax/SortOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/Domain/Query/QueryProperties/SortingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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)),
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/Domain/Syntax/SortOrderTest.php
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'];
}
}

0 comments on commit 7ad0364

Please sign in to comment.