Skip to content

Commit af2fe4e

Browse files
committed
Bugfix Item instantiation
1 parent 899a151 commit af2fe4e

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
All Notable changes to `bakame/http-strucured-fields` will be documented in this file.
44

5+
## [1.2.1](https://github.com/bakame-php/http-structured-fields/compare/1.2.0...1.2.1) - 2024-01-01
6+
7+
### Added
8+
9+
- None
10+
11+
### Fixed
12+
13+
- `Item::new` is fixed to better handle parsing with parameters values.
14+
15+
### Deprecated
16+
17+
- None
18+
19+
### Removed
20+
21+
- None
22+
523
## [1.2.0](https://github.com/bakame-php/http-structured-fields/compare/1.1.0...1.2.0) - 2023-12-30
624

725
### Added

src/Item.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3
1717
*
1818
* @phpstan-import-type SfItem from StructuredField
19+
* @phpstan-import-type SfType from StructuredField
1920
* @phpstan-import-type SfItemInput from StructuredField
21+
* @phpstan-import-type SfTypeInput from StructuredField
2022
* @phpstan-type SfItemPair array{0:ByteSequence|Token|DisplayString|DisplayString|DateTimeInterface|string|int|float|bool, 1:MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>}
2123
*/
2224
final class Item implements ParameterAccess, ValueAccess
@@ -58,8 +60,8 @@ public static function fromAssociative(ByteSequence|Token|DisplayString|DateTime
5860

5961
/**
6062
* @param array{
61-
* 0:ByteSequence|Token|DisplayString|DisplayString|DateTimeInterface|string|int|float|bool,
62-
* 1:MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>
63+
* 0: SfType,
64+
* 1: MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>
6365
* } $pair
6466
*
6567
* @throws SyntaxError If the pair or its content is not valid.
@@ -76,10 +78,16 @@ public static function fromPair(array $pair): self
7678
/**
7779
* Returns a new bare instance from value.
7880
*
81+
* @param SfTypeInput|array{0:SfType, 1:MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>} $value
82+
*
7983
* @throws SyntaxError If the value is not valid.
8084
*/
81-
public static function new(ByteSequence|Token|DisplayString|DateTimeInterface|string|int|float|bool $value): self
85+
public static function new(mixed $value): self
8286
{
87+
if (is_array($value)) {
88+
return self::fromPair($value);
89+
}
90+
8391
return self::fromValue(new Value($value));
8492
}
8593

src/Value.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Exception;
1111
use Stringable;
1212
use Throwable;
13+
use ValueError;
1314

1415
use function abs;
1516
use function date_default_timezone_get;
@@ -33,7 +34,10 @@ final class Value
3334
public readonly Token|ByteSequence|DisplayString|DateTimeImmutable|int|float|string|bool $value;
3435
public readonly Type $type;
3536

36-
public function __construct(ValueAccess|Token|ByteSequence|DisplayString|DateTimeInterface|int|float|string|bool $value)
37+
/**
38+
* @throws ValueError
39+
*/
40+
public function __construct(mixed $value)
3741
{
3842
$this->value = match (true) {
3943
$value instanceof ValueAccess => $value->value(),
@@ -45,9 +49,10 @@ public function __construct(ValueAccess|Token|ByteSequence|DisplayString|DateTim
4549
$value instanceof DateTimeInterface => self::filterDate($value),
4650
is_int($value) => self::filterIntegerRange($value, 'Integer'),
4751
is_float($value) => self::filterDecimal($value),
48-
default => self::filterString($value),
52+
is_string($value) => self::filterString($value),
53+
default => throw new ValueError('Unknown or unsupported type.')
4954
};
50-
$this->type = Type::fromVariable($value);
55+
$this->type = Type::fromVariable($this->value);
5156
}
5257

5358
/**

tests/DataTypeTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,37 @@ public function it_will_generate_the_structured_field_text_represenation_for_lis
6262
OuterList::fromPairs($data)->toHttpValue() /* @phpstan-ignore-line */
6363
);
6464
}
65+
66+
#[Test]
67+
public function it_will_build_the_structured_fields_from_pairs(): void
68+
{
69+
$field = DataType::Dictionary->build([
70+
['a',
71+
[
72+
[
73+
[1, []],
74+
[2, []],
75+
],
76+
[],
77+
],
78+
],
79+
]);
80+
81+
self::assertSame('a=(1 2)', $field);
82+
}
83+
84+
#[Test]
85+
public function it_will_build_the_structured_fields_from_simplified_item(): void
86+
{
87+
$field = DataType::Dictionary->build([
88+
['a',
89+
[
90+
[1, 2],
91+
[],
92+
],
93+
],
94+
]);
95+
96+
self::assertSame('a=(1 2)', $field);
97+
}
6598
}

0 commit comments

Comments
 (0)