Skip to content

Commit

Permalink
Bugfix Item instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 1, 2024
1 parent 899a151 commit af2fe4e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

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

## [1.2.1](https://github.com/bakame-php/http-structured-fields/compare/1.2.0...1.2.1) - 2024-01-01

### Added

- None

### Fixed

- `Item::new` is fixed to better handle parsing with parameters values.

### Deprecated

- None

### Removed

- None

## [1.2.0](https://github.com/bakame-php/http-structured-fields/compare/1.1.0...1.2.0) - 2023-12-30

### Added
Expand Down
14 changes: 11 additions & 3 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3
*
* @phpstan-import-type SfItem from StructuredField
* @phpstan-import-type SfType from StructuredField
* @phpstan-import-type SfItemInput from StructuredField
* @phpstan-import-type SfTypeInput from StructuredField
* @phpstan-type SfItemPair array{0:ByteSequence|Token|DisplayString|DisplayString|DateTimeInterface|string|int|float|bool, 1:MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>}
*/
final class Item implements ParameterAccess, ValueAccess
Expand Down Expand Up @@ -58,8 +60,8 @@ public static function fromAssociative(ByteSequence|Token|DisplayString|DateTime

/**
* @param array{
* 0:ByteSequence|Token|DisplayString|DisplayString|DateTimeInterface|string|int|float|bool,
* 1:MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>
* 0: SfType,
* 1: MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>
* } $pair
*
* @throws SyntaxError If the pair or its content is not valid.
Expand All @@ -76,10 +78,16 @@ public static function fromPair(array $pair): self
/**
* Returns a new bare instance from value.
*
* @param SfTypeInput|array{0:SfType, 1:MemberOrderedMap<string, SfItem>|iterable<array{0:string, 1:SfItemInput}>} $value
*
* @throws SyntaxError If the value is not valid.
*/
public static function new(ByteSequence|Token|DisplayString|DateTimeInterface|string|int|float|bool $value): self
public static function new(mixed $value): self
{
if (is_array($value)) {
return self::fromPair($value);
}

return self::fromValue(new Value($value));
}

Expand Down
11 changes: 8 additions & 3 deletions src/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Exception;
use Stringable;
use Throwable;
use ValueError;

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

public function __construct(ValueAccess|Token|ByteSequence|DisplayString|DateTimeInterface|int|float|string|bool $value)
/**
* @throws ValueError
*/
public function __construct(mixed $value)
{
$this->value = match (true) {
$value instanceof ValueAccess => $value->value(),
Expand All @@ -45,9 +49,10 @@ public function __construct(ValueAccess|Token|ByteSequence|DisplayString|DateTim
$value instanceof DateTimeInterface => self::filterDate($value),
is_int($value) => self::filterIntegerRange($value, 'Integer'),
is_float($value) => self::filterDecimal($value),
default => self::filterString($value),
is_string($value) => self::filterString($value),
default => throw new ValueError('Unknown or unsupported type.')
};
$this->type = Type::fromVariable($value);
$this->type = Type::fromVariable($this->value);
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/DataTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,37 @@ public function it_will_generate_the_structured_field_text_represenation_for_lis
OuterList::fromPairs($data)->toHttpValue() /* @phpstan-ignore-line */
);
}

#[Test]
public function it_will_build_the_structured_fields_from_pairs(): void
{
$field = DataType::Dictionary->build([
['a',
[
[
[1, []],
[2, []],
],
[],
],
],
]);

self::assertSame('a=(1 2)', $field);
}

#[Test]
public function it_will_build_the_structured_fields_from_simplified_item(): void
{
$field = DataType::Dictionary->build([
['a',
[
[1, 2],
[],
],
],
]);

self::assertSame('a=(1 2)', $field);
}
}

0 comments on commit af2fe4e

Please sign in to comment.