final class HasPregCaptures extends Constraint

Constraint that accepts arrays of matches returned from preg_match() having capture groups as specified in the array of expectations.

Checks only entries present in the array of expectations. Special values may be used in the expectations:

  • ['foo' => false] asserts that group 'foo' was not captured,
  • ['foo' => true] asserts that group 'foo' was captured,
  • ['foo' => 'FOO'] asserts that group 'foo' was captured and it's value equals 'FOO'.

Boolean expectations (['foo' => true] or ['foo' => false]) work properly only with arrays obtained from preg_match() invoked with PREG_UNMATCHED_AS_NULL flag.

Methods

static HasPregCaptures
create(array $expected, int $flags = PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL)

Initializes the constraint.

string
toString()

Returns a string representation of the constraint.

bool|null
evaluate(mixed $other, string $description = '', bool $returnResult = false)

Evaluates the constraint for parameter $other.

bool
matches(mixed $other)

Evaluates the constraint for parameter $other. Returns true if the constraint is met, false otherwise.

string
failureDescription(mixed $other)

Returns the description of the failure.

Details

at line 70
static HasPregCaptures create(array $expected, int $flags = PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL)

Initializes the constraint.

Parameters

array $expected

an array of expected values

int $flags

flags as in preg_match()

Return Value

HasPregCaptures

Exceptions

InvalidArgumentException

at line 80
string toString()

Returns a string representation of the constraint.

Return Value

string

at line 101
bool|null evaluate(mixed $other, string $description = '', bool $returnResult = false)

Evaluates the constraint for parameter $other.

If $returnResult is set to false (the default), an exception is thrown in case of a failure. null is returned otherwise.

If $returnResult is true, the result of the evaluation is returned as a boolean value instead: true in case of success, false in case of a failure.

Parameters

mixed $other
string $description
bool $returnResult

Return Value

bool|null

Exceptions

ExpectationFailedException

at line 134
protected bool matches(mixed $other)

Evaluates the constraint for parameter $other. Returns true if the constraint is met, false otherwise.

Parameters

mixed $other

value or object to evaluate

Return Value

bool

at line 152
protected string failureDescription(mixed $other)

Returns the description of the failure.

The beginning of failure messages is "Failed asserting that" in most cases. This method should return the second part of that sentence.

Parameters

mixed $other

evaluated value or object

Return Value

string