Skip to content

Commit

Permalink
Improve internal validation codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 3, 2024
1 parent 0c2295c commit 93225bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
20 changes: 14 additions & 6 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ private static function filterMember(mixed $member): InnerList|Item
{
if ($member instanceof StructuredFieldProvider) {
$member = $member->toStructuredField();
if ($member instanceof Item || $member instanceof InnerList) {
return $member;
}

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

return match (true) {
Expand Down Expand Up @@ -122,6 +127,11 @@ public static function fromPairs(StructuredFieldProvider|Dictionary|Parameters|i
$converter = function (mixed $pair): InnerList|Item {
if ($pair instanceof StructuredFieldProvider) {
$pair = $pair->toStructuredField();
if ($pair instanceof Item || $pair instanceof InnerList) {
return $pair;
}

throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a '.Item::class.' or an '.InnerList::class.'; '.$pair::class.' given.');
}

if ($pair instanceof InnerList || $pair instanceof Item) {
Expand All @@ -140,13 +150,11 @@ public static function fromPairs(StructuredFieldProvider|Dictionary|Parameters|i
return InnerList::new();
}

[$member, $parameters] = match (count($pair)) {
2 => $pair,
1 => [$pair[0], []],
default => throw new SyntaxError('The pair first member is the item value; its second member is the item parameters.'),
};
if (!in_array(count($pair), [1, 2], true)) {
throw new SyntaxError('The pair first member is the item value; its second member is the item parameters.');
}

return is_iterable($member) ? InnerList::fromPair([$member, $parameters]) : Item::fromPair([$member, $parameters]);
return is_iterable($pair[0]) ? InnerList::fromPair($pair) : Item::fromPair($pair);
};

return match (true) {
Expand Down
20 changes: 14 additions & 6 deletions src/OuterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ private function filterMember(mixed $member): InnerList|Item
{
if ($member instanceof StructuredFieldProvider) {
$member = $member->toStructuredField();
if ($member instanceof Item || $member instanceof InnerList) {
return $member;
}

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

return match (true) {
Expand Down Expand Up @@ -96,6 +101,11 @@ public static function fromPairs(StructuredFieldProvider|iterable $pairs): self
$converter = function (mixed $pair): InnerList|Item {
if ($pair instanceof StructuredFieldProvider) {
$pair = $pair->toStructuredField();
if ($pair instanceof Item || $pair instanceof InnerList) {
return $pair;
}

throw new InvalidArgument('The '.StructuredFieldProvider::class.' must provide a '.Item::class.' or an '.InnerList::class.'; '.$pair::class.' given.');
}

if ($pair instanceof InnerList || $pair instanceof Item) {
Expand All @@ -114,13 +124,11 @@ public static function fromPairs(StructuredFieldProvider|iterable $pairs): self
return InnerList::new();
}

[$member, $parameters] = match (count($pair)) {
2 => $pair,
1 => [$pair[0], []],
default => throw new SyntaxError('The pair first member is the item value; its second member is the item parameters.'),
};
if (!in_array(count($pair), [1, 2], true)) {
throw new SyntaxError('The pair first member is the item value; its second member is the item parameters.');
}

return is_iterable($member) ? InnerList::fromPair([$member, $parameters]) : Item::fromPair([$member, $parameters]);
return is_iterable($pair[0]) ? InnerList::fromPair($pair) : Item::fromPair($pair);
};

return match (true) {
Expand Down

0 comments on commit 93225bd

Please sign in to comment.