PHP code example of yuloh / pattern-recognition

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

    

yuloh / pattern-recognition example snippets


['x' => 1]            -> A
['x' => 1, 'y' => 1]  -> B
['x' => 1, 'y' => 2 ] -> C

['x' => 1]           -> A
['x' => 2]           -> no match
['x' => 1, 'y' => 1] -> B
['x' => 1, 'y' => 2] -> C
['x' => 2, 'y' => 2] -> no match
['y' => 1]           -> no match

$pm = new Yuloh\PatternRecognition\Matcher;

$pm
    ->add(['a' => 1], 'A')
    ->add(['b' => 2], 'B');

$pm->find(['a' => 1]); // returns 'A'
$pm->find(['a' => 2]); // returns null
$pm->find(['a' => 1, 'b' => 1]); // returns 'A'. 'b' => 1 is ignored, it was never registered.

$pm = (new Matcher())
    ->add(['a' => 0], 'A')
    ->add(['c' => 2], 'C')
    ->add(['a' => 0, 'b' => 1], 'AB')
    ->(['a' => '*'], 'AG')
    ->add([], 'R');

$pm->find(['a' => 0, 'b' => 1]); // 'AB', because more specific matches beat less specific matches.
$pm->find(['a' => 0, 'c' => 2]); // 'A', because a comes before c and keys are checked in alphabetical order. 
$pm->find(['a' => 0]); // 'A' as exact match, because exact matches are more specific than globs.
$pm->find(['a' => 2]); // 'AG' as glob match, as matches are more specific than root matches.
$pm->find(['b' => 2]); // 'R', as root match.

add(array $pattern, mixed $data)

remove(array $pattern)

find(array $pattern)