public static matches ( array $data, mixed $conditions, integer $i = null, integer $length = null ) : boolean | ||
$data | array | An array of data to execute the match on. |
$conditions | mixed | An array of condition strings or an XPath expression. |
$i | integer | Optional: The 'nth'-number of the item being matched. |
$length | integer | |
return | boolean |
public static function matches($data, $conditions, $i = null, $length = null)
{
if (!$conditions) {
return true;
}
if (is_string($conditions)) {
return (bool) static::extract($data, $conditions);
}
foreach ($conditions as $condition) {
if ($condition === ':last') {
if ($i !== $length) {
return false;
}
continue;
} elseif ($condition === ':first') {
if ($i !== 1) {
return false;
}
continue;
}
if (!preg_match('/(.+?)([><!]?[=]|[><])(.*)/', $condition, $match)) {
if (ctype_digit($condition)) {
if ($i !== (int) $condition) {
return false;
}
} elseif (preg_match_all('/(?:^[0-9]+|(?<=,)[0-9]+)/', $condition, $matches)) {
return in_array($i, $matches[0]);
} elseif (!isset($data[$condition])) {
return false;
}
continue;
}
list(, $key, $op, $expected) = $match;
if (!isset($data[$key])) {
return false;
}
$val = $data[$key];
if ($op === '=' && $expected && $expected[0] === '/') {
return preg_match($expected, $val);
} elseif ($op === '=' && $val != $expected) {
return false;
} elseif ($op === '!=' && $val == $expected) {
return false;
} elseif ($op === '>' && $val <= $expected) {
return false;
} elseif ($op === '<' && $val >= $expected) {
return false;
} elseif ($op === '<=' && $val > $expected) {
return false;
} elseif ($op === '>=' && $val < $expected) {
return false;
}
}
return true;
}
public function testMatchesMultipleLevels() { $result = array( 'Attachment' => array( 'keep' => array() ), 'Comment' => array( 'keep' => array('Attachment' => array('fields' => array('attachment'))) ), 'User' => array('keep' => array()), 'Article' => array( 'keep' => array( 'Comment' => array('fields' => array('comment', 'published')), 'User' => array('fields' => array('user')) ) ) ); $this->assertTrue(Set::matches($result, '/Article/keep/Comment')); $result = Set::matches($result, '/Article/keep/Comment/fields/user'); $this->assertFalse($result); }