Katina - The PHP Output Validator
, (*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
-
- {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)