Skip to content

Commit b6730b1

Browse files
committed
Improve internal codebase
1 parent 66815d9 commit b6730b1

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,12 @@ the changes applied and leave the original instance unchanged.
297297
`Dictionary` and `Parameters` exhibit the following modifying methods:
298298

299299
```php
300-
$map->add($key, $value): static;
301-
$map->append($key, $value): static;
302-
$map->prepend($key, $value): static;
300+
$map->add(string $key, $value): static;
301+
$map->append(string $key, $value): static;
302+
$map->prepend(string $key, $value): static;
303303
$map->mergeAssociative(...$others): static;
304304
$map->mergePairs(...$others): static;
305-
$map->remove(...$key): static;
305+
$map->remove(string ...$key): static;
306306
```
307307

308308
#### Lists
@@ -351,7 +351,7 @@ $list->remove(int ...$key): static;
351351

352352
#### Adding and updating parameters
353353

354-
To ease working with instance that have a `Parameters` object attached to the following
354+
To ease working with instance that have a `Parameters` object attached to, the following
355355
public API is added. It is also possible to instantiate an `InnerList` or an `Item`
356356
instance with included parameters using one of these named constructors:
357357

@@ -361,11 +361,30 @@ use Bakame\Http\StructuredFields\Item;
361361

362362
//@type SfItemInput ByteSequence|Token|DateTimeInterface|Stringable|string|int|float|bool
363363

364-
Item::fromAssociative(SfItemInput $value, iterable<string, SfItemInput>|Parameters $parameters = []): self;
365-
Item::fromPair(array{0:SfItemInput, 1:iterable<array{0:string, 1:SfItemInput}>|Parameters} $pair): self;
364+
Item::fromAssociative(SfItemInput $value, Parameters|iterable<string, SfItemInput> $parameters = []): self;
365+
Item::fromPair(array{0:SfItemInput, 1:Parameters|iterable<array{0:string, 1:SfItemInput}>} $pair): self;
366366

367-
InnerList::fromAssociative(iterable<SfItemInput> $members, iterable<string, SfItemInput>|Parameters $parameters): self;
368-
InnerList::fromPair(array{0:iterable<SfItemInput>, iterable<array{0:string, 1:SfItemInput}>|Parameters} $pair): self;
367+
InnerList::fromAssociative(iterable<SfItemInput> $value, Parameters|iterable<string, SfItemInput> $parameters): self;
368+
InnerList::fromPair(array{0:iterable<SfItemInput>, Parameters|iterable<array{0:string, 1:SfItemInput}>} $pair): self;
369+
```
370+
371+
The following example illustrate how to use those methods:
372+
373+
```php
374+
use Bakame\Http\StructuredFields\Dictionary;
375+
use Bakame\Http\StructuredFields\Item;
376+
377+
echo Item::fromAssociative(
378+
Token::fromString('bar'),
379+
['baz' => 42]
380+
)->toHttpValue(), PHP_EOL;
381+
382+
echo Item::fromPair([
383+
Token::fromString('bar'),
384+
[['baz', 42]],
385+
])->toHttpValue(), PHP_EOL;
386+
387+
//both methods return `bar;baz=42`
369388
```
370389

371390
Both classes allow return their respective pair representation via the `toPair` method.

src/InnerList.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ private static function filterMember(mixed $member): object
5050
};
5151
}
5252

53+
/**
54+
* Returns an instance from an HTTP textual representation.
55+
*
56+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.1
57+
*/
58+
public static function fromHttpValue(Stringable|string $httpValue): self
59+
{
60+
return self::fromAssociative(...Parser::parseInnerList($httpValue));
61+
}
62+
5363
/**
5464
* Returns a new instance.
5565
*/
@@ -86,22 +96,16 @@ public static function fromPair(array $pair): self
8696
/**
8797
* Returns a new instance with an iter.
8898
*
89-
* @param iterable<SfItemInput> $members
99+
* @param iterable<SfItemInput> $value
90100
* @param iterable<string, SfItemInput> $parameters
91101
*/
92-
public static function fromAssociative(iterable $members, iterable $parameters = []): self
102+
public static function fromAssociative(iterable $value, iterable $parameters = []): self
93103
{
94-
return new self(Parameters::fromAssociative($parameters), $members);
95-
}
104+
if (!$parameters instanceof Parameters) {
105+
$parameters = Parameters::fromAssociative($parameters);
106+
}
96107

97-
/**
98-
* Returns an instance from an HTTP textual representation.
99-
*
100-
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.1
101-
*/
102-
public static function fromHttpValue(Stringable|string $httpValue): self
103-
{
104-
return self::fromAssociative(...Parser::parseInnerList($httpValue));
108+
return new self($parameters, $value);
105109
}
106110

107111
public function parameters(): Parameters

0 commit comments

Comments
 (0)