TranslateViewHelper::render()   B
last analyzed

Complexity

Conditions 10
Paths 66

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 16
c 1
b 0
f 0
nc 66
nop 0
dl 0
loc 27
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}