Translate   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 0
loc 140
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setTranslator() 0 5 1
A drawNodes() 0 17 2
A translateValue() 0 13 2
A translateAttribNodes() 0 15 4
A resolveTextDomain() 0 6 2
A resolveLocale() 0 6 2
A resetVarTranslation() 0 16 5
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Draw\Helper;
12
13
use WebinoDraw\Dom\Attr;
14
use WebinoDraw\Dom\NodeList;
15
use WebinoDraw\VarTranslator\Translation;
16
use WebinoDraw\View\Helper\EscapeHtmlTrait;
17
use Zend\I18n\Translator\TranslatorInterface;
18
19
/**
20
 * Class Translate
21
 */
22
class Translate extends Element
23
{
24
    use EscapeHtmlTrait;
25
26
    /**
27
     * Draw helper service name
28
     */
29
    const SERVICE = 'webinodrawtranslate';
30
31
    /**
32
     * @var TranslatorInterface
33
     */
34
    protected $translator;
35
36
    /**
37
     * @param TranslatorInterface|object $translator
38
     */
39
    public function __construct(TranslatorInterface $translator)
40
    {
41
        $this->translator = $translator;
42
    }
43
44
    /**
45
     * @param TranslatorInterface $translator
46
     * @return $this
47
     */
48
    public function setTranslator(TranslatorInterface $translator)
49
    {
50
        $this->translator = $translator;
51
        return $this;
52
    }
53
54
    /**
55
     * @param NodeList $nodes
56
     * @param array $spec
57
     * @return $this
58
     */
59
    public function drawNodes(NodeList $nodes, array $spec)
60
    {
61
        $this->resetVarTranslation($spec);
62
63
        $remainNodes = $this->translateAttribNodes(
64
            $nodes,
65
            $this->resolveTextDomain($spec),
66
            $this->resolveLocale($spec)
67
        );
68
69
        if (empty($remainNodes)) {
70
            // return early when all nodes were attribs
71
            return $this;
72
        }
73
74
        return parent::drawNodes($nodes->create($remainNodes), $spec);
75
    }
76
77
    /**
78
     * @param string $value
79
     * @param Translation $varTranslation
80
     * @param array $spec
81
     * @return string
82
     */
83
    public function translateValue($value, Translation $varTranslation, array $spec)
84
    {
85
        $varValue = trim(parent::translateValue($value, $varTranslation, $spec));
86
        if (empty($varValue)) {
87
            return '';
88
        }
89
90
        return $this->translator->translate(
91
            $varValue,
92
            $this->resolveTextDomain($spec),
93
            $this->resolveLocale($spec)
94
        );
95
    }
96
97
    /**
98
     * @param NodeList $nodes
99
     * @param string $textDomain
100
     * @param string $locale
101
     * @return array
102
     */
103
    protected function translateAttribNodes(NodeList $nodes, $textDomain, $locale)
104
    {
105
        $escape = $this->getEscapeHtml();
106
        $remainNodes = [];
107
108
        foreach ($nodes as $node) {
109
            if ($node instanceof Attr && !$node->isEmpty()) {
110
                $node->nodeValue = $this->translator->translate($escape($node->nodeValue), $textDomain, $locale);
111
            }
112
113
            $remainNodes[] = $node;
114
        }
115
116
        return $remainNodes;
117
    }
118
119
    /**
120
     * @param array $spec
121
     * @return string
122
     */
123
    protected function resolveTextDomain(array $spec)
124
    {
125
        return !empty($spec['text_domain'])
126
               ? $this->getVarTranslation()->translateString($spec['text_domain'])
127
               : 'default';
128
    }
129
130
    /**
131
     * @param array $spec
132
     * @return string
133
     */
134
    protected function resolveLocale(array $spec)
135
    {
136
        return !empty($spec['locale'])
137
               ? $this->getVarTranslation()->translateString($spec['locale'])
138
               : null;
139
    }
140
141
    /**
142
     * @param array $spec
143
     * @return $this
144
     */
145
    private function resetVarTranslation(array $spec)
146
    {
147
        $varTranslation = $this->getVarTranslation();
148
149
        if (!empty($spec['text_domain']) && $varTranslation->containsVar($spec['text_domain'])) {
150
            $this->setVarTranslation(null);
151
            return $this;
152
        }
153
154
        if (!empty($spec['locale']) && $varTranslation->containsVar($spec['locale'])) {
155
            $this->setVarTranslation(null);
156
            return $this;
157
        }
158
159
        return $this;
160
    }
161
}
162