PHP Classes

PHP Validator Library: Validate an array of values using multiple rules

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 46 All time: 10,798 This week: 82Up
Version License PHP version Categories
validator-php 1.0MIT/X Consortium ...5PHP 5, Validation
Description 

Author

This package can validate an array of values using multiple rules.

It provides a class with several functions that can be called in a chain using a fluent interface to define several rules to validate an array of values.

Currently, the package implements validation rules like:

- Check if the type is numeric, integer, string, float, boolean

- A combination of rules or just a single rule of several rules

Picture of Smoren  Freelight
  Performance   Level  
Innovation award
Innovation award
Nominee: 16x

 

Details

PHP Validation Tools

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

How to install to your project

composer require smoren/validator

Usage

use Smoren\Validator\Factories\Value;
use Smoren\Validator\Exceptions\ValidationError;

$rule = Value::container()
    ->array()
    ->hasAttribute('id', Value::integer()->positive())
    ->hasAttribute('probability', Value::float()->between(0, 1))
    ->hasAttribute('vectors', Value::container()->array()->allValuesAre(
        Value::container()
            ->array()
            ->lengthIs(Value::integer()->equal(2))
            ->allValuesAre(Value::integer())
    ));

$validInput = [
    'id' => 13,
    'probability' => 0.92,
    'vectors' => [[1, 2], [3, 4], [5, 6]],
];

try {
    $rule->validate($validInput);
} catch (ValidationError $e) {
    // Input is valid so this block is unreachable.
}

$invalidInput = [
    'id' => '13',
    'probability' => 1.92,
    'vectors' => [[1, 2.1], [3, 4], [5, 6]],
];

try {
    $rule->validate($invalidInput);
} catch (ValidationError $e) {
    // Input is invalid so we catch the exception.
    print_r($e->getViolatedRestrictions());
    /*
    [
        ['attribute_is', [
            'attribute' => 'id',
            'rule' => 'integer',
            'violated_restrictions' => [
                ['integer', []]
            ]
        ]],
        ['attribute_is', [
            'attribute' => 'probability',
            'rule' => 'float',
            'violated_restrictions' => [
                ['between', [
                    'start' => 0,
                    'end' => 1
                ]]
            ]
        ]],
        ['attribute_is', [
            'attribute' => 'vectors',
            'rule' => 'container',
            'violated_restrictions' => [
                ['all_values_are', [
                    'rule' => 'container',
                    'violated_restrictions' => [
                        ['all_values_are', [
                            'rule' => 'integer',
                            'violated_restrictions' => [
                                ['integer', []]
                            ]
                        ]]
                    ]
                ]]
            ]
        ]]
    ]
    */
}

Unit testing

composer install
composer test-init
composer test

Standards

PHP Validator Tools conforms to the following standards:

License

PHP Validation Tools is licensed under the MIT License.


  Files folder image Files (73)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (8 directories)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:46
This week:0
All time:10,798
This week:82Up