Skip to content

Commit 21ad7b1

Browse files
committed
Improve MapKey implementation
1 parent 794a7f3 commit 21ad7b1

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@
4242
"php" : "^8.1"
4343
},
4444
"require-dev": {
45-
"friendsofphp/php-cs-fixer": "^3.64.0",
45+
"friendsofphp/php-cs-fixer": "^3.65.0",
4646
"httpwg/structured-field-tests": "*@dev",
47-
"phpstan/phpstan": "^1.12.8",
47+
"phpstan/phpstan": "^1.12.12",
4848
"phpstan/phpstan-strict-rules": "^1.6.1",
49-
"phpstan/phpstan-phpunit": "^1.4.0",
49+
"phpstan/phpstan-phpunit": "^1.4.1",
5050
"phpstan/phpstan-deprecation-rules": "^1.2.1",
51-
"phpunit/phpunit": "^10.5.15 || ^11.4.3",
51+
"phpunit/phpunit": "^10.5.15 || ^11.4.4",
5252
"phpbench/phpbench": "^1.3.1",
53-
"symfony/var-dumper": "^6.4.14",
53+
"symfony/var-dumper": "^6.4.15",
5454
"bakame/aide-base32": "dev-main"
5555
},
5656
"autoload": {

src/MapKey.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bakame\Http\StructuredFields;
44

5+
use Stringable;
6+
57
use function preg_match;
68

79
/**
@@ -10,29 +12,25 @@
1012
*/
1113
final class MapKey
1214
{
13-
private function __construct(
14-
public readonly string $value
15-
) {
15+
private function __construct(public readonly string $value)
16+
{
1617
}
1718

1819
/**
1920
* @throws SyntaxError If the string is not a valid HTTP value field key
2021
*/
21-
public static function from(string|int $httpValue): self
22+
public static function from(Stringable|string|int $httpValue): self
2223
{
23-
if (!is_string($httpValue)) {
24-
throw new SyntaxError('The key must be a string; '.gettype($httpValue).' received.');
25-
}
26-
27-
$instance = self::fromStringBeginning($httpValue);
28-
if ($instance->value !== $httpValue) {
24+
$key = (string) $httpValue;
25+
$instance = self::fromStringBeginning($key);
26+
if ($instance->value !== $key) {
2927
throw new SyntaxError('No valid http value key could be extracted from "'.$httpValue.'".');
3028
}
3129

3230
return $instance;
3331
}
3432

35-
public static function tryFrom(string|int $httpValue): ?self
33+
public static function tryFrom(Stringable|string|int $httpValue): ?self
3634
{
3735
try {
3836
return self::from($httpValue);

0 commit comments

Comments
 (0)