Skip to content

Commit 3fb07f8

Browse files
committed
Widening parameter hints by adding support for Stringable objects
1 parent b34244d commit 3fb07f8

File tree

7 files changed

+34
-25
lines changed

7 files changed

+34
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1313
- `Token::value` is a readonly property.
1414
- `Item::value` method returns the decoded value of an Item (returns value can be `float|int|string|bool`).
1515
- `Item::fromToken`, `Item::fromDecodedByteSequence` , `Item::fromEncodedByteSequence` to ease `Item` creation.
16+
- `Parser` methods also accepts `Stringable` objects.
1617

1718
### Fixed
1819

src/Dictionary.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Iterator;
8+
use Stringable;
89
use function array_key_exists;
910
use function array_keys;
1011
use function array_map;
@@ -70,7 +71,7 @@ public static function fromPairs(MemberOrderedMap|iterable $pairs = []): self
7071
*
7172
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.2
7273
*/
73-
public static function fromHttpValue(string $httpValue): self
74+
public static function fromHttpValue(Stringable|string $httpValue): self
7475
{
7576
return self::fromAssociative(array_map(
7677
fn (mixed $value): mixed => is_array($value) ? InnerList::fromList(...$value) : $value,

src/InnerList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Iterator;
8+
use Stringable;
89
use function array_filter;
910
use function array_map;
1011
use function array_splice;
@@ -62,7 +63,7 @@ private static function filterForbiddenState(Item $member): Item
6263
return $member;
6364
}
6465

65-
public static function fromHttpValue(string $httpValue): self
66+
public static function fromHttpValue(Stringable|string $httpValue): self
6667
{
6768
return InnerList::fromList(...Parser::parseInnerList($httpValue));
6869
}

src/Item.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,48 +51,51 @@ public static function fromPair(array $pair): self
5151
/**
5252
* Returns a new instance from a value type and an iterable of key-value parameters.
5353
*
54-
* @param iterable<string,Item|DataType> $parameters
54+
* @param iterable<string,Item|DataType>|string|Stringable $parameters
5555
*/
5656
public static function from(
5757
Token|ByteSequence|int|float|string|bool $value,
58-
iterable $parameters = []
58+
iterable|string|Stringable $parameters = []
5959
): self {
6060
return new self(match (true) {
6161
is_int($value) => self::filterInteger($value),
6262
is_float($value) => self::filterDecimal($value),
6363
is_string($value) => self::filterString($value),
6464
default => $value,
65-
}, Parameters::fromAssociative($parameters));
65+
}, match (true) {
66+
is_string($parameters) || $parameters instanceof Stringable => Parameters::fromHttpValue($parameters),
67+
default => Parameters::fromAssociative($parameters),
68+
});
6669
}
6770

6871
/**
6972
* Returns a new instance from an encoded byte sequence and an iterable of key-value parameters.
7073
*
71-
* @param iterable<string,Item|DataType> $parameters
74+
* @param iterable<string,Item|DataType>|Stringable|string|null $parameters
7275
*/
73-
public static function fromEncodedByteSequence(string|Stringable $value, iterable $parameters = []): self
76+
public static function fromEncodedByteSequence(string|Stringable $value, iterable|Stringable|string $parameters = null): self
7477
{
75-
return self::from(ByteSequence::fromEncoded($value), $parameters);
78+
return self::from(ByteSequence::fromEncoded($value), $parameters ?? []);
7679
}
7780

7881
/**
7982
* Returns a new instance from a decoded byte sequence and an iterable of key-value parameters.
8083
*
81-
* @param iterable<string,Item|DataType> $parameters
84+
* @param iterable<string,Item|DataType>|Stringable|string|null $parameters
8285
*/
83-
public static function fromDecodedByteSequence(string|Stringable $value, iterable $parameters = []): self
86+
public static function fromDecodedByteSequence(string|Stringable $value, iterable|Stringable|string $parameters = null): self
8487
{
85-
return self::from(ByteSequence::fromDecoded($value), $parameters);
88+
return self::from(ByteSequence::fromDecoded($value), $parameters ?? []);
8689
}
8790

