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 81e94b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
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

0 comments on commit 81e94b8

Please sign in to comment.