From b5b7792b17abc24ac5c7165d8c61a24d0dd442f0 Mon Sep 17 00:00:00 2001 From: Sam Hauglustaine Date: Mon, 26 Jun 2023 21:24:21 +0200 Subject: [PATCH] fix DatePeriod use on old php --- src/Timespan/Collection.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Timespan/Collection.php b/src/Timespan/Collection.php index 66d4ca6..16a6b71 100644 --- a/src/Timespan/Collection.php +++ b/src/Timespan/Collection.php @@ -2,6 +2,7 @@ namespace Timespan; use \DatePeriod; +use \IteratorAggregate; class Collection extends \ArrayObject { @@ -12,11 +13,18 @@ class Collection extends \ArrayObject public function __construct($mixed = null) { if ($mixed instanceof DatePeriod) { + $iterator = $mixed; + + if ($iterator instanceof IteratorAggregate) { + $iterator = $mixed->getIterator(); + } + $previousDate = null; - foreach ($mixed->getIterator() as $date) { + foreach ($iterator as $date) { if ($previousDate) { $this[] = new Timespan($previousDate, $date); } + $previousDate = $date; } } elseif (is_array($mixed)) {