xepozz / matcher
There is no license information available for the latest version (dev-master) of this package.
dev-master
2024-11-10 20:59 UTC
Requires
- php: ^8.2
Requires (Dev)
- phpunit/phpunit: ^11.4
This package is auto-updated.
Last update: 2024-11-10 21:00:04 UTC
README
A way to match values against a pattern.
Installation
composer require xepozz/matcher
Usage
Basic concept
A matcher is an object that is filled with a value and callbacks that will be called if the value matches the pattern.
Basic example
use Xepozz\Matcher\StringMatcher; /** * Each with* method returns a new instance of the matcher with the added condition. * So that means that the original matcher is not changed and you must use the returned instance. */ $matcher = (new StringMatcher('foo')) // or StringMatcher::of('foo') ->withLengthGreaterThan(2) ->withLengthLessThan(10) ->withContains('fo') ->withEndsWith('oo') ->withStartsWith('fo') ->with(function (string $value) { return $value === 'foo'; // callback for any custom checks }); $matcher->matches(); // true
Combining matchers
use Xepozz\Matcher\LogicMatcher;use Xepozz\Matcher\StringMatcher; $logicMatcher = new LogicMatcher(); $stringMatcher = new StringMatcher('bar'); $stringMatcher->matches(); // true $logicMatcher->not($stringMatcher) ->matches(); // false $stringMatcher->matches(); // true $logicMatcher->or($logicMatcher->not($stringMatcher), $stringMatcher) ->matches(); // true $logicMatcher->or($logicMatcher->not($stringMatcher), $logicMatcher->not($stringMatcher)) ->matches(); // false $logicMatcher->and($stringMatcher, $logicMatcher->not($stringMatcher)) ->matches(); // false