diff --git a/src/Utils/TwigBuilder.php b/src/Utils/TwigBuilder.php index 7888848..41e0e4d 100644 --- a/src/Utils/TwigBuilder.php +++ b/src/Utils/TwigBuilder.php @@ -214,6 +214,8 @@ private function refactorConditionPart($condition) { $condition = str_replace('.length', '|length', $condition); $condition = str_replace('.trim', '|trim', $condition); +// $condition = $this->convertConcat($condition); + foreach (Replacements::getConstants() as $constant => $value) { $condition = str_replace($value, Replacements::getSanitizedConstant($constant), $condition); } @@ -223,9 +225,74 @@ private function refactorConditionPart($condition) { public function refactorTextNode(string $content): string { - $content = str_replace('.length', '|length', $content); - $content = str_replace('.trim', '|trim', $content); + $refactoredContent = ''; + $charsCount = mb_strlen($content, 'UTF-8'); + $open = false; + $lastChar = null; + $quoteChar = null; + $buffer = ''; + for ($i = 0; $i < $charsCount; $i++) { + $char = mb_substr($content, $i, 1, 'UTF-8'); + if ($open === false) { + $refactoredContent .= $char; + if ($char === '{' && $lastChar === '{') { + $open = true; + } + } else { + $buffer .= $char; + if ($quoteChar === null && ($char === '"' || $char === '\'')) { + $quoteChar = $char; + } elseif ($quoteChar === $char && $lastChar !== '\\') { + $quoteChar = null; + } + if ($quoteChar === null && $char === '}' && $lastChar === '}') { + $open = false; + $buffer = $this->convertTemplateString(trim($buffer, '}')); + $refactoredContent .= $this->refactorCondition($buffer) . '}}'; + $buffer = ''; + } + } + $lastChar = $char; + } + + return $refactoredContent; + } + + private function convertConcat($content) { + if (preg_match_all('/(\S*)(\s*\+\s*(\S+))+/', $content, $matches, PREG_SET_ORDER )) { + foreach ($matches as $match) { + $parts = explode('+', $match[0]); + $lastPart = null; + $convertedContent = ''; + foreach ($parts as $part) { + $part = trim($part); + if ($lastPart !== null) { + if (is_numeric($lastPart) && is_numeric($part)) { + $convertedContent .= ' + ' . $part; + } else { + $convertedContent .= ' ~ ' . $part; + } + } else { + $convertedContent = $part; + } + $lastPart = $part; + } + $content = str_replace($match[0], $convertedContent, $content); + } + } + + return $content; + } + + private function convertTemplateString($content) { + if (preg_match_all('/\`([^\`]+)\`/', $content, $matches, PREG_SET_ORDER )) { + foreach ($matches as $match) { + $match[1] = str_replace('${', '\' ~ ', $match[1]); + $match[1] = str_replace('}', ' ~ \'', $match[1]); + $content = str_replace($match[0], '\'' . $match[1] . '\'', $content); + } + } return $content; } diff --git a/tests/TextNodeTest.php b/tests/TextNodeTest.php new file mode 100644 index 0000000..37469de --- /dev/null +++ b/tests/TextNodeTest.php @@ -0,0 +1,67 @@ +