Skip to content

Commit

Permalink
Differentiate between 0 and null in NumDropMenu
Browse files Browse the repository at this point in the history
The season dropdown on eventreport.php has both "All" (empty string, converted
to null by Request->optionalInt) and "0" (converted to 0 by
Request->optionalInt) both of which are fuzzily equal to 0 but only one of which
is strictly equal to 0. So take this opportunity to tighten up the types here
and make that work how it should ("All" is preselected when no season specified,
but 0 is preselected if season 0 is specified).
  • Loading branch information
bakert committed Nov 10, 2024
1 parent 4504473 commit 8188cbd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
8 changes: 2 additions & 6 deletions gatherling/Views/Components/NumDropMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

class NumDropMenu extends DropMenu
{
public function __construct(string $field, ?string $title, int $max, int|string|null $def, int $min = 0, ?string $special = null)
public function __construct(string $field, ?string $title, int $max, ?int $def, int $min = 0, ?string $special = null)
{
if ($def === '') {
$def = -1;
}

$options = [];
if ($special) {
$options[] = [
Expand All @@ -26,7 +22,7 @@ public function __construct(string $field, ?string $title, int $max, int|string|
$options[] = [
'text' => (string) $n,
'value' => (string) $n,
'isSelected' => $n == $def,
'isSelected' => $n === $def,
];
}

Expand Down
20 changes: 10 additions & 10 deletions gatherling/Views/Pages/EventForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ public function __construct(Event $event, bool $edit)
if ($event->start != null) {
$date = $event->start;
preg_match('/([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):.*/', $date, $datearr);
$year = $datearr[1];
$month = $datearr[2];
$day = $datearr[3];
$hour = $datearr[4];
$minutes = $datearr[5];
$year = (int) $datearr[1];
$month = (int) $datearr[2];
$day = (int) $datearr[3];
$hour = (int) $datearr[4];
$minutes = (int) $datearr[5];
} else {
$year = date('Y', time());
$month = date('n', time());
$day = date('j', time());
$hour = date('H', time());
$minutes = date('i', time());
$year = (int) date('Y', time());
$month = (int) date('n', time());
$day = (int) date('j', time());
$hour = (int) date('H', time());
$minutes = (int) date('i', time());
}

$navLinks = [];
Expand Down
2 changes: 1 addition & 1 deletion gatherling/eventreport.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function eventList(string $format, string $series, ?int $season): array
$params['series'] = $series;
}

if (!empty($season)) {
if ($season !== null) {
$sql .= ' AND e.season = :season';
$params['season'] = $season;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Views/Components/NumDropMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public function testNumDropMenu(): void
$this->assertCount(12, $html->filter('select')->filter('option'));
$this->assertEquals('5', $html->filter('select')->filter('option[selected]')->attr('value'));

$numDropMenu = new NumDropMenu('test', 'Test', 10, '5');
$numDropMenu = new NumDropMenu('test', 'Test', 10, null);
$html = new Crawler($numDropMenu->render());
$this->assertEquals('test', $html->filter('select')->attr('name'));
$this->assertCount(12, $html->filter('select')->filter('option'));
$this->assertEquals('5', $html->filter('select')->filter('option[selected]')->attr('value'));
// Nothing is explicitly marked as selected, not even the default option
$this->assertEquals(0, $html->filter('select')->filter('option[selected]')->count());

$numDropMenu = new NumDropMenu('test', 'Test', 10, null);
$numDropMenu = new NumDropMenu('test', 'Test', 10, 0);
$html = new Crawler($numDropMenu->render());
$this->assertEquals('test', $html->filter('select')->attr('name'));
$this->assertCount(12, $html->filter('select')->filter('option'));
Expand Down

0 comments on commit 8188cbd

Please sign in to comment.