From b0e39e8ce78e30ea079f0bef560a11b1d72d327d Mon Sep 17 00:00:00 2001 From: Joe Siebert Date: Sun, 27 Sep 2020 14:46:59 -0600 Subject: [PATCH] updating recursionreader to use carbon api --- src/Helpers/RecursionReader.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Helpers/RecursionReader.php b/src/Helpers/RecursionReader.php index 1499846..d70f9c3 100644 --- a/src/Helpers/RecursionReader.php +++ b/src/Helpers/RecursionReader.php @@ -10,7 +10,7 @@ class RecursionReader { use Injectable; - + const DAY_SECONDS = 86400; // Seconds in a day const WEEK_SECONDS = 604800; // Seconds in a week @@ -38,7 +38,7 @@ class RecursionReader * @var array */ protected $allowedDaysOfWeek = []; - + /** * @var array */ @@ -65,7 +65,7 @@ public function __construct(CalendarEvent $event) $this->datetimeClass = $cal->getDateTimeClass(); $this->eventClass = $cal->getEventClass(); $relation = $cal->getDateToEventRelation(); - + if ($datetime = DataList::create($this->datetimeClass) ->filter($relation, $event->ID)->first() ) { @@ -85,7 +85,7 @@ public function __construct(CalendarEvent $event) } } } - + if ($exceptions = $event->getComponents('Exceptions')) { foreach ($exceptions as $exception) { $this->exceptions[] = $exception->ExceptionDate; @@ -99,12 +99,13 @@ public function __construct(CalendarEvent $event) */ public function recursionHappensOn($ts) { - $testDate = Carbon::createFromTimestamp($ts); + $originalTestDate = Carbon::createFromTimestamp($ts); + $testDate = $originalTestDate->copy(); $startDate = Carbon::createFromTimestamp($this->ts); $result = false; - + // Current date is before the recurring event begins. - if ($testDate->getTimestamp() < $startDate->getTimestamp() + if ($testDate->getTimestamp() < $startDate->getTimestamp() || in_array($testDate->toDateString(), $this->exceptions) ) { return $result; @@ -124,15 +125,15 @@ public function recursionHappensOn($ts) // Weekly case CalendarEvent::RECUR_INTERVAL_WEEKLY: $testFirstDay = clone $testDate; - $testFirstDay->modify(($testFirstDay->format('l') == 'Sunday') - ? 'Monday last week' + $testFirstDay->modify(($testFirstDay->format('l') == 'Sunday') + ? 'Monday last week' : 'Monday this week' ); if ((($testFirstDay->getTimestamp() - $startDate->startOfWeek()->getTimestamp()) / self::WEEK_SECONDS) % $this->event->WeeklyInterval == 0 && in_array($testDate->format('w'), $this->allowedDaysOfWeek) ) { $result = true; - }; + }; break; // Monthly @@ -142,7 +143,7 @@ public function recursionHappensOn($ts) if ($this->event->MonthlyRecursionType1 == 1) { // A given set of dates in the month e.g. 2 and 15. - if (in_array($testDate->reset()->format('j'), $this->allowedDaysOfMonth)) { + if (in_array($originalTestDate->format('j'), $this->allowedDaysOfMonth)) { $result = true; } @@ -151,15 +152,15 @@ public function recursionHappensOn($ts) // e.g. "First Monday of the month" if ($this->event->MonthlyIndex == 5) { // Last day of the month? - $targetDate = $testDate->addMonth()->startOfMonth()->previous($this->event->MonthlyDayOfWeek)->dump(); + $targetDate = $testDate->addMonth()->startOfMonth()->previous($this->event->MonthlyDayOfWeek)->format('Y-m-d H:i:s'); } else { $testDate->modify("last day of previous month"); for ($i = 0; $i < $this->event->MonthlyIndex; $i++) { - $testDate->next($this->event->MonthlyDayOfWeek)->dump(); + $testDate->next($this->event->MonthlyDayOfWeek)->format('Y-m-d H:i:s'); } - $targetDate = $testDate->dump(); + $targetDate = $testDate->format('Y-m-d H:i:s'); } - return $testDate->reset()->dump() == $targetDate; + return $originalTestDate->format('Y-m-d H:i:s') == $targetDate; } } break;