<?php
namespace App\Utils\BaseParser\Utils; class WildcardMatcher { use \App\Utils\BaseParser\SingletonTrait; public function matchList(string $elementToBeMatched, array $patternsList) { $tmpWildcardReplacement = "__WILDCARD_REPLACEMENT__"; foreach ($patternsList as $pattern) { $regexp = preg_replace('/(?<!\\\\)\\*/', $tmpWildcardReplacement, $pattern); if ($regexp === null) { throw new \Exception\Regexp\Base(sprintf("[%s] Invalid regexp generated for pattern: '%s'", __METHOD__, $pattern)); } $regexp = preg_quote($regexp, "/"); $regexp = str_replace(['\\\\*', $tmpWildcardReplacement, ], ['*', '(.+)', ], $regexp); $matched = preg_match("/^{$regexp}$/i", $elementToBeMatched); if ($matched === false) { throw new \Exception\Regexp\Base(sprintf("[%s] Invalid regexp: '%s'", __METHOD__, $regexp)); } if ($matched === 1) { return true; } } return false; } } 