Api‎ > ‎Util‎ > ‎

Match

Class RMatch

RMatch helps verify the contents of any mixed variable according to a definition (expected). The mixed variable can be a complex array, but could also just be an object or a scalar. By default RMatch verifies wheter values are equal but it can be extended to offer other kinds of matchs such as regular expression matches etc.

Example
<?php



require_once('rawdev/RawDev.php');
require_once(RAWDEV_LIB.'/Util/Match.php');

$expected = array('person' => array('name' => 'Jane', 'spouse' => array('name' => 'John', 'address' => array('city' => 'Philadelphia'))));
$value = $expected;

#success
RMatch::construct($expected)->match($value);
print "successnn";

$value = array('person' => array('name' => 'Jane', 'spouse' => array('name' => 'John', 'address' => array('city' => 'New York'))));

#fail
try {
  RMatch::construct($expected)->match($value);
}
catch (RException $e) {
  print $e->getMessage()."n";
}

?>

Vars

var mixed $expected

The value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))).

var bool $partial

By default arrays are matched on all keys, alternatively a selected number of keys can be checked.

var bool $exactType

By default scalar types are not checked, alternatively they can be checked.

Functions

RMatch function __construct($expected, $partial, $exactType)

Creates an match expression object with the definion of the expected value. Alternatively RMatch::construct('RMatch', $expected) can be used.

$expectedmixedThe value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))).
$partialboolTurning this on will loosen the exact match: selected keys in $expected are checked (not all keys are required)
$exactTypeboolTurning this on tighten exact match: all scalar types will need to match exactly (e.g. 1 != "1")
returnsRMatch$value mixed The value to be checked against the expected.

static RMatch function construct($class, $expected, $partial, $exactType) [fluid]

Static helper function that constructs an match expression.

$classstringBy default an RMatch object is created but this can be changed to a different derived class such as RMatchRegex.
$expectedmixedThe value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))).
$partialboolTurning this on will loosen the exact match: selected keys in $expected are checked (not all keys are required)
$exactTypeboolTurning this on tighten exact match: all scalar types will need to match exactly (e.g. 1 != "1")
returnsRMatch$value mixed The value to be checked against the expected.

RMatch function match($value) [fluid]

Determines wheter $value matches. Throws an RException if $value does not match.

$valuemixedMixed value to be evaluated.
returnsRMatchFluid handle to the object.

void function _match($expected, $value, $path)

Private function that check whether value matches the expected outcome. Throws an RException if $value does not match. This function can be called anywhere in the tree. @dependencies construct

$expectedmixedThe value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))). Expected can also be a class name.
$valuemixedThe value to be checked against the expected.
$pathstringA string representation of the variable in the array tree that is evaluated for debugging/reference purposes (e.g. ->address.city).

void function matchScalar($expected, $value, $path)

Checks wheter scalars $expected and $value match.

$expectedmixedThe value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))).
$valuemixedThe value to be checked against the expected.
$pathstringA string representation of the variable in the array tree that is evaluated for debugging/reference purposes (e.g. ->address.city).

void function matchArray($expected, $value, $path)

Checks wheter arrays $expected and $value match. $value can be an object in which case the keys in $expected are compared to the properties in $value.

$expectedmixedThe value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))).
$valuemixedThe value to be checked against the expected.
$pathstringA string representation of the variable in the array tree that is evaluated for debugging/reference purposes (e.g. ->address.city).

void function matchObject($expected, $value, $path)

Checks wheter objects $expected and $value match.

$expectedmixedThe value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))).
$valuemixedThe value to be checked against the expected.
$pathstringA string representation of the variable in the array tree that is evaluated for debugging/reference purposes (e.g. ->address.city).
Comments