Provide start and end as DateTimeImmutable in DateDemand

This commit is contained in:
Daniel Siepmann 2022-07-12 07:52:44 +02:00
parent 7e604504a3
commit 69ac00823b
3 changed files with 176 additions and 8 deletions

View file

@ -63,14 +63,14 @@ class DateDemand
protected $limit = ''; protected $limit = '';
/** /**
* @var int|null * @var null|\DateTimeImmutable
*/ */
protected $start = null; protected $startObject = null;
/** /**
* @var int|null * @var null|\DateTimeImmutable
*/ */
protected $end = null; protected $endObject = null;
/** /**
* @var bool * @var bool
@ -315,24 +315,58 @@ class DateDemand
return $this->synonyms[$searchWord] ?? []; return $this->synonyms[$searchWord] ?? [];
} }
public function getStartObject(): ?\DateTimeImmutable
{
return $this->startObject;
}
public function getStart(): ?int public function getStart(): ?int
{ {
return $this->start; if ($this->getStartObject() === null) {
return null;
}
return (int) $this->getStartObject()->format('U');
} }
public function setStart(?int $start): void public function setStart(?int $start): void
{ {
$this->start = $start; if ($start === null) {
return;
}
$this->startObject = new \DateTimeImmutable(date('Y-m-d H:i', $start));
}
public function getEndObject(): ?\DateTimeImmutable
{
return $this->endObject;
}
public function getEndsOnSameDay(): bool
{
if ($this->getStartObject() === null || $this->getEndObject() === null) {
return true;
}
return $this->getStartObject()->format('Y-m-d') === $this->getEndObject()->format('Y-m-d');
} }
public function getEnd(): ?int public function getEnd(): ?int
{ {
return $this->end; if ($this->getEndObject() === null) {
return null;
}
return (int) $this->getEndObject()->format('U');
} }
public function setEnd(?int $end): void public function setEnd(?int $end): void
{ {
$this->end = $end; if ($end === null) {
return;
}
$this->endObject = new \DateTimeImmutable(date('Y-m-d H:i', $end));
} }
public function setUseMidnight(bool $useMidnight): void public function setUseMidnight(bool $useMidnight): void

View file

@ -44,6 +44,9 @@ Features
This might be used to add further variables or to alter variables, e.g. hide some This might be used to add further variables or to alter variables, e.g. hide some
categories, group dates, etc. categories, group dates, etc.
* Provide start and end as ``\DateTimeImmutable`` instances within ``DateDemand``.
This allows to respect time zone and usage of ``f:format.date`` ViewHelper.
Fixes Fixes
----- -----

View file

@ -149,4 +149,135 @@ class DateDemandTest extends TestCase
$result->getRegions() $result->getRegions()
); );
} }
/**
* @test
*/
public function startIsSetByRequest(): void
{
$result = DateDemand::createFromRequestValues(
[
'start' => '2022-07-12',
],
[
]
);
self::assertInstanceOf(
\DateTimeImmutable::class,
$result->getStartObject()
);
self::assertSame(
'2022-07-12',
$result->getStartObject()->format('Y-m-d')
);
self::assertSame(
1657576800,
$result->getStart()
);
}
/**
* @test
*/
public function endIsSetByRequest(): void
{
$result = DateDemand::createFromRequestValues(
[
'end' => '2022-07-12',
],
[
]
);
self::assertInstanceOf(
\DateTimeImmutable::class,
$result->getEndObject()
);
self::assertSame(
'2022-07-12',
$result->getEndObject()->format('Y-m-d')
);
self::assertSame(
1657663140,
$result->getEnd()
);
}
/**
* @test
* @dataProvider possibleEndAndStartNullCombinations
*/
public function returnsEndsOnSameDayIfAnyIsNull(
string $start,
string $end
): void {
$result = DateDemand::createFromRequestValues(
[
'start' => $start,
'end' => $end,
],
[
]
);
self::assertTrue(
$result->getEndsOnSameDay()
);
}
public function possibleEndAndStartNullCombinations(): \Generator
{
yield 'Both are empty' => [
'start' => '',
'end' => '',
];
yield 'Start is empty' => [
'start' => '',
'end' => '2022-07-12',
];
yield 'End is empty' => [
'start' => '2022-07-12',
'end' => '',
];
}
/**
* @test
*/
public function returnsEndsOnSameDayIfBothAreOnSameDay(): void
{
$result = DateDemand::createFromRequestValues(
[
'start' => '2022-07-12',
'end' => '2022-07-12',
],
[
]
);
self::assertTrue(
$result->getEndsOnSameDay()
);
}
/**
* @test
*/
public function returnsEndsOnSameDayIfBothAreOnDifferentDays(): void
{
$result = DateDemand::createFromRequestValues(
[
'start' => '2022-07-12',
'end' => '2022-07-13',
],
[
]
);
self::assertFalse(
$result->getEndsOnSameDay()
);
}
} }