Skip to content

Commit a3dc14c

Browse files
committed
Add InnerList auto conversion
1 parent 46570b3 commit a3dc14c

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,23 @@ use Bakame\Http\StructuredFields\Item;
335335
use Bakame\Http\StructuredFields\Token;
336336

337337
$value = Dictionary::new()
338-
->add('a', Item::fromToken('bar'))
338+
->add('a', InnerList::new(Item::fromToken('bar'), tem::fromString('bar'))
339339
->prepend('b', Item::false())
340340
->append('c', Item::fromDateString('2022-12-23 13:00:23'))
341341
;
342342

343-
echo $value->toHttpValue(); //"b=?0, a=bar, c=@1671800423"
344-
echo $value; //"b=?0, a=bar, c=@1671800423"
343+
echo $value->toHttpValue(); //b=?0, a=(bar "bar"), c=@1671800423
344+
echo $value; //b=?0, a=(bar "bar"), c=@1671800423
345345
```
346346

347-
**Of note: For all containers, if the submitted type is not a `StructuredField`
348-
implementing object, it will be passed to `Item::new` to convert it into a
349-
bare `Item` instances.**
347+
#### Automatic conversion.
348+
349+
For all containers to ease instantiaiton the following automatic conversion are applied on
350+
the member argument of each modifying methods, if the submitted type is:
351+
352+
- a `StructuredField` implementing object, it will be passed as is
353+
- an iterable structure it will be converted to an `InnerList` instance using `InnerList::new`
354+
- otherwise the value will be converted to an `Item` using `Item::new`.
350355

351356
This means that the previous example can be rewritten like this:
352357

@@ -356,7 +361,7 @@ use Bakame\Http\StructuredFields\Item;
356361
use Bakame\Http\StructuredFields\Token;
357362

358363
$value = Dictionary::new()
359-
->add('a', 'bar')
364+
->add('a', [Token::fromString('bar'), 'bar'])
360365
->prepend('b', false)
361366
->append('c', new DateTimeImmutable('2022-12-23 13:00:23'))
362367
;

src/Dictionary.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ public function pair(int $index): array
234234
return [...$this->toPairs()][$this->filterIndex($index)];
235235
}
236236

237-
public function add(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
237+
/**
238+
* @param SfMember|SfMemberInput $member
239+
*/
240+
public function add(string $key, iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
238241
{
239242
$members = $this->members;
240243
$members[MapKey::from($key)->value] = self::filterMember($member);
@@ -265,7 +268,10 @@ public function remove(string|int ...$keys): static
265268
return $this->newInstance($members);
266269
}
267270

268-
public function append(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
271+
/**
272+
* @param SfMember|SfMemberInput $member
273+
*/
274+
public function append(string $key, iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
269275
{
270276
$members = $this->members;
271277
unset($members[$key]);
@@ -274,7 +280,10 @@ public function append(string $key, StructuredField|Token|ByteSequence|DateTimeI
274280
return $this->newInstance($members);
275281
}
276282

277-
public function prepend(string $key, StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
283+
/**
284+
* @param SfMember|SfMemberInput $member
285+
*/
286+
public function prepend(string $key, iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
278287
{
279288
$members = $this->members;
280289
unset($members[$key]);

src/OuterList.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ public function get(string|int $key): StructuredField
156156

157157
/**
158158
* Inserts members at the beginning of the list.
159+
*
160+
* @param SfMember|SfMemberInput ...$members
159161
*/
160-
public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool ...$members): static
162+
public function unshift(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool ...$members): static
161163
{
162164
if ([] === $members) {
163165
return $this;
@@ -168,8 +170,10 @@ public function unshift(StructuredField|Token|ByteSequence|DateTimeInterface|str
168170

169171
/**
170172
* Inserts members at the end of the list.
173+
*
174+
* @param SfMember|SfMemberInput ...$members
171175
*/
172-
public function push(StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool ...$members): static
176+
public function push(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool ...$members): static
173177
{
174178
if ([] === $members) {
175179
return $this;
@@ -181,9 +185,11 @@ public function push(StructuredField|Token|ByteSequence|DateTimeInterface|string
181185
/**
182186
* Inserts members starting at the given index.
183187
*
188+
* @param SfMember|SfMemberInput ...$members
189+
*
184190
* @throws InvalidOffset If the index does not exist
185191
*/
186-
public function insert(int $key, StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool ...$members): static
192+
public function insert(int $key, iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool ...$members): static
187193
{
188194
$offset = $this->filterIndex($key);
189195

@@ -200,7 +206,10 @@ public function insert(int $key, StructuredField|Token|ByteSequence|DateTimeInte
200206
};
201207
}
202208

203-
public function replace(int $key, StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
209+
/**
210+
* @param SfMember|SfMemberInput $member
211+
*/
212+
public function replace(int $key, iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool $member): static
204213
{
205214
if (null === ($offset = $this->filterIndex($key))) {
206215
throw InvalidOffset::dueToIndexNotFound($key);

0 commit comments

Comments
 (0)