PHP Classes

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

Recommend this page to a friend!
  Info   View files View files (73)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-06-20 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 40 All time: 10,679 This week: 65Up
Version License PHP version Categories
validator-php 1.0MIT/X Consortium ...5PHP 5, Validation
Description 

Author

Smoren


Contributor

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  
Name: Smoren Freelight <contact>
Classes: 26 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 12x

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  
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

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageChecks (6 files)
Files folder imageExceptions (5 files)
Files folder imageFactories (2 files, 1 directory)
Files folder imageHelpers (2 files)
Files folder imageInterfaces (13 files)
Files folder imageModels (1 file)
Files folder imageRules (10 files)
Files folder imageStructs (5 files)

  Files folder image Files  /  src  /  Checks  
File Role Description
  Plain text file AllOfCheck.php Class Class source
  Plain text file AnyOfCheck.php Class Class source
  Plain text file Check.php Class Class source
  Plain text file CompositeCheck.php Class Class source
  Plain text file RetrospectiveCheck.php Class Class source
  Plain text file WrappedCheck.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file CheckError.php Class Class source
  Plain text file CompositeCheckError.php Class Class source
  Plain text file FormInvalidConfigError.php Class Class source
  Plain text file FormValidationError.php Class Class source
  Plain text file ValidationError.php Class Class source

  Files folder image Files  /  src  /  Factories  
File Role Description
Files folder imageChecks (5 files)
  Plain text file CheckBuilder.php Class Class source
  Plain text file Value.php Class Class source

  Files folder image Files  /  src  /  Factories  /  Checks  
File Role Description
  Plain text file BooleanCheckFactory.php Class Class source
  Plain text file ContainerCheckFactory.php Class Class source
  Plain text file MixedCheckFactory.php Class Class source
  Plain text file NumericCheckFactory.php Class Class source
  Plain text file StringCheckFactory.php Class Class source

  Files folder image Files  /  src  /  Helpers  
File Role Description
  Plain text file ContainerAccessHelper.php Class Class source
  Plain text file TypeHelper.php Class Class source

  Files folder image Files  /  src  /  Interfaces  
File Role Description
  Plain text file BooleanRuleInterface.php Class Class source
  Plain text file CheckInterface.php Class Class source
  Plain text file CheckWrapperInterface.php Class Class source
  Plain text file CompositeRuleInterface.php Class Class source
  Plain text file ContainerRuleInterface.php Class Class source
  Plain text file FloatRuleInterface.php Class Class source
  Plain text file FormInterface.php Class Class source
  Plain text file IntegerRuleInterface.php Class Class source
  Plain text file MixedRuleInterface.php Class Class source
  Plain text file NumericRuleInterface.php Class Class source
  Plain text file StringRuleInterface.php Class Class source
  Plain text file UtilityCheckInterface.php Class Class source
  Plain text file ValidationResultInterface.php Class Class source

  Files folder image Files  /  src  /  Models  
File Role Description
  Plain text file Form.php Class Class source

  Files folder image Files  /  src  /  Rules  
File Role Description
  Plain text file AllOfRule.php Class Class source
  Plain text file AnyOfRule.php Class Class source
  Plain text file BaseRule.php Class Class source
  Plain text file BooleanRule.php Class Class source
  Plain text file ContainerRule.php Class Class source
  Plain text file FloatRule.php Class Class source
  Plain text file IntegerRule.php Class Class source
  Plain text file MixedRule.php Class Class source
  Plain text file NumericRule.php Class Class source
  Plain text file StringRule.php Class Class source

  Files folder image Files  /  src  /  Structs  
File Role Description
  Plain text file CheckName.php Class Class source
  Plain text file CheckWrapper.php Class Class source
  Plain text file Param.php Class Class source
  Plain text file RuleName.php Class Class source
  Plain text file ValidationSuccessResult.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageunit (9 files, 1 directory)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imageFixture (3 files)
  Plain text file AllOfRuleTest.php Class Class source
  Plain text file AnyOfRuleTest.php Class Class source
  Plain text file BooleanTest.php Class Class source
  Plain text file ContainerTest.php Class Class source
  Plain text file FloatTest.php Class Class source
  Plain text file IntegerTest.php Class Class source
  Plain text file MixedTest.php Class Class source
  Plain text file NumericTest.php Class Class source
  Plain text file StringTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  Fixture  
File Role Description
  Plain text file ArrayAccessListFixture.php Class Class source
  Plain text file ArrayAccessMapFixture.php Class Class source
  Plain text file SomeClassFixture.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:40
This week:0
All time:10,679
This week:65Up