Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ parameters:
message: "#with generic class DatePeriod but does not specify its types: TDate, TEnd, TRecurrences$#"
count: 1
path: src/ChronosPeriod.php
-
message: "#with generic class DatePeriod but does not specify its types: TDate, TEnd, TRecurrences$#"
count: 1
path: src/ChronosDatePeriod.php
83 changes: 83 additions & 0 deletions src/ChronosDatePeriod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);

/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\Chronos;

use DatePeriod;
use Iterator;

/**
* DatePeriod wrapper that returns Chronos instances.
*
* @template TKey int
* @template TValue \Cake\Chronos\ChronosDate
* @template-implements \Iterator<int, \Cake\Chronos\ChronosDate>
*/
class ChronosDatePeriod implements Iterator
{
/**
* @var \Iterator<int, \DateTimeInterface>
*/
protected Iterator $iterator;

/**
* @param \DatePeriod $period
*/
public function __construct(DatePeriod $period)
{
/** @var \Iterator<int, \DateTimeInterface> $iterator */
$iterator = $period->getIterator();
$this->iterator = $iterator;
}

/**
* @return \Cake\Chronos\ChronosDate
*/
public function current(): ChronosDate
{
return new ChronosDate($this->iterator->current());
}

/**
* @return int
*/
public function key(): int
{
return $this->iterator->key();
}

/**
* @return void
*/
public function next(): void
{
$this->iterator->next();
}

/**
* @return void
*/
public function rewind(): void
{
$this->iterator->rewind();
}

/**
* @return bool
*/
public function valid(): bool
{
return $this->iterator->valid();
}
}
40 changes: 40 additions & 0 deletions tests/TestCase/ChronosDatePeriodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\Chronos\Test\TestCase;

use Cake\Chronos\ChronosDate;
use Cake\Chronos\ChronosDatePeriod;
use DateInterval;
use DatePeriod;
use DateTime;

class ChronosDatePeriodTest extends TestCase
{
public function testChronosPeriod(): void
{
$period = new ChronosDatePeriod(new DatePeriod(new DateTime('2025-01-01 00:00:00'), new DateInterval('P1D'), 3));
$output = [];
foreach ($period as $key => $value) {
$output[$key] = $value;
}
$this->assertCount(4, $output);
$this->assertInstanceOf(ChronosDAte::class, $output[0]);
$this->assertSame('2025-01-01 00:00:00', $output[0]->format('Y-m-d H:i:s'));
$this->assertInstanceOf(ChronosDate::class, $output[1]);
$this->assertSame('2025-01-02 00:00:00', $output[1]->format('Y-m-d H:i:s'));
$this->assertInstanceOf(ChronosDate::class, $output[3]);
$this->assertSame('2025-01-04 00:00:00', $output[3]->format('Y-m-d H:i:s'));
}
}