From 81e94b818b9e1e12e97aa4055a5422538e59de4f Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Mon, 1 Jan 2024 14:41:32 +0100 Subject: [PATCH] Bugfix Item instantiation --- src/Item.php | 14 +++++++++++--- src/Value.php | 11 ++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Item.php b/src/Item.php index d44f329..afeeff4 100644 --- a/src/Item.php +++ b/src/Item.php @@ -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|iterable} */ final class Item implements ParameterAccess, ValueAccess @@ -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|iterable + * 0: SfType, + * 1: MemberOrderedMap|iterable * } $pair * * @throws SyntaxError If the pair or its content is not valid. @@ -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|iterable} $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)); } diff --git a/src/Value.php b/src/Value.php index 9da888e..4a35289 100644 --- a/src/Value.php +++ b/src/Value.php @@ -10,6 +10,7 @@ use Exception; use Stringable; use Throwable; +use ValueError; use function abs; use function date_default_timezone_get; @@ -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(), @@ -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); } /**