Skip to content

Commit

Permalink
Merge pull request #11690 from o0h/hash-matches-strict-compare-numeri…
Browse files Browse the repository at this point in the history
…c-value

Hash matches strict compare numeric value
  • Loading branch information
markstory committed Feb 5, 2018
2 parents 9e7bdbc + e70013f commit 2787d60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Utility/Hash.php
Expand Up @@ -267,15 +267,17 @@ protected static function _matches($data, $selector)
$prop = $prop ? '1' : '0';
} elseif ($isBool) {
$prop = $prop ? 'true' : 'false';
} elseif (is_numeric($prop)) {
$prop = (string)$prop;
}

// Pattern matches and other operators.
if ($op === '=' && $val && $val[0] === '/') {
if (!preg_match($val, $prop)) {
return false;
}
} elseif (($op === '=' && $prop != $val) ||
($op === '!=' && $prop == $val) ||
} elseif (($op === '=' && $prop !== $val) ||
($op === '!=' && $prop === $val) ||
($op === '>' && $prop <= $val) ||
($op === '<' && $prop >= $val) ||
($op === '>=' && $prop < $val) ||
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Utility/HashTest.php
Expand Up @@ -1421,6 +1421,26 @@ public function testExtractMatchesNull()
$this->assertEquals($expected, $result);
}

/**
* Test extracting value-zero contained data based on attributes with string
*
* @return void
*/
public function testExtractAttributeStringWithDataContainsZero()
{
$data = [
['value' => '0'],
['value' => 0],
['value' => 'string-value'],
];

$expected = [
['value' => 'string-value'],
];
$result = Hash::extract($data, '{n}[value=string-value]');
$this->assertSame($expected, $result);
}

/**
* Test that uneven keys are handled correctly.
*
Expand Down

0 comments on commit 2787d60

Please sign in to comment.