Skip to content

Commit

Permalink
Treat empty month and year strings as null
Browse files Browse the repository at this point in the history
Signed-off-by: ramchale <[email protected]>
  • Loading branch information
ramchale committed Dec 9, 2024
1 parent 6481287 commit 02d2e44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/MonthSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,26 @@ public function __invoke(mixed $value): mixed
return $this->filter($value);
}

/**
* Returns the result of filtering $value
*
* @template T
* @param T $value
* @return string|null|T
*/
public function filter(mixed $value): mixed
{
if (! is_array($value)) {
return $value;
}

/** @var mixed $month */
$month = $value['month'] ?? null;
/** @var mixed $year */
/** @var mixed $month */
$month = $month === '' ? null : $month;

$year = $value['year'] ?? null;
/** @var mixed $year */
$year = $year === '' ? null : $year;

if ($this->returnNullIfAnyFieldEmpty && ($month === null || $year === null)) {
return null;
Expand All @@ -55,7 +65,7 @@ public function filter(mixed $value): mixed
}

if (! $this->isParsableAsDateValue($month, 1, 12) || ! $this->isParsableAsDateValue($year, 0, 9999)) {
/** @psalm-suppress InvalidReturnStatement */
/** @psalm-var T */
return $value;
}

Expand Down
1 change: 1 addition & 0 deletions test/MonthSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static function provideFilter(): array
[['null_on_empty' => true], ['year' => null], null],
[['null_on_all_empty' => true], ['year' => null, 'month' => null], null],
[['null_on_all_empty' => true], [], null],
[['null_on_all_empty' => true], ['year' => '', 'month' => ''], null],
];
}

Expand Down

0 comments on commit 02d2e44

Please sign in to comment.