PHP code example of alleyinteractive / laminas-validator-extensions

1. Go to this page and download the library: Download alleyinteractive/laminas-validator-extensions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alleyinteractive / laminas-validator-extensions example snippets




class Float extends \Laminas\Validator\AbstractValidator
{
    const FLOAT = 'float';

    protected $messageTemplates = [
        self::FLOAT => "'%value%' is not a floating point value",
    ];

    public function isValid($value)
    {
        $this->setValue($value);

        if (! is_float($value)) {
            $this->error(self::FLOAT);
            return false;
        }

        return true;
    }
}



class Float extends \Alley\Validator\ExtendedAbstractValidator
{
    const FLOAT = 'float';

    protected $messageTemplates = [
        self::FLOAT => "'%value%' is not a floating point value",
    ];

    public function testValue($value): void
    {
        if (! is_float($value)) {
            $this->error(self::FLOAT);
        }
    }
}



class Float extends \Alley\Validator\FreeformValidator
{
    public function testValue($value): void
    {
        if (! is_float($value)) {
            $this->error('float', 'Please enter a floating point value');
        }
    }
}



$valid = new \Alley\Validator\AnyValidator([new \Laminas\Validator\LessThan(['max' => 10])]);
$valid->attach(new \Laminas\Validator\GreaterThan(['min' => 90]));

$valid->isValid(9); // true
$valid->isValid(99); // true
$valid->isValid(42); // false

$valid = new \Alley\Validator\FastFailValidatorChain([new \Laminas\Validator\LessThan(['max' => 10])]);
$valid->attach(new \Laminas\Validator\GreaterThan(['min' => 90]));

$valid->isValid(42); // false
count($valid->getMessages()); // 1



$images = \Alley\WP\match_blocks(
    $post,
    [
        'name' => 'core/image',
        'attrs' => [
            [
                'key' => 'credit',
                'value' => '/(The )?Associated Press/i',
                'operator' => 'REGEX',
            ],
        ],
    ],
);

$valid = new \Alley\Validator\ValidatorByOperator('REGEX', '/^foo/');
$valid->isValid('foobar'); // true

$valid = new \Alley\Validator\ValidatorByOperator('NOT IN', ['bar', 'baz']);
$valid->isValid('bar'); // false

$valid = new \Alley\Validator\ValidatorByOperator('!==', 42);
$valid->isValid(43); // true



$valid = new \Alley\Validator\AlwaysValid();
$valid->isValid(42); // true
$valid->isValid(false); // true
$valid->isValid('abcdefghijklmnopqrstuvwxyz'); // true



$valid = new \Alley\Validator\Comparison(
    [
        'operator' => '<=',
        'compared' => 100,
    ]
);
$valid->isValid(101); // false

$valid = new \Alley\Validator\Comparison(
    [
        'operator' => '!==',
        'compared' => false,
    ]
);
$valid->isValid(true); // true



$valid = new \Alley\Validator\ContainsString(
    [
        'needle' => 'foo',
    ],
);

$valid->isValid('foobar'); // true
$valid->isValid('barbaz'); // false



$valid = new \Alley\Validator\DivisibleBy(
    [
        'divisor' => 3,
    ],
);

$valid->isValid(9); // true
$valid->isValid(10); // false



$origin = new \Alley\Validator\OneOf(['haystack' => ['foo', 'bar']]);
$valid = new \Alley\Validator\Not($origin, 'The input was invalid.');

$valid->isValid('foo'); // false
$valid->isValid('baz'); // true



$valid = new \Alley\Validator\OneOf(['haystack' => ['one', 'two', 'three']]);
$valid->isValid('four'); // false
$valid->getMessages(); // ['notOneOf' => 'Must be one of [one, two, three] but is four.']



$valid = new \Alley\Validator\Type(['type' => 'callable']);
$valid->isValid('date_create_immutable'); // true

$valid = new \Alley\Validator\Type(['type' => 'bool']);
$valid->isValid([]); // false



$origin = new \Laminas\Validator\GreaterThan(42);
$valid = new \Alley\Validator\WithMessage('tooSmall', 'Please enter a number greater than 42.', $origin);

$valid->isValid(41); // false
$valid->getMessages(); // ['tooSmall' => 'Please enter a number greater than 42.']