Skip to content

Commit 050f206

Browse files
authored
Just Say "No" To Mutability (#140)
* heck w/ mutability * Update to latest Spatie iCalendar Generator package
1 parent 0c82ea1 commit 050f206

9 files changed

Lines changed: 31 additions & 33 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"php": "^8.3",
1515
"rlanvin/php-rrule": "^2.3.1",
1616
"spatie/calendar-links": "^1.0",
17-
"spatie/icalendar-generator": "^2.3.3",
18-
"statamic/cms": "^6.0.0-alpha.14"
17+
"spatie/icalendar-generator": "^3.2.1",
18+
"statamic/cms": "^6.0"
1919
},
2020
"require-dev": {
2121
"orchestra/testbench": "^9.2 || ^10.0",

src/Modifiers/InMonth.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace TransformStudios\Events\Modifiers;
44

5-
use Illuminate\Support\Carbon;
5+
use Carbon\CarbonImmutable;
66
use Statamic\Modifiers\Modifier;
77
use Statamic\Support\Arr;
88

@@ -11,11 +11,11 @@ class InMonth extends Modifier
1111
public function index($value, $params, $context)
1212
{
1313
$month = parse_date(
14-
Arr::get($context, 'get.month', Carbon::now()->englishMonth).
14+
Arr::get($context, 'get.month', CarbonImmutable::now()->englishMonth).
1515
' '.
16-
Arr::get($context, 'get.year', Carbon::now()->year)
16+
Arr::get($context, 'get.year', CarbonImmutable::now()->year)
1717
)->month;
1818

19-
return Carbon::parse($value)->month == $month;
19+
return CarbonImmutable::parse($value)->month == $month;
2020
}
2121
}

src/Modifiers/IsEndOfWeek.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Carbon\CarbonImmutable;
66
use Composer\InstalledVersions;
7-
use Illuminate\Support\Carbon;
87
use Statamic\Modifiers\Modifier;
98

109
class IsEndOfWeek extends Modifier
@@ -14,7 +13,7 @@ public function index($value, $params, $context)
1413
$date = CarbonImmutable::parse($value);
1514

1615
if (InstalledVersions::getVersion('nesbot/carbon') >= '3') {
17-
$date->isSameDay($date->locale(Carbon::getLocale())->startOfWeek());
16+
$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
1817
}
1918

2019
return $date->dayOfWeek == now()->endOfWeek()->dayOfWeek;

src/Modifiers/IsStartOfWeek.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Carbon\CarbonImmutable;
66
use Composer\InstalledVersions;
7-
use Illuminate\Support\Carbon;
87
use Statamic\Modifiers\Modifier;
98

109
class IsStartOfWeek extends Modifier
@@ -14,7 +13,7 @@ public function index($value, $params, $context)
1413
$date = CarbonImmutable::parse($value);
1514

1615
if (InstalledVersions::getVersion('nesbot/carbon') >= '3') {
17-
$date->isSameDay($date->locale(Carbon::getLocale())->startOfWeek());
16+
$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
1817
}
1918

2019
return $date->dayOfWeek == now()->startOfWeek()->dayOfWeek;

src/ServiceProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace TransformStudios\Events;
44

5+
use Carbon\CarbonImmutable;
56
use Composer\InstalledVersions;
6-
use Illuminate\Support\Carbon;
77
use Illuminate\Support\Facades\Artisan;
88
use Statamic\Entries\Entry;
99
use Statamic\Facades\Collection;
@@ -30,7 +30,7 @@ public function bootAddon()
3030

