Skip to content

Commit

Permalink
Add Natural validator
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHash committed Sep 1, 2020
1 parent deba12e commit 8c1e403
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
48 changes: 24 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/Validator/NaturalValidator.php
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
/**
* This file is part of the daikon-cqrs/validize project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Daikon\Validize\Validator;

use Daikon\Interop\Assert;
use Daikon\ValueObject\Natural;

final class NaturalValidator extends Validator
{
/** @param mixed $input */
protected function validate($input): Natural
{
$settings = $this->getSettings();
$min = $settings['min'] ?? 0;
$max = $settings['max'] ?? PHP_INT_MAX;

Assert::that($input)
->integerish('Must be an integer.')
->greaterOrEqualThan($min, "Must be at least $min.")
->lessOrEqualThan($max, "Must be at most $max.");

return Natural::fromNative($input);
}
}

0 comments on commit 8c1e403

Please sign in to comment.