Skip to content

Pattern

Pieter Hordijk edited this page Jan 1, 2019 · 1 revision

TOC

Regex

Validates the input (string) to match against a PCRE regex pattern (string).

Version information

Available since: 1.0.0

Usage

<?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');

Failure reasons

  • Type.String when the validated value is not a string
  • Pattern.Regex when the validated value does not match the supplied PCRE regex pattern
Clone this wiki locally