Skip to content

Commit

Permalink
Handle valueless query params
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed Mar 9, 2017
1 parent 187ba51 commit 280a968
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/QueryParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static function fromString(string $query = ''): QueryParameterBag
}

return new static(Arr::mapToAssoc(explode('&', $query), function (string $keyValue) {
return explode('=', $keyValue, 2);
$parts = explode('=', $keyValue, 2);

return count($parts) === 2 ? $parts : [$parts[0], null];
}));
}

Expand Down
16 changes: 16 additions & 0 deletions tests/UrlQueryParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ public function it_can_unset_a_query_parameter()

$this->assertFalse($url->hasQueryParameter('offset'));
}

/** @test */
public function it_can_handle_empty_query_parameters()
{
$url = Url::create()->withQuery('offset');

$this->assertTrue($url->hasQueryParameter('offset'));
}

/** @test */
public function empty_query_parameters_default_to_null()
{
$url = Url::create()->withQuery('offset');

$this->assertNull($url->getQueryParameter('offset'));
}
}

0 comments on commit 280a968

Please sign in to comment.