NodePathMatcher.php 1.1 KB

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