dev-master
9999999-dev https://github.com/ilsenem/matcherArray schema matcher
MIT
The Development Requires
by Ilya Nemytchenko
v1.0.0
1.0.0.0 https://github.com/ilsenem/matcherArray schema matcher
MIT
The Development Requires
by Ilya Nemytchenko
Wallogit.com
2017 © Pedro Peláez
Array schema matcher
Simple schema matcher for arrays., (*1)
Install as Composer dependency:, (*2)
composer require ilsenem/matcher
In your code:, (*3)
use Matcher\Schema;
// $schema = [];
// $data = [];
$matcher = new Schema($schema);
if (!$matcher->match($data)) {
print_r($matcher->getErrors());
}
Supported types for values are:, (*4)
$schema = [
'id' => 'integer',
'email' => 'string',
'active' => 'boolean',
'rating' => 'double',
];
$matcher = new Matcher\Schema($schema);
$matcher->match([
'id' => 1,
'email' => 'some@domain.zone',
'active' => true,
'rating' => .5,
]);
Add ? before type declaration to mark value as nullable., (*5)
$schema = [
'id' => 'integer',
'email' => 'string',
'nickname' => '?string',
];
$matcher = new Schema($schema);
$matcher->match([
'id' => 7,
'email' => 'leeroy.jenkins@wow.lol',
'nickname' => null,
]);
Add ? before key to skip matching if key is not present in data., (*6)
$schema = [
'id' => 'integer',
'email' => 'string',
'?optional' => 'boolean'
];
$matcher = new Schema($schema);
$matcher->match([
'id' => 18,
'email' => 'tired.to.fake@emails.zone',
]);
If an array have no strict schema but follow typings for key and value, you could
set composite key => value type for it:, (*7)
$schema = [
'rules' => 'string => boolean',
];
$matcher = new Schema($schema);
$matcher->match([
'rules' => [
'admin.cp' => true,
'admin.users' => true,
],
]);
You could nest schema one into another to match complex structures and match array collections:, (*8)
$schema = [ '*' => [ // Many users in collection 'id' => 'integer', 'email' => 'string', 'active' => 'boolean', 'tokens' => [ // Nest more rules 'activation' => '?string', 'authorization' => '?string', ], 'role' => [ 'id' => 'integer', 'title' => 'string', 'superuser' => 'boolean', '?rules' => 'string => boolean', ], '?orders' => [ '*' => [ 'id' => 'integer', 'quantity' => 'integer', 'price' => 'double', ], ], ], ]; $matcher = new Schema($schema); $matcher->match([ [ 'id' => 1, 'email' => 'admin@domain.zone', 'active' => true, 'tokens' => [ 'activation' => null, 'authorization' => '0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914', ], 'role' => [ 'id' => 1, 'title' => 'Administrator', ], ], [ 'id' => 2, 'email' => 'moderator@domain.zone', 'active' => true, 'tokens' => [ 'activation' => null, 'authorization' => null, ], 'role' => [ 'id' => 2, 'title' => 'Moderator', 'rules' => [ 'admin.cp' => false, 'moderator.cp' => true, ], ] ], [ 'id' => 87, 'email' => 'customer@domain.zone', 'active' => true, 'tokens' => [ 'activation' => null, 'authorization' => null, ], 'role' => [ 'id' => 3, 'title' => 'Customer', 'rules' => [ 'admin.cp' => false, 'moderator.cp' => false, ], ], 'orders' => [ [ 'id' => 873, 'quantity' => 7, 'price' => 18.99, ], [ 'id' => 1314, 'quantity' => 19, 'price' => 1.97, ] ], ] ]);
With $matcher->getErrors() after matching you could get array of errors:, (*9)
[
'path.to.*.array.key' => [
'TYPE_OF_ERROR' => 'Human readable description.',
],
// ...
]
Types of errors:, (*10)
Schema::ERR_COLLECTION_DEFINITION - Definition of the collection must be the only definition of the level.Schema::ERR_KEY_NOT_FOUND - The key defined in the schema is not found in the data.Schema::ERR_TYPE_UNKNOWN - Unknown type given in schema.Schema::ERR_TYPE_MISMATCH - Type mismatch for any type of declaration.MIT., (*11)
Array schema matcher
MIT
Array schema matcher
MIT