-
Notifications
You must be signed in to change notification settings - Fork 2
Pattern
Pieter Hordijk edited this page Jan 1, 2019
·
1 revision
Validates the input (string
) to match against a PCRE regex pattern (string
).
Available since: 1.0.0
<?php declare(strict_types);
use HarmonyIO\Validation\Rule\Pattern\Regex;
(new Regex('~foo~i'))->validate('Foo bar baz');
Note: direct use of this validator is discouraged. Instead use it inside your own custom validators.
<?php declare(strict_types);
use Amp\Promise;
use HarmonyIO\Validation\Rule\Pattern\Regex;
use HarmonyIO\Validation\Rule\Rule;
use function Amp\call;
use function HarmonyIO\Validation\fail;
use function HarmonyIO\Validation\succeed;
class Volume implements Rule
{
/**
* {@inheritdoc}
*/
public function validate($value): Promise
{
return call(function () use ($value) {
/** @var Result $result */
$result = yield (new Regex('~^\d+x\d+x\d+$~'))->validate($value);
if ($result->isValid()) {
return succeed();
}
return fail('Custom.Volume');
});
}
}
(new Volume())->validate('13x10x10');
-
Type.String
when the validated value is not a string -
Pattern.Regex
when the validated value does not match the supplied PCRE regex pattern