Passed
Push — master ( 16f071...f55a40 )
by Timo
27:02
created

TranslateViewHelper::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0046

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1.0046
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\Core\Parser\SyntaxTree\ViewHelperNode;
19
use TYPO3\CMS\Fluid\ViewHelpers\TranslateViewHelper as CoreTranslateViewHelper;
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
 * @package ApacheSolrForTypo3\Solr\ViewHelpers
28
 */
29
class TranslateViewHelper extends CoreTranslateViewHelper
30
{
31
    /**
32
     * @var bool
33
     */
34
    protected $escapeChildren = true;
35
36
    /**
37
     * @return string|null
38
     */
39 34
    public function render()
40
    {
41 34
        $result = parent::render();
42 34
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
43 34
        $result = vsprintf($result, $this->arguments['arguments']);
44 34
        return $result;
45
    }
46
47
    /**
48
     * Wrapper call to static LocalizationUtility
49
     *
50
     * @param string $id Translation Key compatible to TYPO3 Flow
51
     * @param string $extensionName UpperCamelCased extension key (for example BlogExample)
52
     * @param array $arguments Arguments to be replaced in the resulting string
53
     * @param string $languageKey Language key to use for this translation
54
     * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
55
     *
56
     * @return string|null
57
     */
58 34
    public static function translateAndReplaceMarkers($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
59
    {
60 34
        $result = LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
61 34
        $result = self::replaceTranslationPrefixesWithAtWithStringMarker($result);
62 34
        $result = vsprintf($result, $arguments['arguments']);
63 34
        return $result;
64
    }
65
66
    /**
67
     * @param string $argumentsName
68
     * @param string $closureName
69
     * @param string $initializationPhpCode
70
     * @param ViewHelperNode $node
71
     * @param TemplateCompiler $compiler
72
     * @return string
73
     */
74 4
    public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
75
    {
76 4
        return sprintf(
77 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()',
78 4
            static::class,
79 4
            $argumentsName,
80 4
            $closureName
81
        );
82
    }
83
84
    /**
85
     * @param $result
86
     * @return mixed
87
     */
88 36
    protected static function replaceTranslationPrefixesWithAtWithStringMarker($result)
89
    {
90 36
        if (strpos($result, '@') !== false) {
91 29
            $result = preg_replace('~\"?@[a-zA-Z]*\"?~', '%s', $result);
92
        }
93 36
        return $result;
94
    }
95
}
96