Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

List of available predicates

Frank Kleine edited this page Aug 4, 2014 · 9 revisions

CallablePredicate

Wraps a predicate evaluation into a callable.

$startsWithFoo = new CallablePredicate(
        function($value) { return substr($value, 0, 3) === 'foo'; }
);
$startsWithFoo->test('foobarbaz'); // evaluates to true

Contains

Predicate to test that something is contained. This predicate works with any scalar value:

$contains = new Contains(true);
// evaluates to true if $value contains `true`
$contains->test($value);

$contains = new Contains('foo');
// evaluates to true if $value is equal to foo or a string containing the phrase foo
$contains->test($value);

Please note that for boolean values no type conversion is done, the predicate only evaluates to true when the given value is of type boolean and has the same value as the expectation:

$containsTrue = new Contains(true);
// evaluates to false
$containsTrue->test(1);
// evaluates to false
$containsTrue->test('true');

However, for integers it evaluates to true when the given value contains the integer:

$contains5 = new Contains(5);
// evaluates to true
$contains5->test(5);
$contains5->test(55);
$contains5->test(25);
$contains5->test('foo5');
$contains5->test('fo5o');

Equals

Predicate to test that something is equal. It can compare any scalar value with an expected value. The value to test has to be of the same type and must have the same content as the expected value in order for the predicate to evaluate to true.

$equals = new Equals(303);
// evaluates to true if $value contains the integer 303
$equals->test($value);

IsExistingDirectory

Predicate to test that a string denotes an existing directory.

$isExistingDirectory = new IsExistingDirectory('/path/where/relatives/must/reside');
// evaluates to true if the path /path/where/relatives/must/reside/foo exists and is a directory
$isExistingDirectory->test('foo');
// evaluates to true if the path /path/where/relatives/must/reside/path/to/foo exists and is a directory
$isExistingDirectory->test('/path/to/foo');

In case no base directory is specified both global and relative pathes can be evaluated:

$isExistingDirectory = new IsExistingDirectory();
// evaluates to true if the path /path/to/foo exists and is a directory
$isExistingDirectory->test('/path/to/foo');
// evaluates to true if the path getcwd() . '/foo' exists and is a directory
$isExistingDirectory->test('foo');

IsExistingFile

Predicate to test that a string denotes an existing file.

$isExistingFile = new IsExistingFile('/path/where/relatives/must/reside');
// evaluates to true if the path /path/where/relatives/must/reside/foo.txt exists and is a file
$isExistingFile->test('foo.txt');
// evaluates to true if the path /path/where/relatives/must/reside/path/to/foo.txt exists and is a file
$isExistingFile->test('/path/to/foo.txt');

In case no base directory is specified both global and relative pathes can be evaluated:

$isExistingFile = new IsExistingFile();
// evaluates to true if the path /path/to/foo.txt exists and is a file
$isExistingFile->test('/path/to/foo.txt');
// evaluates to true if the path getcwd() . '/foo.txt' exists and is a file
$isExistingFile->test('foo.txt');

IsExistingHttpUri

Predicate to test that a string is an existing http uri, i.e. has a DNS record.

IsHttpUri

Predicate to test that a string is a http uri. Does a syntactical check only, to ensure that the HTTP uri really exists use IsExistingHttpUri instead (see above).

IsIpAddress

Predicate to test that something is an IP address, either v4 or v6.

IsIpV4Address

Predicate to test that something is an IPv4 address.

IsIpV6Address

Predicate to test that something is an IPv6 address.

IsMailAddress

Predicate to test that a string is a mail address.

IsOneOf

Predicate to test a value against a list of allowed values.

$isOneOf = new IsOneOf(['foo', 'bar', 'baz']);
// evaluates to true if $value is either 'foo', 'bar' or 'baz' or
// $value is an array where all values are one of the allowed values
$isOneOf->test($value);

Regex

Predicate to ensure a value complies to a given regular expression. The predicate uses preg_match() and checks if the value occurs exactly one time. Please make sure that the supplied regular expression contains correct delimiters, they will not be applied automatically. The test() method throws a stubbles\lang\exception\RuntimeException in case the regular expression is invalid.

$regex = new Regex('/^([a-z]{3})$/');
// evaluates to true if $value satisfies the regular expression
$regex->test($value);