From bd980ff615344f989e97d7dc8d3ea8e87d2600fb Mon Sep 17 00:00:00 2001 From: Farid Adam Date: Sun, 5 Jan 2025 20:18:02 -0500 Subject: [PATCH] Add Carbon dependency and implement calculateNextDate method in Interval enum --- composer.json | 1 + src/Enums/Interval.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/composer.json b/composer.json index c716a5d..eb8023a 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "require": { "php": "^8.0", "guzzlehttp/guzzle": "^7.0", + "nesbot/carbon": "^3.8.4", "ext-json": "*" }, "require-dev": { diff --git a/src/Enums/Interval.php b/src/Enums/Interval.php index 2ff8afb..4ec24fc 100644 --- a/src/Enums/Interval.php +++ b/src/Enums/Interval.php @@ -2,6 +2,8 @@ namespace Faridibin\Paystack\Enums; +use Carbon\Carbon; + enum Interval: string { case HOURLY = 'hourly'; @@ -11,4 +13,23 @@ enum Interval: string case QUARTERLY = 'quarterly'; case BI_ANNUALLY = 'biannually'; case ANNUALLY = 'annually'; + + /** + * Calculate the next date based on the interval + * + * @param Carbon $date + * @return Carbon + */ + public function calculateNextDate(Carbon $date): Carbon + { + return match ($this) { + self::HOURLY => $date->copy()->addHour(), + self::DAILY => $date->copy()->addDay(), + self::WEEKLY => $date->copy()->addWeek(), + self::MONTHLY => $date->copy()->addMonth(), + self::QUARTERLY => $date->copy()->addMonths(3), + self::BI_ANNUALLY => $date->copy()->addMonths(6), + self::ANNUALLY => $date->copy()->addYear(), + }; + } }