8891
/**
8992
* Returns a new instance from a Token and an iterable of key-value parameters.
9093
*
91-
* @param iterable<string,Item|ByteSequence|Token|bool|int|float|string> $parameters
94+
* @param iterable<string,Item|DataType>|Stringable|string|null $parameters
9295
*/
93-
public static function fromToken(string|Stringable $value, iterable $parameters = []): self
96+
public static function fromToken(string|Stringable $value, iterable|Stringable|string $parameters = null): self
9497
{
95-
return self::from(Token::fromString($value), $parameters);
98+
return self::from(Token::fromString($value), $parameters ?? []);
9699
}
97100

98101
/**
@@ -143,9 +146,9 @@ private static function filterInteger(int $value): int
143146
*
144147
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3
145148
*/
146-
public static function fromHttpValue(string $httpValue): self
149+
public static function fromHttpValue(Stringable|string $httpValue): self
147150
{
148-
$itemString = trim($httpValue, ' ');
151+
$itemString = trim((string) $httpValue, ' ');
149152

150153
[$value, $parameters] = match (true) {
151154
1 === preg_match("/[\r\t\n]|[^\x20-\x7E]/", $itemString),

src/OrderedList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Iterator;
8+
use Stringable;
89
use function array_filter;
910
use function array_map;
1011
use function array_splice;
@@ -81,7 +82,7 @@ private static function filterForbiddenState(InnerList|Item $member): InnerList|
8182
*
8283
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.1
8384
*/
84-
public static function fromHttpValue(string $httpValue): self
85+
public static function fromHttpValue(Stringable|string $httpValue): self
8586
{
8687
return self::fromList(array_map(
8788
fn (mixed $value): mixed => is_array($value) ? InnerList::fromList(...$value) : $value,

src/Parameters.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Iterator;
8+
use Stringable;
89
use function array_key_exists;
910
use function array_keys;
1011
use function array_map;
@@ -108,10 +109,10 @@ public static function fromPairs(MemberOrderedMap|iterable $pairs = []): self
108109
* @throws SyntaxError If the string is not a valid
109110
* @throws ForbiddenStateError If the bare item contains parameters
110111
*/
111-
public static function fromHttpValue(string $httpValue): self
112+
public static function fromHttpValue(Stringable|string $httpValue): self
112113
{
113114
$instance = new self();
114-
$httpValue = ltrim($httpValue, ' ');
115+
$httpValue = ltrim((string) $httpValue, ' ');
115116
if ('' === $httpValue) {
116117
return $instance;
117118
}

src/Parser.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Bakame\Http\StructuredFields;
66

7+
use Stringable;
78
use function in_array;
89
use function ltrim;
910
use function preg_match;
@@ -32,10 +33,10 @@ final class Parser
3233
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
3334
* }|Item|ByteSequence|Token|bool|int|float|string>
3435
*/
35-
public static function parseList(string $httpValue): array
36+
public static function parseList(Stringable|string $httpValue): array
3637
{
3738
$list = [];
38-
$remainder = ltrim($httpValue, ' ');
39+
$remainder = ltrim((string) $httpValue, ' ');
3940
while ('' !== $remainder) {
4041
[$list[], $offset] = self::parseItemOrInnerList($remainder);
4142
$remainder = self::removeCommaSeparatedWhiteSpaces($remainder, $offset);
@@ -54,10 +55,10 @@ public static function parseList(string $httpValue): array
5455
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
5556
* }|bool|int|float|string>
5657
*/
57-
public static function parseDictionary(string $httpValue): array
58+
public static function parseDictionary(Stringable|string $httpValue): array
5859
{
5960
$map = [];
60-
$remainder = ltrim($httpValue, ' ');
61+
$remainder = ltrim((string) $httpValue, ' ');
6162
while ('' !== $remainder) {
6263
$key = MapKey::fromStringBeginning($remainder)->value;
6364
$remainder = substr($remainder, strlen($key));
@@ -82,9 +83,9 @@ public static function parseDictionary(string $httpValue): array
8283
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
8384
* }
8485
*/
85-
public static function parseInnerList(string $httpValue): array
86+
public static function parseInnerList(Stringable|string $httpValue): array
8687
{
87-
$remainder = ltrim($httpValue, ' ');
88+
$remainder = ltrim((string) $httpValue, ' ');
8889
if ('(' !== $remainder[0]) {
8990
throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a inner list is missing a parenthesis.");
9091
}

0 commit comments

Comments
 (0)