2017 © Pedro Peláez
 

library katina

Test helper class that validates any kind of output based on the rules passed. Useful to test API reponses or data that changes over time.

image

mascame/katina

Test helper class that validates any kind of output based on the rules passed. Useful to test API reponses or data that changes over time.

  • Friday, July 8, 2016
  • by marcmascarell
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3,322 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 7 % Grown

The README.md

Katina - The PHP Output Validator

Latest Version Travis Software License, (*1)

Test helper class that validates any kind of output based on the passed rules. Useful to test API reponses or data that changes over time., (*2)

A modernized version of ptrofimov/matchmaker package., (*3)

Installation

composer require mascame/katina --dev, (*4)

Basic usage

$data = [
    'name' => 'John Doe',
    'count' => 123,
];

$validator = new \Mascame\Katina\Validator(['name' => ':string', 'count' => ':int']);

$validator->check($data); // true

Advanced usage

$data = [
    'name' => 'John Doe',
    'count' => 145,
    'list' => [
        'emptyList' => false,
        'nested-list' => [
            1, 2, 3
        ]
    ],
    'books' => [
        [
            'type' => 'book',
            'title' => 'Geography book',
            'chapters' => [
                'eu' => ['title' => 'Europe', 'interesting' => true],
                'as' => ['title' => 'America', 'interesting' => false]
            ]
        ],
        [
            'type' => 'book',
            'title' => 'Foreign languages book',
            'chapters' => [
                'de' => ['title' => 'Deutsch']
            ]
        ]
    ]
];

$requiredFields = [
    'name' => ':string',
    'count' => ':int',
    'list' => [
        'emptyList' => ':bool',
        'nested-list' => [
            ':int'
        ]
    ],
    'books' => [
        '*' => [
            'type' => 'book',
            'title' => ':string contains(book)',
            'chapters' => [
                ':string length(2) {1,3}' => [
                    'title' => ':string',
                    'interesting?' => ':bool',
                ]
            ]
        ]
    ]
];

$optionalFields = [
    'something' => 'boolean'
];

$validator = new \Mascame\Katina\Validator($requiredFields, $optionalFields);

$validator->check($data); // true

Adding rules

$data = [
    'my-birthday' => '1980-01-01'
];

$requiredFields = ['my-birthday' => ':birthdayValidator'];

$validator = new \Mascame\Katina\Validator($requiredFields);

// You can add or override rules
\Mascame\Katina\Rules::setRules(['birthdayValidator' => function($value) {
    return ($value == '1980-01-01');
}]);

$validator->check($data); // true

Matching rules

Matching rules are strings that start with ':'. You can use multiple matchers joined with space., (*5)

Matcher could be any callable (name of function or closure). You can add your own rules or replace standard ones., (*6)

  • General, (*7)

    • empty
    • nonempty
    • required
    • in(a, b, ...)
    • mixed
    • any
  • Types, (*8)

    • array
    • bool
    • boolean
    • callable
    • double
    • float
    • int
    • integer
    • long
    • numeric
    • number
    • object
    • real
    • resource
    • scalar
    • string
  • Numbers, (*9)

    • gt(n)
    • gte(n)
    • lt(n)
    • lte(n)
    • negative
    • positive
    • between(a, b)
  • Strings, (*10)

    • alnum
    • alpha
    • cntrl
    • digit
    • graph
    • lower
    • print
    • punct
    • space
    • upper
    • xdigit
    • regexp(pattern)
    • email
    • url
    • ip
    • length(n)
    • min(n)
    • max(n)
    • contains(needle)
    • starts(s)
    • ends(s)
    • json
    • date
  • Arrays, (*11)

    • count(n)
    • keys(key1, key2, ...)
  • Objects, (*12)

    • instance(class)
    • property(name, value)
    • method(name, value)

For more details click here, (*13)

Quantifiers for keys

  • ! - one key required (default)
  • ? - optional key
      • any count of keys
  • {3} - strict count of keys
  • {1,5} - range

For matchers (i.e. ':string') default quantifier is *, (*14)

Using Matcher as standalone

You can directly use Matcher:, (*15)

\Mascame\Katina\Matcher::matches(['test' => true], ['test' => ':bool']); // true

Contributing

Thank you for considering contributing! You can contribute at any time forking the project and making a pull request., (*16)

Support

If you need help or any kind of support, please send an e-mail to Marc Mascarell at marcmascarell@gmail.com., (*17)

License

Credit to Petr Trofimov for his package ptrofimov/matchmaker which this package is heavily based on., (*18)

MIT, (*19)

The Versions

08/07 2016

dev-master

9999999-dev

Test helper class that validates any kind of output based on the rules passed. Useful to test API reponses or data that changes over time.

  Sources   Download

MIT

The Development Requires

by Marc Mascarell

08/07 2016

v2.0.0

2.0.0.0

Test helper class that validates any kind of output based on the rules passed. Useful to test API reponses or data that changes over time.

  Sources   Download

MIT

The Development Requires

by Marc Mascarell

07/07 2016

v1.0.0

1.0.0.0

Test helper class that validates any kind of output based on the rules passed. Useful to test API reponses or data that changes over time.

  Sources   Download

MIT

The Development Requires

by Marc Mascarell