File tree Expand file tree Collapse file tree 4 files changed +58
-4
lines changed Expand file tree Collapse file tree 4 files changed +58
-4
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
6
6
7
7
### Added
8
8
9
- - None
9
+ - ` Item::fromPair ` named constructor to create a new instance from a pair expressed as an array list with two values.
10
10
11
11
### Fixed
12
12
Original file line number Diff line number Diff line change 4
4
5
5
namespace Bakame \Http \StructuredFields ;
6
6
7
+ use function count ;
7
8
use function in_array ;
8
9
use function is_bool ;
9
10
use function is_float ;
@@ -34,6 +35,26 @@ public static function __set_state(array $properties): self
34
35
return new self ($ properties ['value ' ], $ properties ['parameters ' ]);
35
36
}
36
37
38
+ /**
39
+ * @param array{
40
+ * 0:Token|ByteSequence|int|float|string|bool,
41
+ * 1?:iterable<string,Item|ByteSequence|Token|bool|int|float|string>
42
+ * } $pair
43
+ */
44
+ public static function fromPair (array $ pair ): self
45
+ {
46
+ if (!array_is_list ($ pair )) { /* @phpstan-ignore-line */
47
+ throw new SyntaxError ('The pair must be represented by an array as a list. ' );
48
+ }
49
+
50
+ $ pair [1 ] = $ pair [1 ] ?? [];
51
+ if (2 !== count ($ pair )) { /* @phpstan-ignore-line */
52
+ throw new SyntaxError ('The pair first value should be the item value and the optional second value the item parameters. ' );
53
+ }
54
+
55
+ return self ::from (...$ pair );
56
+ }
57
+
37
58
/**
38
59
* Returns a new instance from a value type and an iterable of key-value parameters.
39
60
*
Original file line number Diff line number Diff line change @@ -256,4 +256,39 @@ public function it_can_exchange_parameters(): void
256
256
self ::assertCount (1 , $ instance ->parameters );
257
257
self ::assertSame ('bar ' , $ instance ->parameters ->value ('foo ' ));
258
258
}
259
+
260
+ /**
261
+ * @test
262
+ */
263
+ public function it_can_create_an_item_from_a_array_pair (): void
264
+ {
265
+ $ instance1 = Item::from (Token::fromString ('babayaga ' ));
266
+ $ instance2 = Item::fromPair ([Token::fromString ('babayaga ' )]);
267
+ $ instance3 = Item::fromPair ([Token::fromString ('babayaga ' ), []]);
268
+
269
+ self ::assertEquals ($ instance2 , $ instance1 );
270
+ self ::assertEquals ($ instance3 , $ instance1 );
271
+ }
272
+
273
+ /**
274
+ * @test
275
+ * @dataProvider invalidPairProvider
276
+ * @param array<mixed> $pair
277
+ */
278
+ public function it_fails_to_create_an_item_from_a_array_pair (array $ pair ): void
279
+ {
280
+ $ this ->expectException (SyntaxError::class);
281
+
282
+ Item::fromPair ($ pair ); /* @phpstan-ignore-line */
283
+ }
284
+
285
+ /**
286
+ * @return iterable<string, array{pair:array<mixed>}>
287
+ */
288
+ public function invalidPairProvider (): iterable
289
+ {
290
+ yield 'empty pair ' => ['pair ' => []];
291
+ yield 'empty extra filled pair ' => ['pair ' => [1 , [2 ], 3 ]];
292
+ yield 'associative array ' => ['pair ' => ['value ' => 'bar ' , 'parameters ' => ['foo ' => 'bar ' ]]];
293
+ }
259
294
}
Original file line number Diff line number Diff line change 10
10
*/
11
11
final class MapKey
12
12
{
13
- private const REGEXP_KEY = '/^(?<key>[a-z*][a-z0-9.*_-]*)/ ' ;
14
-
15
13
private function __construct (
16
14
public readonly string $ value
17
15
) {
@@ -35,7 +33,7 @@ public static function fromString(string $httpValue): self
35
33
*/
36
34
public static function fromStringBeginning (string $ httpValue ): self
37
35
{
38
- if (1 !== preg_match (self :: REGEXP_KEY , $ httpValue , $ found )) {
36
+ if (1 !== preg_match (' /^(?<key>[a-z*][a-z0-9.*_-]*)/ ' , $ httpValue , $ found )) {
39
37
throw new SyntaxError ("No valid http value key could be extracted from ` $ httpValue`. " );
40
38
}
41
39
You can’t perform that action at this time.
0 commit comments