Skip to content

Commit 07c3f0c

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

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

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

src/ChronosPeriod.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
$this->iterator = $period->getIterator();
40+
}
41+
42+
/**
43+
* @return \Cake\Chronos\Chronos
44+
*/
45+
public function current(): Chronos
46+
{
47+
return new Chronos($this->iterator->current());
48+
}
49+
50+
/**
51+
* @return int
52+
*/
53+
public function key(): int
54+
{
55+
return $this->iterator->key();
56+
}
57+
58+
/**
59+
* @return void
60+
*/
61+
public function next(): void
62+
{
63+
$this->iterator->next();
64+
}
65+
66+
/**
67+
* @return void
68+
*/
69+
public function rewind(): void
70+
{
71+
$this->iterator->rewind();
72+
}
73+
74+
/**
75+
* @return bool
76+
*/
77+
public function valid(): bool
78+
{
79+
return $this->iterator->valid();
80+
}
81+
}
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)