-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathNodePathMatcher.php
40 lines (38 loc) · 1.11 KB
/
NodePathMatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Config\Dom;
/**
* Matching of XPath expressions to path patterns
*/
class NodePathMatcher
{
/**
* Whether a subject XPath matches to a given path pattern
*
* @param string $pathPattern Example: '/some/static/path' or '/some/regexp/path(/item)+'
* @param string $xpathSubject Example: '/some[@attr="value"]/static/ns:path'
* @return bool
*/
public function match($pathPattern, $xpathSubject)
{
$pathSubject = $this->simplifyXpath($xpathSubject);
$pathPattern = '#^' . $pathPattern . '$#';
return (bool)preg_match($pathPattern, $pathSubject);
}
/**
* Strip off predicates and namespaces from the XPath
*
* @param string $xpath
* @return string
*/
protected function simplifyXpath($xpath)
{
$result = $xpath;
$result = preg_replace('/\[@[^\]]+?\]/', '', $result);
$result = preg_replace('/\/[^:]+?\:/', '/', $result);
return $result;
}
}