Passed
Push — master ( 6d83d0...d2efa3 )
by Timo
45:45 queued 16:20
created

TranslateViewHelper::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.0787

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 7
cp 0.5714
rs 10
cc 1
nc 1
nop 5
crap 1.0787
1
<?php
2
namespace ApacheSolrForTypo3\Solr\ViewHelpers;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
18
use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper as CoreTranslateViewHelper;
19
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
20
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
21
22
/**
23
 * Class TranslateViewHelper
24
 *
25
 * @author Frans Saris <[email protected]>
26
 * @author Timo Hund <[email protected]>
27
 */
28
class TranslateViewHelper extends CoreTranslateViewHelper
29
{
30
    /**
31
     * @var bool
32
     */
33
    protected $escapeChildren = true;
34
35
    /**
36
     * @return string|null
37
     */
38 35
    public function render()
39
    {
40 35
        $result = parent::render();
41 35
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
42 35
        $result = vsprintf($result, $this->arguments['arguments']);
43 35
        return $result;
44
    }
45
46
    /**
47
     * Wrapper call to static LocalizationUtility
48
     *
49
     * @param string $id Translation Key compatible to TYPO3 Flow
50
     * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
51
     * @param array $arguments Arguments to be replaced in the resulting string
52
     * @param string $languageKey Language key to use for this translation
53
     * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
54
     *
55
     * @return string|null
56
     */
57 35
    public static function translateAndReplaceMarkers($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
58
    {
59 35
        $result = LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
60 35
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
61 35
        $result = vsprintf($result, $arguments['arguments']);
62 35
        return $result;
63
    }
64
65
    /**
66
     * @param string $argumentsName
67
     * @param string $closureName
68
     * @param string $initializationPhpCode
69
     * @param ViewHelperNode $node
70
     * @param TemplateCompiler $compiler
71
     * @return string
72
     */
73 4
    public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
74
    {
75 4
        return sprintf(
76 4
            '\\%1$s::translateAndReplaceMarkers(%2$s[\'key\'] ?? %2$s[\'id\'], %2$s[\'extensionName\'] ?? $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName(), %2$s[\'arguments\'], %2$s[\'languageKey\'], %2$s[\'alternativeLanguageKeys\']) ?? %2$s[\'default\'] ?? %3$s()',
77 4
            static::class,
78
            $argumentsName,
79
            $closureName
80
        );
81
    }
82
83
    /**
84
     * @param $result
85
     * @return mixed
86
     */
87 37
    protected static function replaceTranslationPrefixesWithAtWithStringMarker($result)
88
    {
89 37
        if (strpos($result, '@') !== false) {
90 31
            $result = preg_replace('~\"?@[a-zA-Z]*\"?~', '%s', $result);
91
        }
92 37
        return $result;
93
    }
94
}
95