Skip to content

Commit

Permalink
Update SuperchargedCarbonServiceProvider.php
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-ivona authored Nov 9, 2023
1 parent 51d955b commit 0b3987f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/SuperchargedCarbonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


use Carbon\Carbon\CarbonInterface;
use Carbon\CarbonImmutable;
use Illuminate\Support\Carbon;
use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -124,9 +125,11 @@ public function register()

foreach ($holidays as $holiday_check_method => $holiday_check_macro) {
Carbon::macro($holiday_check_method, $holiday_check_macro);
CarbonImmutable::macro($holiday_check_method, $holiday_check_macro);
}

Carbon::macro('isHoliday', function () use ($holidays) {

$holiday_closure = function () use ($holidays) {
if (!$this->isWeekday()) {
return true;
}
Expand All @@ -138,20 +141,29 @@ public function register()
}

return false;
});
};

Carbon::macro('isWorkday', function () {
$workday_closure = function () {
return !$this->isHoliday();
});
};

Carbon::macro('addWorkdays', function ($days_to_add = 1) {
$add_workdays_closure = function ($days_to_add = 1) {
while ($days_to_add > 0) {
$this->addDay();
if ($this->isWorkday()) {
$days_to_add--;
}
}
return $this;
});
};

Carbon::macro('isHoliday', $holiday_closure);
CarbonImmutable::macro('isHoliday', $holiday_closure);

Carbon::macro('isWorkday', $workday_closure);
CarbonImmutable::macro('isWorkday', $workday_closure);

Carbon::macro('addWorkdays', $add_workdays_closure);
CarbonImmutable::macro('addWorkdays', $add_workdays_closure);
}
}

0 comments on commit 0b3987f

Please sign in to comment.