Download the PHP package nicmart/universal-matcher without Composer
On this page you can find all versions of the php package nicmart/universal-matcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nicmart/universal-matcher
More information about nicmart/universal-matcher
Files in nicmart/universal-matcher
Package universal-matcher
Short Description A rules based matcher engine for php
License MIT
Informations about the package universal-matcher
Universal Matcher
UniversalMatcher
is a library that simplifies and abstracts the construction of custom matchers.
A Matcher acts like a filter that transforms an arbitrary value to another, following the business rules you specify
in the matcher definition. The "match" is intended to be between the input value and the rule that is applied to that value.
For installing instructions (composer!) please go to the end of this README
Changelog
- 0.2.0 Dropped php 5.3 support, PSR-4 autoloading
- 0.1.1 Added matchAll method
Example
Instantiate the matcher, and define some maps:
The FluentFunction
simplifies the construction of maps, but it is completely optional. The number in the
third definition specifies a priority. Default is 0
, so the example above the last map has the highest priority.
Then you define the rules. Each rule is attached to a previously defined map and to an expected map value, and specifies a value that will be returned when the rule matches:
The setDefault
call defines the value taht will be returned when no rule matches.
Now you can use your matcher:
Documentation
Summary
A matcher is defined by a set of maps and a set of rules.
When you invoke the matcher,
the input value is transformed by the registered map with highest priority. If there is
a registered rule for that map that has the expected value (second argument of rule
method)
equal to the transformed value, then the matcher returns the return value of that rule
(third argument of the rule
method).
If no rules match for that map, the matcher will pass to the next (in priority order) map, and so on until there is a rule match.
When the matcher has cycled throughout all the registered maps without finding a matching rule, a default value is returned.
Maps
You register a map with the MapMatcher::defineMap()
method. The first argument is
the map name that the rules will use to refer to the map, and the second is the real map, that
can be any valid php callable:
Priority
defineMap
accepts also a third optional argument to specify a priority. Default is 0
, and the rules
that corresponds to higher priority maps will win. If two maps have the same priority, the first defined wins.
You can retrieve the priority of a registered map with the MapMatcher::priority
method. So
if you want, for example, to be sure to define a map with prioriy higher that the baz
map,
you can do
FluentFunction
With a FluentFunction
you can define and compose more easily some very common callables:
Concatenation is easy too:
Rules
A rule is composed of three arguments: the name of the map that will transform the input value, the expected returned value, and the value to be returned on match.
The order of the rules, unlike the maps definitions, has no effect on matching.
Skip the map definition
If a map is intended to be used with only one rule, you can skip the definition of the map
and directly define the rule with the callbackRule
method:
Default return value
You can set the return value of the matcher when no rules match with the setDefault
method.
Default is null
.
Performance considerations
MapMatcher
has been designed to minimize cycles between rules. Indeed, the cost of a match
is independent on the number of rules, but only on the number of registered maps (and of course
on the cost of each map).
So there should not be issues if the number of rules is high but the number of maps remains low.
Measuring the cost on php array accesses, we have, given the number of maps M
, that
as you can see, the cost is linear on the number of maps.
Where is it used
I use UniversalMatcher
in the compiler definitions
of the DomainSpecificQuery
component. The Universal matcher allowed us to minimize rules checks while mantaining maximum
flexibility on the compiler definition.
Install
The best way to install UniversalMatcher is through composer.
Just create a composer.json file for your project:
Then you can run these two commands to install it:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
or simply run composer install
if you have have already installed the composer globally.
Then you can include the autoloader, and you will have access to the library classes: