2017 © Pedro Peláez
 

library pattern-recognition

A pattern matching library for arrays

image

yuloh/pattern-recognition

A pattern matching library for arrays

  • Monday, March 28, 2016
  • by yuloh
  • Repository
  • 0 Watchers
  • 3 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 25 % Grown

The README.md

Pattern Recognition

A pattern matcher for PHP arrays., (*1)

Need to pick out an array based on a subset of its key value pairs? Say you've got:, (*2)

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

Then this library would give you:, (*3)

['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

Quick Example

$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.

Since you are matching a subset, the pattern to find can contain any number of extra key value pairs., (*4)

Install

composer require yuloh/pattern-recognition

Why

Since this library is a port of the javascript library patrun, the rationale is the same. This library lets you build a simple decision tree to avoid writing if statements., (*5)

Rules

  1. More specific matches beat less specific matches. That is, more key value pairs beat fewer.
  2. Array keys are checked in alphabetical order.
  3. Exact matches are more specific than globs.
  4. Matches are more specific than root matches.
$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.

API

add(array $pattern, mixed $data)

Register a pattern, and the data that will be returned if an input matches. Both keys and values are considered to be strings. Other types are converted to strings., (*6)

remove(array $pattern)

Remove this pattern, and it's data, from the matcher., (*7)

find(array $pattern)

Find the given pattern and return it's data., (*8)

jsonSerialize()

Serializes the object to a value that can be serialized natively by json_encode()., (*9)

toArray()

Serializes the matcher to an array of the registered matches and data., (*10)

The Versions

28/03 2016

dev-master

9999999-dev

A pattern matching library for arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

28/03 2016

v0.1.0

0.1.0.0

A pattern matching library for arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires