TranslateViewHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 51
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 27 10
A initializeArguments() 0 8 1
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package Flow
7
 */
8
9
10
namespace Aimeos\Shop\ViewHelper;
11
12
13
use Neos\FluidAdaptor\Core\ViewHelper\Exception;
14
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
15
16
17
/**
18
 * Aimeos translations view helper
19
 *
20
 * @package Flow
21
 * @subpackage ViewHelper
22
 */
23
class TranslateViewHelper extends AbstractViewHelper
24
{
25
	protected $escapeChildren = false;
26
27
28
	/**
29
	 * Registers the known arguments
30
	 */
31
	public function initializeArguments()
32
	{
33
		$this->registerArgument( 'singular', 'string', 'Singular form of the text to translate' );
34
		$this->registerArgument( 'plural', 'string', 'Plural form of the text, used if $number is greater than one', false );
35
		$this->registerArgument( 'number', 'integer', 'Amount of things relevant for the plural form', false );
36
		$this->registerArgument( 'escape', 'boolean', 'If translated string should be escaped', false );
37
		$this->registerArgument( 'domain', 'string', 'Translation domain from core or an extension', false );
38
		$this->registerArgument( 'arguments', 'array', 'Arguments to be replaced in the resulting string', false );
39
	}
40
41
42
	/**
43
	 * Returns the translated string for the given phrase
44
	 *
45
	 * @return mixed Translated string
46
	 */
47
	public function render()
48
	{
49
		$iface = '\Aimeos\MW\View\Iface';
50
		$view = $this->templateVariableContainer->get( '_aimeos_view' );
51
52
		if( !is_object( $view ) || !( $view instanceof $iface ) ) {
53
			throw new Exception( 'Aimeos view object is missing' );
54
		}
55
56
		if( !isset( $this->arguments['singular'] ) ) {
57
			throw new Exception( 'Attribute "singular" missing for Aimeos translate view helper' );
58
		}
59
60
		$singular = $this->arguments['singular'];
61
		$plural = ( isset( $this->arguments['plural'] ) ? $this->arguments['plural'] : '' );
62
		$number = ( isset( $this->arguments['number'] ) ? $this->arguments['number'] : 1 );
63
		$escape = ( isset( $this->arguments['escape'] ) ? $this->arguments['escape'] : true );
64
		$domain = ( isset( $this->arguments['domain'] ) ? $this->arguments['domain'] : 'client' );
65
		$values = ( isset( $this->arguments['arguments'] ) ? $this->arguments['arguments'] : array() );
66
67
		$string = vsprintf( $view->translate( $domain, $singular, $plural, $number ), (array) $values );
68
69
		if( $escape === false ) {
70
			return $string;
71
		}
72
73
		return $view->encoder()->html( $string );
74
	}
75
}