3131
private function bootCarbon(): self
3232
{
33-
Carbon::setLocale(Site::current()->locale());
33+
CarbonImmutable::setLocale(Site::current()->locale());
3434

3535
if (InstalledVersions::getVersion('nesbot/carbon') >= '3') {
3636
return $this;
@@ -44,12 +44,12 @@ private function bootCarbon(): self
4444
4545
*/
4646

47-
if (is_string($weekStartDay = Carbon::getTranslator()->trans(id: 'first_day_of_week', locale: Site::current()->locale()))) {
47+
if (is_string($weekStartDay = CarbonImmutable::getTranslator()->trans(id: 'first_day_of_week', locale: Site::current()->locale()))) {
4848
$weekStartDay = 0;
4949
}
5050

51-
Carbon::setWeekStartsAt(day: $weekStartDay);
52-
Carbon::setWeekEndsAt(day: ($weekStartDay + 6) % 7);
51+
CarbonImmutable::setWeekStartsAt(day: $weekStartDay);
52+
CarbonImmutable::setWeekEndsAt(day: ($weekStartDay + 6) % 7);
5353

5454
return $this;
5555
}

src/Tags/Events.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace TransformStudios\Events\Tags;
44

5+
use Carbon\Carbon;
6+
use Carbon\CarbonImmutable;
57
use Carbon\CarbonInterface;
68
use Illuminate\Pagination\Paginator;
7-
use Illuminate\Support\Carbon;
89
use Illuminate\Support\Collection;
910
use Statamic\Contracts\Query\Builder;
1011
use Statamic\Contracts\Taxonomies\Term;
@@ -24,8 +25,8 @@ class Events extends Tags
2425
public function between(): EntryCollection|array
2526
{
2627
return $this->output($this->generator()->between(
27-
from: Carbon::parse($this->params->get('from', now()))->startOfDay(),
28-
to: Carbon::parse($this->params->get('to'))->endOfDay()
28+
from: CarbonImmutable::parse($this->params->get('from', now()))->startOfDay(),
29+
to: CarbonImmutable::parse($this->params->get('to'))->endOfDay()
2930
));
3031
}
3132

@@ -52,7 +53,7 @@ public function downloadLink(): string
5253
'statamic.events.ics.show',
5354
Arr::removeNullValues([
5455
'collection' => $this->params->get('collection', 'events'),
55-
'date' => $this->params->has('date') ? Carbon::parse($this->params->get('date'))->toDateString() : null,
56+
'date' => $this->params->has('date') ? CarbonImmutable::parse($this->params->get('date'))->toDateString() : null,
5657
'event' => $this->params->get('event'),
5758
])
5859
);
@@ -165,7 +166,7 @@ private function makeEmptyDates(CarbonInterface $from, CarbonInterface $to): Col
165166
$dates = collect();
166167
$currentDay = $from->copy()->toMutable();
167168

168-
foreach (range(0, Carbon::parse($to)->diffInDays($from)) as $ignore) {
169+
foreach (range(0, CarbonImmutable::parse($to)->diffInDays($from)) as $ignore) {
169170
$date = $currentDay->toDateString();
170171
$dates->put($date, [
171172
'date' => $date,

src/Types/Event.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace TransformStudios\Events\Types;
44

5-
use Carbon\Carbon;
65
use Carbon\CarbonImmutable;
76
use Carbon\CarbonInterface;
87
use DateTimeInterface;
@@ -167,9 +166,9 @@ protected function supplement(CarbonInterface $date): ?Entry
167166

168167
protected function toCarbonImmutable(string|CarbonInterface $date): CarbonImmutable
169168
{
170-
$carbon = is_string($date) ? Carbon::parse($date) : $date;
169+
$carbon = is_string($date) ? CarbonImmutable::parse($date) : $date;
171170

172-
return $carbon->shiftTimezone($this->timezone['name'])->toImmutable();
171+
return $carbon->shiftTimezone($this->timezone['name']);
173172
}
174173

175174
private function collect(array $dates): Collection

src/Types/RecurringEvent.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace TransformStudios\Events\Types;
44

5+
use Carbon\CarbonImmutable;
56
use Illuminate\Support\Arr;
6-
use Illuminate\Support\Carbon;
77
use RRule\RRule;
88
use RRule\RRuleInterface;
99
use Spatie\IcalendarGenerator\Components\Event as ICalendarEvent;
@@ -57,7 +57,7 @@ protected function rule(): RRuleInterface
5757
];
5858

5959
if ($end = $this->end_date) {
60-
$rule['until'] = Carbon::parse($end)->shiftTimezone($this->timezone['name'])->endOfDay();
60+
$rule['until'] = CarbonImmutable::parse($end)->shiftTimezone($this->timezone['name'])->endOfDay();
6161
}
6262

6363
if (! empty($days = $this->onSpecificDays())) {
@@ -82,11 +82,11 @@ private function frequency(): int
8282
private function frequencyToRecurrence(): RecurrenceFrequency
8383
{
8484
return match ($this->frequency()) {
85-
Rrule::DAILY => RecurrenceFrequency::daily(),
86-
Rrule::WEEKLY => RecurrenceFrequency::weekly(),
87-
Rrule::MONTHLY => RecurrenceFrequency::monthly(),
88-
Rrule::YEARLY => RecurrenceFrequency::yearly(),
89-
default => RecurrenceFrequency::daily()
85+
Rrule::DAILY => RecurrenceFrequency::Daily,
86+
Rrule::WEEKLY => RecurrenceFrequency::Weekly,
87+
Rrule::MONTHLY => RecurrenceFrequency::Monthly,
88+
Rrule::YEARLY => RecurrenceFrequency::Yearly,
89+
default => RecurrenceFrequency::Daily
9090
};
9191
}
9292

@@ -107,7 +107,7 @@ private function spatieRule(): ICalendarRule
107107
->interval($this->interval());
108108

109109
if ($end = $this->end_date) {
110-
$rule->until(Carbon::parse($end)->endOfDay());
110+
$rule->until(CarbonImmutable::parse($end)->endOfDay());
111111
}
112112

113113
return $rule;

src/helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3+
use Carbon\CarbonImmutable;
34
use Carbon\CarbonInterface;
45
use Carbon\Exceptions\InvalidFormatException;
5-
use Illuminate\Support\Carbon;
66

77
if (! function_exists('parse_date')) {
88
/**
@@ -11,7 +11,7 @@
1111
function parse_date(string $date): CarbonInterface
1212
{
1313
try {
14-
$date = Carbon::parse($date);
14+
$date = CarbonImmutable::parse($date);
1515
} catch (InvalidFormatException $e) {
1616
$date = now();
1717
}

0 commit comments

Comments
 (0)