Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add other validators #20

Draft
wants to merge 1 commit into
base: v3-develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Validators/Dictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Firehed\Input\Validators;

use stdClass;

use function is_array;

class Dictionary implements ValidatorInterface
{
/**
* @param ValidatorInterface[] $required
* @param ValidatorInterface[] $optional
*/
public function __construct(
private array $required,
private array $optional = [],
private bool $rejectUnexpected = true,
) {
}

public function validate(mixed $decoded): Result
{
return match (true) {
is_array($decoded) => $this->validateArray($decoded),
$decoded instanceof stdClass => $this->validateObject($decoded),
};
}

private function validateArray(array $decoded)
{
foreach ($decoded as $key => $value) {
// if in required, force validation
// if in optional, still force validation
// if in neither, examine $rejectUnexpected
}
// check if any optional values are not yet in the output & apply their
// default values
}

private function validateObject(stdClass $decoded)
{
}

public function getDefaultValue()
{
}
}
38 changes: 38 additions & 0 deletions src/Validators/ListOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Firehed\Input\Validators;

use function is_iterable;

/**
* @implements ValidatorInterface<Item[]>
* @template Item
*/
class ListOf implements ValidatorInterface
{
/**
* @param ValidatorInterface<Item> $validator
*/
public function __construct(private ValidatorInterface $validator)
{
}

public function validate(mixed $decoded): Result
{
if (!is_iterable($decoded)) {
return Result::error('Value is not convertable to a list');
}
// Normally array_map would be preferred here, but this avoids having
// to do any additional type juggling (for non-array iterables) or
// re-keying (for ensuring array_is_list(result))
$output = [];
foreach ($decoded as $item) {
$output[] = $this->validator->validate($item);
}
return Result::ok(
array_map(fn ($item) => $item->unwrap(), $output)
);
}
}
27 changes: 27 additions & 0 deletions src/Validators/Nullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Firehed\Input\Validators;

/**
* @template NonNullable
* @implements ValidatorInterface<NonNullable | null>
*/
class Nullable implements ValidatorInterface
{
/**
* @param ValidatorInterface<NonNullable> $validator
*/
public function __construct(private ValidatorInterface $validator)
{
}

public function validate(mixed $decoded): Result
{
if ($decoded === null) {
return Result::ok(null);
}
return $this->validator->validate($decoded);
}
}