|
| 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 | +} |
0 commit comments