-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |