Rules
Every rule is built by a static method of EugeneErg\Assert\Assert. The first argument is the subject; the other arguments can be literals or expressions too.
Each rule below, except the logic ones, also exists as a chain method without the subject: Assert::key('x')->lessThan(5).
Logic
| Rule | Holds when |
|---|---|
all($a, $b, ...) | every rule holds; all failures are reported |
any($a, $b, ...) | at least one rule holds |
not($rule) | the rule does not hold (details) |
each($ref, $rule) | the rule holds for every element |
some($ref, $rule) | the rule holds for at least one element |
when($if, $then, $else = null) | $then holds if $if does, $else otherwise (details) |
optional($ref, $rule) | the key is missing, or the rule holds |
Assert::any(Assert::isNull(Assert::value()), Assert::isInteger(Assert::value()))->violations('x');
// value must be of type null, string given or value must be of type int, string given
Assert::some(Assert::ref('roles'), Assert::equal(Assert::value(), 'admin'))->violations(['roles' => ['user', 'guest']]);
// roles[0] must be identical to "admin" or roles[1] must be identical to "admin"Presence and types
| Rule | Message |
|---|---|
exists($ref) | id must exist |
type($x, 'int|string') | value must be of type int|string, float given |
isString, isInteger, isFloat, isBool, isNull, isArray, isObject, isIterable, isCallable, isNumeric | value must be of type string, int given |
instanceOf($x, Countable::class) | value must be of type Countable, stdClass given |
isA($x, $class, allowString: false) | value must be a "Countable" |
isSubclassOf($x, $class, allowString: false) | value must be a subclass of "Exception" |
Type names follow get_debug_type(): int, float, string, bool, array, null, object, iterable, callable, numeric, scalar, mixed, or a class, interface or enum. An unknown name throws InvalidArgumentException when the rule is built, not when it runs.
isA and isSubclassOf accept a class name given as a string only with allowString: true, as PHP's is_a() does.
Values
| Rule | Message |
|---|---|
equal($x, $expected, strict: true) | value must be identical to 1 (be equal to when not strict) |
in($x, ['draft', 'published'], strict: true) | status must be one of ["draft", "published"] |
empty($x) | value must be empty (empty as PHP's empty() sees it) |
callback($x, fn (mixed $v): bool => ..., 'be even') | n must be even |
Comparison
| Rule | Message |
|---|---|
lessThan($x, $limit) | value must be less than 5 |
lessThanOrEqual($x, $limit) | value must be less than or equal to 5 |
greaterThan($x, $limit) | value must be greater than 5 |
greaterThanOrEqual($x, $limit) | value must be greater than or equal to 5 |
between($x, $from, $to, fromInclusive: true, toInclusive: true) | qty must be greater than or equal to 1 and less than or equal to 10 |
Comparison is strict about kinds: numbers with numbers, strings with strings, dates (DateTimeInterface) with dates. A mix is reported as invalid rather than compared the PHP way:
Assert::lessThan(Assert::ref('qty'), 10)->violations(['qty' => '5']);
// qty must be of type int|float to compare with 10, string givenCast or check isNumeric first if your input comes as strings.
Strings
| Rule | Message |
|---|---|
matchesRegularExpression($x, '/^a/') | value must match "/^a/" |
isRegularExpression($x) | value must be a valid regular expression |
A broken pattern in matchesRegularExpression is reported, not thrown: "/^a" must be a valid regular expression (No ending delimiter '/' found).