Skip to content

Commit 2561d73

Browse files
committed
Improve InnerList::fromPair
1 parent 747b112 commit 2561d73

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ use Bakame\Http\StructuredFields\InnerList;
423423
use Bakame\Http\StructuredFields\ValueAccess;
424424

425425
InnerList::fromAssociative(iterable<string, Value|DataType> $parameters, ...$members): self;
426-
InnerList::fromPairs(iterable<array{0:string, 1:Value|DataType}>} $parameters, ...$members): self;
426+
InnerList::fromPair(array{0:list<Item>, iterable<array{0:string, 1:Value|DataType}>} $pair): self;
427427
```
428428

429429
## Contributing

src/InnerList.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,28 @@ public static function from(StructuredField|Value|Token|ByteSequence|DateTimeInt
5959
}
6060

6161
/**
62-
* @param MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}> $parameters
62+
* @param array{
63+
* 0:iterable<SfItemInput>,
64+
* 1?:MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>
65+
* } $pair
6366
*/
64-
public static function fromPairs(
65-
iterable $parameters,
66-
StructuredField|Value|Token|ByteSequence|DateTimeInterface|Stringable|string|int|float|bool ...$members
67-
): self {
68-
return new self(Parameters::fromPairs($parameters), $members);
67+
public static function fromPair(array $pair): self
68+
{
69+
$pair[1] = $pair[1] ?? [];
70+
71+
if (!array_is_list($pair)) { /* @phpstan-ignore-line */
72+
throw new SyntaxError('The pair must be represented by an array as a list.');
73+
}
74+
75+
if (2 !== count($pair)) { /* @phpstan-ignore-line */
76+
throw new SyntaxError('The pair first value should be the member list and the optional second value the inner list parameters.');
77+
}
78+
79+
if (!$pair[1] instanceof Parameters) {
80+
$pair[1] = Parameters::fromPairs($pair[1]);
81+
}
82+
83+
return new self($pair[1], $pair[0]);
6984
}
7085

7186
/**

src/InnerListTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,10 @@ public function it_can_create_via_with_parameters_method_a_new_object(): void
195195
'a',
196196
true
197197
);
198-
$instance1bis = InnerList::fromPairs(
198+
$instance1bis = InnerList::fromPair([
199+
[Token::fromString('babayaga'), 'a', true],
199200
[['a', true]],
200-
Token::fromString('babayaga'),
201-
'a',
202-
true
203-
);
201+
]);
204202
$instance2 = $instance1->withParameters(Parameters::fromAssociative(['a' => true]));
205203
$instance3 = $instance1->withParameters(Parameters::fromAssociative(['a' => false]));
206204

0 commit comments

Comments
 (0)