2017 © Pedro Peláez
 

library array-validator

image

sieg/array-validator

  • Friday, May 25, 2018
  • by sieg
  • Repository
  • 0 Watchers
  • 1 Stars
  • 24 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 20 % Grown

The README.md

Array Validator

Build Status Quality Gate Status Coverage Technical Debt Packagist, (*1)

Simple form data / any array validation tool., (*2)

  • Component validates an array by provided rules list.
  • Its possible to use multiple configurations of one rule for one field in one validation run.
  • Not dependant on other third party components.
  • Tested with PHP 7.3 and up, including PHP 8.0. Fits PSR-12 coding style.

Installation

Installation via composer:, (*3)

composer require fresh-advance/array-validator

Usage example

use Sieg\ArrayValidator\Keys;
use Sieg\ArrayValidator\Rule;
use Sieg\ArrayValidator\RuleCase;
use Sieg\ArrayValidator\RuleCaseCollection;
use Sieg\ArrayValidator\Validator;

$configurationExample = new RuleCaseCollection(
    new RuleCase(
        new Keys\All(),
        new Rule\LengthRange(5, 7)
    ),
    new RuleCase(
        new Keys\Collection('field1', 'field3'),
        new Rule\Expression('/value\d+/')
    ),
    new RuleCase(
        new Keys\Expression('/2$/'),
        new Rule\Expression('/value\d+/'),
        'Special message'
    )
);

$dataExample = [
    'field1' => 'value1',
    'field2' => 'something'
];

$validator = new Validator($configurationExample);
$errors = $validator->validate($dataExample);
if (empty($errors)) {
    // array fits validation configuration
    echo 'ok';
} else {
    print_r($errors);
}

Gives validation errors with fields as keys:, (*4)

Array
(
    [field2] => Array
        (
            [0] => VALIDATOR_RULE_LENGTH_TOO_HIGH
            [1] => Special message
        )

    [field3] => Array
        (
            [0] => VALIDATOR_RULE_EXPRESSION_MATCH_FAILED
        )
)

Predefined Rules

There are some basic rules implemented with the component:, (*5)

  • Callback(closure $closure), (*6)

    • Takes Closure as parameter. $key and $data will be sent to Closure.
  • EqualsTo(mixed $value), (*7)

    • Check if value is equal to Rule $value parameter.
  • EqualsToKey(string $key), (*8)

    • Check if value is equal to other key value.
  • Expression(string $regex), (*9)

    • Takes regex as parameter.
  • Filter(int $filterRule, array $filterOptions), (*10)

    • Rule uses filter_var function for validating the value.
    • Takes PHP filter constants to apply as first param:
    • FILTER_VALIDATE_EMAIL
    • FILTER_VALIDATE_FLOAT
    • FILTER_VALIDATE_INT
    • FILTER_VALIDATE_IP
    • FILTER_VALIDATE_MAC
    • FILTER_VALIDATE_REGEXP
    • FILTER_VALIDATE_URL
    • Takes filter_var options array as second param.
    • Refer to filter_var function documentation for more information
  • InArray(array $choices), (*11)

  • Length(int $length), (*12)

  • LengthRange(int $min, int $max), (*13)

  • Max(int $max), (*14)

  • MaxLength(int $max), (*15)

  • Min(int $min), (*16)

  • MinLength(int $min), (*17)

  • Range(int $min, int $max), (*18)

  • Required
    • Check if the field exists and not empty

Create and use custom Rules

  • Custom rule should extend \Sieg\ArrayValidator\Rule\AbstractRule
  • Use it as regular rules whose comes with the component.
  • Validator catches \Sieg\ArrayValidator\Exception\RuleFailed type Exceptions for setting field error messages.

The Versions

25/05 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

24/05 2018

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

14/01 2018

v0.5.2

0.5.2.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

06/12 2017

v0.5.1

0.5.1.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

05/12 2017

v0.5.0

0.5.0.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

27/11 2017

v0.4.0

0.4.0.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

26/11 2017

v0.3.0

0.3.0.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

26/11 2017

dev-another_query_structure

dev-another_query_structure

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

20/11 2017

v0.2.0

0.2.0.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya

15/11 2017

v0.1.0

0.1.0.0

  Sources   Download

MIT

The Development Requires

by Anton Fedurtsya