<?php
namespace App\Worldspan\FileParser;

class RuleLineRegexpAndComplexReplacement extends \App\Utils\BaseParser\TextCleaner\Rule
{
    public function isMatch($line)
    {
        return preg_match("/{$this->lineMatch}/", $line);
    }

    public function replace(string $line)
    {
        $searchRegexp = "";
		foreach (str_split($this->search, 1) as $idx=>$char) {
			$searchRegexp.= "{$char}(\s*)";
		}

        $replaceArr = str_split($this->replace, 1);
        $matches = [];
        preg_match("/{$searchRegexp}/", $line, $matches);
        foreach (array_slice($matches, 1) as $idx=>$m) {
            if ($idx<count($replaceArr)) {
                $replaceArr[$idx].= $m;
            }
        }
        return str_replace($matches[0], implode("", $replaceArr), $line);
    }
}
