Completed
Push — develop ( b85a0a...7b70ec )
by Jens
03:15
created

TranslationHelper::getOptionValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace JaySDe\HandlebarsBundle\Helper;
7
8
use LightnCandy\SafeString;
9
use Symfony\Component\Translation\TranslatorInterface;
10
11
class TranslationHelper implements HelperInterface
12
{
13
    /**
14
     * @var TranslatorInterface
15
     */
16
    private $translator;
17
18
    /**
19
     * @var string
20
     */
21
    private $defaultNamespace = null;
22
23
    /**
24
     * @var string
25
     */
26
    private $interpolationPrefix = null;
27
28
    /**
29
     * @var string
30
     */
31
    private $interpolationSuffix = null;
32
33 10
    public function __construct(
34
        TranslatorInterface $translator = null,
35
        $defaultNamespace = null,
36
        $interpolationPrefix = '%',
37
        $interpolationSuffix = '%'
38
    ) {
39 10
        $this->translator = $translator;
40 10
        $this->defaultNamespace = $defaultNamespace;
41 10
        $this->interpolationPrefix = $interpolationPrefix;
42 10
        $this->interpolationSuffix = $interpolationSuffix;
43 10
    }
44
45 10
    public function handle($context, $options)
46
    {
47 10
        $options = isset($options['hash']) ? $options['hash'] : [];
48 10
        if (strstr($context, ':')) {
49 7
            list($bundle, $context) = explode(':', $context, 2);
50 7
            $options['bundle'] = $bundle;
51
        }
52
53 10
        $bundle = $this->getOptionValue($options, 'bundle', $this->defaultNamespace);
54 10
        $locale = $this->getOptionValue($options, 'locale');
55 10
        $count = $this->getOptionValue($options, 'count');
56
57 10
        $args = $this->transformOptions($options);
58
59 10
        return new SafeString($this->trans($context, $args, $count, $bundle, $locale));
60
    }
61
62 10
    private function trans($context, $args, $count, $bundle, $locale)
63
    {
64 10
        if (is_null($count)) {
65 7
            $trans = $this->translator->trans($context, $args, $bundle, $locale);
66
        } else {
67 3
            $trans = $this->translator->transChoice($context, $count, $args, $bundle, $locale);
68
        }
69
70 10
        return $trans;
71
    }
72
73 10
    private function transformOptions($options)
74
    {
75 10
        $args = [];
76 10
        foreach ($options as $key => $value) {
77 8
            $key = $this->interpolationPrefix.$key.$this->interpolationSuffix;
78 8
            $args[$key] = $value;
79
        }
80 10
        return $args;
81
    }
82
83 10
    private function getOptionValue($options, $key, $default = null)
84
    {
85 10
        return isset($options[$key]) ? $options[$key] : $default;
86
    }
87
}
88