Skip to content

Commit

Permalink
Add other validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Firehed committed Aug 19, 2021
1 parent f5625bc commit c4d9d4a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
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);
}
}

0 comments on commit c4d9d4a

Please sign in to comment.