Skip to content

Commit

Permalink
Simplify Value validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 3, 2024
1 parent b818225 commit 6a40baa
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions src/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,14 @@ public function __construct(Item|Token|Bytes|DisplayString|DateTimeInterface|int
$value instanceof DisplayString => [$value, $value->type()],
false === $value,
$value => [$value, Type::Boolean],
$value instanceof DateTimeInterface => [self::filterDate($value), Type::Date],
$value instanceof DateTimeInterface => [
!$value instanceof DateTimeImmutable ? DateTimeImmutable::createFromInterface($value) : $value,
Type::fromVariable($value),
],
default => [$value, Type::fromVariable($value)],
};
}

/**
* Filter a date according to draft-ietf-httpbis-sfbis-latest.
*
* @see https://httpwg.org/http-extensions/draft-ietf-httpbis-sfbis.html#section-3.3.7
*/
private static function filterDate(DateTimeInterface $value): DateTimeImmutable
{
return match (true) {
!Type::Integer->supports($value->getTimestamp()) => throw new SyntaxError('Date timestamp are limited to 15 digits.'),
!$value instanceof DateTimeImmutable => DateTimeImmutable::createFromInterface($value),
default => $value,
};
}

/**
* Returns a new instance from an encoded byte sequence and an iterable of key-value parameters.
*/
Expand Down

0 comments on commit 6a40baa

Please sign in to comment.