Skip to content

Commit ee38f12

Browse files
committed
Add ChronosPeriod which returns Chronos instances from DatePeriod
1 parent 0eed977 commit ee38f12

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ parameters:
1313
message: "#^Call to an undefined static method DateTimeImmutable\\:\\:createFromTimestamp\\(\\)\\.$#"
1414
count: 1
1515
path: src/Chronos.php
16+
-
17+
message: "#with generic class DatePeriod but does not specify its types: TDate, TEnd, TRecurrences$#"
18+
count: 1
19+
path: src/ChronosPeriod.php

src/ChronosPeriod.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
15+
namespace Cake\Chronos;
16+
17+
use DatePeriod;
18+
use Iterator;
19+
20+
/**
21+
* DatePeriod wrapper that returns Chronos instances.
22+
*
23+
* @template TKey int
24+
* @template TValue \Cake\Chronos\Chronos
25+
* @template-implements \Iterator<int, \Cake\Chronos\Chronos>
26+
*/
27+
class ChronosPeriod implements Iterator
28+
{
29+
/**
30+
* @var \Iterator<int, \DateTimeInterface>
31+
*/
32+
protected Iterator $iterator;
33+
34+
/**
35+
* @param \DatePeriod $period
36+
*/
37+
public function __construct(DatePeriod $period)
38+
{
39+
/** @var \Iterator<int, \DateTimeInterface> $iterator */
40+
$iterator = $period->getIterator();
41+
$this->iterator = $iterator;
42+
}
43+
44+
/**
45+
* @return \Cake\Chronos\Chronos
46+
*/
47+
public function current(): Chronos
48+
{
49+
return new Chronos($this->iterator->current());
50+
}
51+
52+
/**
53+
* @return int
54+
*/
55+
public function key(): int
56+
{
57+
return $this->iterator->key();
58+
}
59+
60+
/**
61+
* @return void
62+
*/
63+
public function next(): void
64+
{
65+
$this->iterator->next();
66+
}
67+
68+
/**
69+
* @return void
70+
*/
71+
public function rewind(): void
72+
{
73+
$this->iterator->rewind();
74+
}
75+
76+
/**
77+
* @return bool
78+
*/
79+
public function valid(): bool
80+
{
81+
return $this->iterator->valid();
82+
}
83+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6+
*
7+
* Licensed under The MIT License
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
15+
namespace Cake\Chronos\Test\TestCase;
16+
17+
use Cake\Chronos\Chronos;
18+
use Cake\Chronos\ChronosPeriod;
19+
use DateInterval;
20+
use DatePeriod;
21+
use DateTime;
22+
23+
class ChronosPeriodTest extends TestCase
24+
{
25+
public function testChronosPeriod(): void
26+
{
27+
$period = new ChronosPeriod(new DatePeriod(new DateTime('2025-01-01 00:00:00'), new DateInterval('PT1H'), 3));
28+
$output = [];
29+
foreach ($period as $key => $value) {
30+
$output[$key] = $value;
31+
}
32+
$this->assertCount(4, $output);
33+
$this->assertInstanceOf(Chronos::class, $output[0]);
34+
$this->assertSame('2025-01-01 00:00:00', $output[0]->format('Y-m-d H:i:s'));
35+
$this->assertInstanceOf(Chronos::class, $output[1]);
36+
$this->assertSame('2025-01-01 01:00:00', $output[1]->format('Y-m-d H:i:s'));
37+
}
38+
}

0 commit comments

Comments
 (0)