Skip to content

Commit

Permalink
Improve Bare Item check
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 3, 2024
1 parent 18fa2d0 commit 390c4b7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
14 changes: 10 additions & 4 deletions src/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ private function filterMember(mixed $member): Item
{
if ($member instanceof StructuredFieldProvider) {
$member = $member->toStructuredField();
if (!$member instanceof Item) {
throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a '.Item::class.'; '.$member::class.' given.');
}

return $member;
}

return match (true) {
$member instanceof Item => $member,
default => Item::new($member),
};
if (!$member instanceof Item) {
return Item::new($member);
}

return $member;
}

/**
Expand Down
33 changes: 14 additions & 19 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,13 @@ public static function false(): self
*/
public function value(?callable $validate = null): Bytes|Token|DisplayString|DateTimeImmutable|string|int|float|bool
{
$value = $this->value;
if (null === $validate) {
return $value;
return $this->value;
}

$exceptionMessage = $validate($value);
$exceptionMessage = $validate($this->value);
if (true === $exceptionMessage) {
return $value;
return $this->value;
}

if (!is_string($exceptionMessage) || '' === trim($exceptionMessage)) {
Expand Down Expand Up @@ -379,25 +378,21 @@ public function toHttpValue(?Ietf $rfc = Ietf::Rfc9651): string
*
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-4.1
*/
private function serialize(?Ietf $rfc = Ietf::Rfc9651): string
private function serialize(?Ietf $rfc = null): string
{
$rfc ??= Ietf::Rfc9651;
if (!$rfc->supports($this->type)) {
throw MissingFeature::dueToLackOfSupport($this->type(), $rfc);
}

$value = $this->value;

return match (true) {
$value instanceof DateTimeImmutable => '@'.$value->getTimestamp(),
$value instanceof Token => $value->toString(),
$value instanceof Bytes => ':'.$value->encoded().':',
$value instanceof DisplayString => '%"'.$value->encoded().'"',
is_int($value) => (string) $value,
is_float($value) => (string) json_encode(round($value, 3, PHP_ROUND_HALF_EVEN), JSON_PRESERVE_ZERO_FRACTION),
$value,
false === $value => '?'.($value ? '1' : '0'),
default => '"'.preg_replace('/(["\\\])/', '\\\$1', $value).'"',
!$rfc->supports($this->type) => throw MissingFeature::dueToLackOfSupport($this->type, $rfc),
$this->value instanceof DateTimeImmutable => '@'.$this->value->getTimestamp(),
$this->value instanceof Token => $this->value->toString(),
$this->value instanceof Bytes => ':'.$this->value->encoded().':',
$this->value instanceof DisplayString => '%"'.$this->value->encoded().'"',
is_int($this->value) => (string) $this->value,
is_float($this->value) => (string) json_encode(round($this->value, 3, PHP_ROUND_HALF_EVEN), JSON_PRESERVE_ZERO_FRACTION),
$this->value,
false === $this->value => '?'.($this->value ? '1' : '0'),
default => '"'.preg_replace('/(["\\\])/', '\\\$1', $this->value).'"',
};
}

Expand Down
19 changes: 14 additions & 5 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,22 @@ private static function filterMember(mixed $member): Item
{
if ($member instanceof StructuredFieldProvider) {
$member = $member->toStructuredField();
if ($member instanceof Item) {
return self::filterMember($member);
}

throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a '.Item::class.'; '.$member::class.' given.');
}

return match (true) {
!$member instanceof Item => Item::new($member),
$member->parameters()->isEmpty() => $member,
default => throw new InvalidArgument('The "'.$member::class.'" instance is not a Bare Item.'),
};
if (!$member instanceof Item) {
$member = Item::new($member);
}

if ($member->parameters()->isNotEmpty()) {
throw new InvalidArgument('The "'.$member::class.'" instance is not a Bare Item.');
}

return $member;
}

/**
Expand Down

0 comments on commit 390c4b7

Please sign in to comment.