<?php
namespace App\Utils\BaseParser\TextCleaner; class TextCleaner { protected $rules = []; public function appendRule(Rule $rule) { $this->rules[]= [ 'class' => get_class($rule), 'obj' => $rule, 'hits' => 0, ]; return $this; } public function simplifyRules() { $this->rules = $this->internal_simplifyRules($this->getRules()); return $this; } public function getRules() { return $this->rules; } protected function internal_simplifyRules(array $rules) { $rules = array_uunique($rules, function (array $a, array $b) { $ret = 0; if (!$ret) { $ret = strcmp(get_class($a['obj']), get_class($b['obj'])); } if (!$ret) { $ret = strcmp($a['obj']->lineMatch, $b['obj']->lineMatch); } if (!$ret) { $ret = strcmp($a['obj']->search, $b['obj']->search); } return $ret; }); usort($rules, function (array $a, array $b) { $ret = 0; if (!$ret) { $ret = strcmp(get_class($b['obj']), get_class($a['obj'])); } if (!$ret) { $ret = strlen($b['obj']->lineMatch) - strlen($a['obj']->lineMatch); } if (!$ret) { $ret = strcmp($b['obj']->lineMatch, $a['obj']->lineMatch); } return $ret; }); return $rules; } public function hasRules() { return (bool)$this->rules; } public function cleanupArrRaw(array $contentArr) { $hitRules = []; $hitLines = []; foreach ($contentArr as $lineIdx=>$line) { foreach ($this->rules as $ruleIndex=>$rule) { if ($rule['obj']->isMatch($line)) { $nline = $rule['obj']->replace($line); $this->rules[$ruleIndex]['hits']++; $hitRules[$ruleIndex]++; $hitLines[$lineIdx][] = $ruleIndex; $contentArr[$lineIdx] = $nline; $line = $nline; } } } return [$contentArr, $hitRules, $hitLines]; } public function cleanupArr(array $contentArr) { list($contentArr, , ) = $this->cleanupArrRaw($contentArr); return $contentArr; } } 