Skip to content

Commit 89b7fc1

Browse files
author
Stephan Wentz
committed
Add period range, update to php 8.5
1 parent 30bf8f3 commit 89b7fc1

8 files changed

Lines changed: 79 additions & 19 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
pull_request:
77
push:
88
branches:
9-
- "master"
9+
- "main"
1010

1111
jobs:
1212
tests:

.github/workflows/code_coverage.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request: null
55
push:
66
branches:
7-
- master
7+
- main
88

99
jobs:
1010
code_coverage:

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: "Release"
55
on:
66
push:
77
branches:
8-
- "master"
8+
- "main"
99

1010
jobs:
1111
tests:

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^8.3",
14+
"php": "^8.5",
1515
"ext-date": "*",
1616
"psr/clock": "^1.0"
1717
},
@@ -20,8 +20,8 @@
2020
"ergebnis/phpstan-rules": "^2.0",
2121
"lcobucci/clock": "^3.0",
2222
"phpstan/phpstan": "^2.0",
23-
"phpunit/phpunit": "^12.0",
24-
"symfony/serializer": "^6.0|^7.0",
23+
"phpunit/phpunit": "^13.0",
24+
"symfony/serializer": "^6.0|^7.0|^8.0",
2525
"twig/twig": "^3.0",
2626
"roave/backward-compatibility-check": "^8.2"
2727
},

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ parameters:
55
- src
66
- tests
77
ergebnis:
8+
noPhpstanIgnore:
9+
enabled: false
810
noNullableReturnTypeDeclaration:
911
enabled: false
1012
noParameterWithNullableTypeDeclaration:

src/Exception/MismatchingPeriods.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public static function differ(Period $period, Period $otherPeriod): self
1919
$otherPeriod::class,
2020
));
2121
}
22+
23+
public static function empty(): self
24+
{
25+
return new self('Empty periods are not allowed.');
26+
}
2227
}

src/PeriodFactory.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Brainbits\Period\Exception\InvalidPeriodIdentifier;
88
use Brainbits\Period\Exception\MismatchingPeriods;
99
use DateTimeImmutable;
10-
use Generator;
1110
use Psr\Clock\ClockInterface;
1211

1312
use function sprintf;
@@ -213,24 +212,21 @@ public function fromIdentifier(string $periodIdentifier): Period
213212
};
214213
}
215214

216-
/**
217-
* @param T $period
218-
* @param T $toPeriod
219-
*
220-
* @return Generator<T>
221-
*
222-
* @template T of Period
223-
*/
224-
public function fromTo(Period $period, Period $toPeriod): Generator
215+
public function fromTo(Period $fromPeriod, Period $toPeriod): PeriodRange
225216
{
226-
if ($period::class !== $toPeriod::class) {
227-
throw MismatchingPeriods::differ($period, $toPeriod);
217+
if ($fromPeriod::class !== $toPeriod::class) {
218+
throw MismatchingPeriods::differ($fromPeriod, $toPeriod);
228219
}
229220

221+
$period = $fromPeriod;
222+
$periods = [];
223+
230224
do {
231-
yield $period;
225+
$periods[] = $period;
232226

233227
$period = $this->next($period);
234228
} while ($period->getPeriodString() < $toPeriod->getPeriodString());
229+
230+
return new PeriodRange(...$periods);
235231
}
236232
}

src/PeriodRange.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\Period;
6+
7+
use Brainbits\Period\Exception\MismatchingPeriods;
8+
use IteratorAggregate;
9+
use Traversable;
10+
11+
use function array_first;
12+
use function array_last;
13+
use function array_values;
14+
15+
/** @implements IteratorAggregate<Period> */
16+
final class PeriodRange implements IteratorAggregate
17+
{
18+
/** @var non-empty-list<Period> */
19+
private array $periods;
20+
21+
public function __construct(Period ...$periods)
22+
{
23+
if (!$periods) {
24+
throw MismatchingPeriods::empty();
25+
}
26+
27+
$firstPeriod = null;
28+
foreach ($periods as $period) {
29+
if ($firstPeriod === null) {
30+
$firstPeriod = $period;
31+
continue;
32+
}
33+
34+
if ($firstPeriod::class !== $period::class) {
35+
throw MismatchingPeriods::differ($firstPeriod, $period);
36+
}
37+
}
38+
39+
$this->periods = array_values($periods);
40+
}
41+
42+
public function startPeriod(): Period
43+
{
44+
return array_first($this->periods);
45+
}
46+
47+
public function endPeriod(): Period
48+
{
49+
return array_last($this->periods);
50+
}
51+
52+
/** @return Traversable<Period> */
53+
public function getIterator(): Traversable
54+
{
55+
yield from $this->periods;
56+
}
57+
}

0 commit comments

Comments
 (0)