Skip to content

Commit

Permalink
Merge pull request #8 from answear/get-sorted-working-hours
Browse files Browse the repository at this point in the history
Add utility method to get working hours sorted by day
  • Loading branch information
malarzm authored Oct 14, 2021
2 parents 54069f5 + 43002ef commit 2b9c357
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/Response/DTO/ParcelShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ public function getCoordinates(): ?Coordinates
: null;
}

/**
* @return WorkingHours[]
*/
public function getSortedWorkingHours(): array
{
$copy = $this->workingHours;
usort(
$copy,
static function (WorkingHours $a, WorkingHours $b): int {
if (null === $a->type && null === $b->type) {
return 0;
}
if (null === $a->type) {
return -1;
}
if (null === $b->type) {
return 1;
}
if ($a->type->is($b->type)) {
return $a->from <=> $b->from;
}

return $a->type->getOrdinal() <=> $b->type->getOrdinal();
}
);

return $copy;
}

public function setAddress(Address $address): void
{
$this->address = $address;
Expand Down
2 changes: 1 addition & 1 deletion src/Response/DTO/WorkingHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class WorkingHours
{
public ?DayType $type;
public ?DayType $type = null;
public string $typeName;
public ?string $from;
public ?string $until;
Expand Down
75 changes: 75 additions & 0 deletions tests/Unit/Response/DTO/ParcelShopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Answear\OverseasBundle\Tests\Unit\Response\DTO;

use Answear\OverseasBundle\Enum\DayType;
use Answear\OverseasBundle\Response\DTO\ParcelShop;
use Answear\OverseasBundle\Response\DTO\WorkingHours;
use PHPUnit\Framework\TestCase;

class ParcelShopTest extends TestCase
{
/**
* @dataProvider provideWorkingHoursSortedCorrectly
* @test
*/
public function workingHoursSortedCorrectly(array $workingHours, array $expected): void
{
$shop = new ParcelShop();
$shop->workingHours = $workingHours;
self::assertSame($expected, $shop->getSortedWorkingHours());
}

public function provideWorkingHoursSortedCorrectly(): iterable
{
$nulledType = new WorkingHours();
$workingDays = $this->createWorkingHours(DayType::workingDay());
$monday = $this->createWorkingHours(DayType::monday());
$tuesday = $this->createWorkingHours(DayType::tuesday());
$wednesday = $this->createWorkingHours(DayType::wednesday());
$thursday = $this->createWorkingHours(DayType::thursday());
$friday = $this->createWorkingHours(DayType::friday());
$saturday = $this->createWorkingHours(DayType::saturday());
$sunday = $this->createWorkingHours(DayType::sunday());

yield 'with working days, already sorted' => [
[$workingDays, $saturday, $sunday],
[$workingDays, $saturday, $sunday],
];
yield 'with working days, scrambled' => [
[$sunday, $workingDays, $saturday],
[$workingDays, $saturday, $sunday],
];
yield 'full week, sorted' => [
[$monday, $tuesday, $wednesday, $thursday, $friday, $saturday, $sunday],
[$monday, $tuesday, $wednesday, $thursday, $friday, $saturday, $sunday],
];
yield 'full week, scrambled' => [
[$saturday, $tuesday, $monday, $wednesday, $friday, $thursday, $sunday],
[$monday, $tuesday, $wednesday, $thursday, $friday, $saturday, $sunday],
];
yield 'nulled type is first' => [
[$workingDays, $saturday, $sunday, $nulledType],
[$nulledType, $workingDays, $saturday, $sunday],
];

$saturdayFirstPart = $this->createWorkingHours(DayType::saturday(), '09:00', '11:00');
$saturdaySecondPart = $this->createWorkingHours(DayType::saturday(), '14:00', '16:00');
yield 'day parts' => [
[$saturdaySecondPart, $workingDays, $saturdayFirstPart, $sunday],
[$workingDays, $saturdayFirstPart, $saturdaySecondPart, $sunday],
];
}

private function createWorkingHours(DayType $dayType, string $from = '09:00', string $until = '17:00'): WorkingHours
{
$workingHours = new WorkingHours();
$workingHours->setType($dayType);
$workingHours->from = $from;
$workingHours->until = $until;

return $workingHours;
}
}

0 comments on commit 2b9c357

Please sign in to comment.