PHP code example of ilsenem / matcher

1. Go to this page and download the library: Download ilsenem/matcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ilsenem / matcher example snippets


use Matcher\Schema;

// $schema = [];
// $data   = [];

$matcher = new Schema($schema);

if (!$matcher->match($data)) {
    print_r($matcher->getErrors());
}

$schema = [
    'id'     => 'integer',
    'email'  => 'string',
    'active' => 'boolean',
    'rating' => 'double',
];

$matcher = new Matcher\Schema($schema);

$matcher->match([
    'id'     => 1,
    'email'  => '[email protected]',
    'active' => true,
    'rating' => .5,
]);

$schema = [
    'id'       => 'integer',
    'email'    => 'string',
    'nickname' => '?string',
];

$matcher = new Schema($schema);

$matcher->match([
    'id'       => 7,
    'email'    => '[email protected]',
    'nickname' => null,
]);

$schema = [
    'id'        => 'integer',
    'email'     => 'string',
    '?optional' => 'boolean'
];

$matcher = new Schema($schema);

$matcher->match([
    'id'    => 18,
    'email' => '[email protected]',
]);

$schema = [
    'rules' => 'string => boolean',
];

$matcher = new Schema($schema);

$matcher->match([
    'rules' => [
        'admin.cp'    => true,
        'admin.users' => true,
    ],
]);


$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'  => '[email protected]',
        'active' => true,
        'tokens' => [
            'activation'    => null,
            'authorization' => '0329a06b62cd16b33eb6792be8c60b158d89a2ee3a876fce9a881ebb488c0914',
        ],
        'role' => [
            'id'    => 1,
            'title' => 'Administrator',
        ],
    ],
    [
        'id'     => 2,
        'email'  => '[email protected]',
        'active' => true,
        'tokens' => [
            'activation'    => null,
            'authorization' => null,
        ],
        'role' => [
            'id'    => 2,
            'title' => 'Moderator',
            'rules' => [
                'admin.cp'     => false,
                'moderator.cp' => true,
            ],
        ]
    ],
    [
        'id'     => 87,
        'email'  => '[email protected]',
        '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,
            ]
        ],
    ]
]);

[
    'path.to.*.array.key' => [
        'TYPE_OF_ERROR' => 'Human readable description.',
    ],
    // ...
]