PHP code example of c0de8 / matchmaker

1. Go to this page and download the library: Download c0de8/matchmaker 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/ */

    

c0de8 / matchmaker example snippets



use C0DE8\MatchMaker\Manager;

use C0DE8\MatchMaker\Exception\InvalidValueTypeException;
use C0DE8\MatchMaker\Exception\KeyMatcherFailException;
use C0DE8\MatchMaker\Exception\KeyMatchFailException;
use C0DE8\MatchMaker\Exception\MatcherException;
 
$books = [
    [
        'type'     => 'book',
        'title'    => 'Geography book',
        'chapters' => [
            'eu' => ['title' => 'Europe',  'interesting' => true],
            'as' => ['title' => 'America', 'interesting' => false]
        ],
        'price'    => 19.99
    ],
    [
        'type'     => 'book',
        'title'    => 'Foreign languages book',
        'chapters' => [
            'de' => ['title' => 'Deutsch']
        ],
        'price'    => 29.99
    ]
];

$pattern = [
    '*' => [
        'type'     => 'book',
        'title'    => ':string contains(book)',
        'chapters' => [
            ':string length(2) {1,3}' => [
                'title'        => ':string',
                'interesting?' => ':bool',
            ]
        ],
        'price'    => ':float'
    ]
];
 
try {
 
    (new Manager)->matchAgainst($books, $pattern); // return true (otherwise throws an exception)
 
} catch (\InvalidArgumentException $excetpion) {
 
    echo $exception->getMessage();
 
} catch (InvalidValueTypeException $excetpion) {
 
    echo $exception->getMessage();
 
} catch (KeyMatcherFailException $excetpion) {
 
    echo $exception->getMessage();
 
} catch (MatcherException $excetpion) {
 
    echo $exception->getMessage();
 
} catch (\Exception $excetpion) {
 
    echo $exception->getMessage();
}