Completed
Push — master ( 008773...8be1a5 )
by Filip
9s
created

TemplateHelpers::translateFilterAware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 6
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Translation;
12
13
use Kdyby;
14
use Latte\Engine;
15
use Latte\Runtime\FilterInfo;
16
use Nette;
17
18
19
20
/**
21
 * @author Filip Procházka <[email protected]>
22
 */
23
class TemplateHelpers extends Nette\Object
24
{
25
26
	/**
27
	 * @var ITranslator
28
	 */
29
	private $translator;
30
31
32
33
	public function __construct(ITranslator $translator)
34
	{
35
		$this->translator = $translator;
36
	}
37
38
39
40
	public function register(Engine $engine)
41
	{
42
		if (class_exists('Latte\Runtime\FilterInfo')) {
43
			$engine->addFilter('translate', [$this, 'translateFilterAware']);
44
		} else {
45
			$engine->addFilter('translate', [$this, 'translate']);
46
		}
47
		$engine->addFilter('getTranslator', [$this, 'getTranslator']);
48
	}
49
50
51
52
	/**
53
	 * @return ITranslator
54
	 */
55
	public function getTranslator()
56
	{
57
		return $this->translator;
58
	}
59
60
61
62
	public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL)
63
	{
64 View Code Duplication
		if (is_array($count)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
			$locale = $domain ?: NULL;
66
			$domain = $parameters ?: NULL;
67
			$parameters = $count ?: [];
68
			$count = NULL;
69
		}
70
71
		return $this->translator->translate($message, $count, (array) $parameters, $domain, $locale);
0 ignored issues
show
Unused Code introduced by
The call to ITranslator::translate() has too many arguments starting with (array) $parameters.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
72
	}
73
74
75
76
	public function translateFilterAware(FilterInfo $filterInfo, $message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL)
77
	{
78
		return $this->translate($message, $count, $parameters, $domain, $locale);
79
	}
80
81
82
83
	/**
84
	 * @deprecated
85
	 */
86
	public function loader($method)
87
	{
88
		if (method_exists($this, $method) && strtolower($method) !== 'register') {
89
			return [$this, $method];
90
		}
91
	}
92
93
}
94