Regex.php
<?php
namespace Tlf\Scrawl\Utility;
class Regex {
static public function matchRegexes($targetObject, $regexArray, File $file=null, $text=null){
if ($file === null && $text === null)throw new \Exception("Either \$file or \$text must be set");
if ($text === null)$text = $file->content();
$ret = [];
foreach ($regexArray as $name => $regs){
$func = $regs['function'] ?? str_replace(['-','.'], '_', $name);
unset($regs['function']);
foreach ($regs as $i => $regex){
$didMatch = preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
if ($didMatch){
$info = [
'matchList'=>$matches,
'lineStart' => -1,
'lineEnd' => -1,
'text' => $text,
'regIndex' => null,
];
foreach ($matches as $key => $match){
$info['regIndex']=$key;
$lineStart = 0;
$lineEnd = 0;
$info['lineStart'] = $lineStart;
$info['lineEnd'] = $lineEnd;
$ret[] = call_user_func([$targetObject, $func], $name, $match, $file, $info);
}
}
}
}
return $ret;
}
}