Skip to content

Commit

Permalink
fix: getString() behaviour for non-scalar values (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Dec 20, 2023
1 parent 3d8a935 commit c9c9cd8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:

sca:
uses: zenstruck/.github/.github/workflows/php-stan.yml@main
with:
php: 8.2

fixcs:
name: Run php-cs-fixer
Expand Down
16 changes: 13 additions & 3 deletions src/Uri/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function has(string $param): bool
*
* @param T|\Throwable $default
*
* @return T
* @return T|mixed
*
* @throws \Throwable If passed as default and no match
*/
Expand All @@ -57,11 +57,21 @@ public function get(string $param, mixed $default = null): mixed
}

/**
* @throws \Throwable If passed as default and no match
* @throws \Throwable If passed as default and no match OR if value cannot be converted to a string
*/
public function getString(string $param, string|\Throwable $default = ''): string
{
return (string) $this->get($param, $default);
$value = $this->get($param, $default);

if (\is_scalar($value) || null === $value || $value instanceof \Stringable) {
return (string) $value;
}

if ($default instanceof \Throwable) {
throw $default;
}

return $default;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Uri/Part/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function get(string $param, mixed $default = null): mixed
}

/**
* @throws \Throwable If passed as default and no match
* @throws \Throwable If passed as default and no match OR if value cannot be converted to a string
*/
public function getString(string $param, string|\Throwable $default = ''): string
{
Expand Down
43 changes: 43 additions & 0 deletions tests/ParametersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the zenstruck/uri package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Uri\Tests;

use PHPUnit\Framework\TestCase;
use Zenstruck\Uri\Parameters;

/**
* @author Kevin Bond <[email protected]>
*/
final class ParametersTest extends TestCase
{
/**
* @test
*/
public function get_string_on_other_types(): void
{
$this->assertSame('', (new Parameters(['foo' => ['bar']]))->getString('foo'));
$this->assertSame('', (new Parameters(['foo' => null]))->getString('foo'));

$this->expectException(\RuntimeException::class);

(new Parameters(['foo' => []]))->getString('foo', new \RuntimeException());
}

/**
* @test
*/
public function get_int_on_other_types(): void
{
$this->assertSame(1, (new Parameters(['foo' => ['bar']]))->getInt('foo'));
$this->assertSame(0, (new Parameters(['foo' => null]))->getInt('foo'));
}
}

0 comments on commit c9c9cd8

Please sign in to comment.