Skip to content

Commit 1493a22

Browse files
committed
Add ChronosPeriod which returns Chronos instances from DatePeriod
1 parent 5b520fb commit 1493a22

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

src/ChronosPeriod.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
class ChronosPeriod implements Iterator
21+
{
22+
/**
23+
* @var \Iterator
24+
*/
25+
protected Iterator $iterator;
26+
27+
/**
28+
* @param \DatePeriod $period
29+
*/
30+
public function __construct(DatePeriod $period)
31+
{
32+
$this->iterator = $period->getIterator();
33+
}
34+
35+
/**
36+
* @return \Cake\Chronos\Chronos
37+
*/
38+
public function current(): Chronos
39+
{
40+
return new Chronos($this->iterator->current());
41+
}
42+
43+
/**
44+
* @return mixed
45+
*/
46+
public function key(): mixed
47+
{
48+
return $this->iterator->key();
49+
}
50+
51+
/**
52+
* @return void
53+
*/
54+
public function next(): void
55+
{
56+
$this->iterator->next();
57+
}
58+
59+
/**
60+
* @return void
61+
*/
62+
public function rewind(): void
63+
{
64+
$this->iterator->rewind();
65+
}
66+
67+
/**
68+
* @return bool
69+
*/
70+
public function valid(): bool
71+
{
72+
return $this->iterator->valid();
73+
}
74+
}
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)