Completed
Push — develop ( ed4bc2...e50d1b )
by Peter
02:24
created

Value::inLoop()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 24
rs 8.5125
cc 5
eloc 15
nc 4
nop 1
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-2016 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Manipulator\Plugin;
12
13
use DOMNode;
14
use WebinoDraw\Dom\Element;
15
use WebinoDraw\Exception;
16
use Zend\View\Helper\EscapeHtml;
17
18
/**
19
 * Class Value
20
 */
21
class Value extends AbstractPlugin implements InLoopPluginInterface
22
{
23
    /**
24
     * @var EscapeHtml
25
     */
26
    protected $escapeHtml;
27
28
    /**
29
     * @param EscapeHtml $escapeHtml
30
     */
31
    public function __construct(EscapeHtml $escapeHtml)
32
    {
33
        $this->escapeHtml = $escapeHtml;
34
    }
35
36
    /**
37
     * @param PluginArgument $arg
38
     */
39
    public function inLoop(PluginArgument $arg)
40
    {
41
        $spec = $arg->getSpec();
42
        if (!array_key_exists('value', $spec) || null === $spec['value']) {
43
            return;
44
        }
45
46
        $node = $arg->getNode();
47
        if (!($node instanceof DOMNode)) {
48
            throw new Exception\LogicException('Expected node of type ' . DOMNode::class);
49
        }
50
51
        $varTranslation  = $arg->getVarTranslation();
52
        $translatedValue = $arg->getHelper()->translateValue($spec['value'], $varTranslation, $spec);
53
        $node->nodeValue = $this->escapeHtml->__invoke($varTranslation->removeVars($translatedValue));
54
55
        $varKey = $varTranslation->makeVar($varTranslation->makeExtraVarKey($node::NODE_VALUE_PROPERTY));
56
        $varTranslation->offsetSet($varKey, $node->nodeValue);
57
58
        ($node instanceof Element)
59
            and $varTranslation->merge(
60
                $this->createNodeHtmlTranslation($node, $spec)->getVarTranslation()->getArrayCopy()
61
            );
62
    }
63
}
64