Skip to content

Commit 1a8e8eb

Browse files
authored
Add support for month param (#163)
1 parent a5c8c0d commit 1a8e8eb

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

src/Modifiers/InMonth.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88

99
class InMonth extends Modifier
1010
{
11-
public function index($value, $params, $context)
11+
public function index($value, $params, $context): bool
1212
{
13-
$month = parse_date(
14-
Arr::get($context, 'get.month', CarbonImmutable::now()->englishMonth).
15-
' '.
16-
Arr::get($context, 'get.year', CarbonImmutable::now()->year)
17-
)->month;
13+
$month = $params[0] ?? Arr::get($context, 'get.month') ?? CarbonImmutable::now()->englishMonth;
14+
$year = CarbonImmutable::now()->year;
1815

19-
return CarbonImmutable::parse($value)->month == $month;
16+
return CarbonImmutable::parse($value)->month === parse_date("$month $year")->month;
2017
}
2118
}

tests/Modifiers/InMonthTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use Carbon\CarbonImmutable;
4+
use Statamic\Modifiers\Modify;
5+
6+
it('returns true when date is in param month', function () {
7+
expect(modifyInMonth(value: '2026-05-15', params: ['May']))->toBeTrue();
8+
});
9+
10+
it('returns false when date is not in param month', function () {
11+
expect(modifyInMonth(value: '2026-04-15', params: ['May']))->toBeFalse();
12+
});
13+
14+
it('returns true for first day of month', function () {
15+
expect(modifyInMonth(value: '2026-05-01', params: ['May']))->toBeTrue();
16+
});
17+
18+
it('returns true for last day of month', function () {
19+
expect(modifyInMonth(value: '2026-05-31', params: ['May']))->toBeTrue();
20+
});
21+
22+
it('returns false for day before month', function () {
23+
expect(modifyInMonth(value: '2026-04-30', params: ['May']))->toBeFalse();
24+
});
25+
26+
it('returns false for day after month', function () {
27+
expect(modifyInMonth(value: '2026-06-01', params: ['May']))->toBeFalse();
28+
});
29+
30+
it('returns true when date matches month from context', function () {
31+
expect(modifyInMonth(value: '2026-05-15', context: ['get' => ['month' => 'May']]))->toBeTrue();
32+
});
33+
34+
it('returns false when date does not match month from context', function () {
35+
expect(modifyInMonth(value: '2026-04-15', context: ['get' => ['month' => 'May']]))->toBeFalse();
36+
});
37+
38+
it('param takes precedence over context', function () {
39+
expect(modifyInMonth(value: '2026-06-01', params: ['June'], context: ['get' => ['month' => 'May']]))->toBeTrue();
40+
});
41+
42+
it('uses current month when no param or context given', function () {
43+
CarbonImmutable::setTestNow('2026-05-15');
44+
45+
expect(modifyInMonth(value: '2026-05-01'))->toBeTrue();
46+
expect(modifyInMonth(value: '2026-04-30'))->toBeFalse();
47+
48+
CarbonImmutable::setTestNow();
49+
});
50+
51+
function modifyInMonth(string $value, array $params = [], array $context = []): bool
52+
{
53+
return Modify::value($value)->context($context)->inMonth($params)->fetch();
54+
}

0 commit comments

Comments
 (0)