Skip to content

Commit 29ca09e

Browse files
committed
Adding Item::fromPair
1 parent fb8ff49 commit 29ca09e

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
66

77
### Added
88

9-
- None
9+
- `Item::fromPair` named constructor to create a new instance from a pair expressed as an array list with two values.
1010

1111
### Fixed
1212

src/Item.php

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

55
namespace Bakame\Http\StructuredFields;
66

7+
use function count;
78
use function in_array;
89
use function is_bool;
910
use function is_float;
@@ -34,6 +35,26 @@ public static function __set_state(array $properties): self
3435
return new self($properties['value'], $properties['parameters']);
3536
}
3637

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+
3758
/**
3859
* Returns a new instance from a value type and an iterable of key-value parameters.
3960
*

src/ItemTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,39 @@ public function it_can_exchange_parameters(): void
256256
self::assertCount(1, $instance->parameters);
257257
self::assertSame('bar', $instance->parameters->value('foo'));
258258
}
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+
}
259294
}

src/MapKey.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
*/
1111
final class MapKey
1212
{
13-
private const REGEXP_KEY = '/^(?<key>[a-z*][a-z0-9.*_-]*)/';
14-
1513
private function __construct(
1614
public readonly string $value
1715
) {
@@ -35,7 +33,7 @@ public static function fromString(string $httpValue): self
3533
*/
3634
public static function fromStringBeginning(string $httpValue): self
3735
{
38-
if (1 !== preg_match(self::REGEXP_KEY, $httpValue, $found)) {
36+
if (1 !== preg_match('/^(?<key>[a-z*][a-z0-9.*_-]*)/', $httpValue, $found)) {
3937
throw new SyntaxError("No valid http value key could be extracted from `$httpValue`.");
4038
}
4139

0 commit comments

Comments
 (0)