Completed
Push — develop ( 7b8839...dac3c0 )
by Peter
11:35
created

Value   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 0
loc 33
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B inLoop() 0 25 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\Manipulator\Plugin;
12
13
use DOMNode;
14
use WebinoDraw\Dom\Element;
15
use WebinoDraw\Exception;
16
use WebinoDraw\View\Helper\EscapeHtmlTrait;
17
18
/**
19
 * Class Value
20
 */
21
class Value extends AbstractPlugin implements InLoopPluginInterface
22
{
23
    use EscapeHtmlTrait;
24
25
    /**
26
     * @param PluginArgument $arg
27
     */
28
    public function inLoop(PluginArgument $arg)
29
    {
30
        $spec = $arg->getSpec();
31
        if (!array_key_exists('value', $spec) || null === $spec['value']) {
32
            return;
33
        }
34
35
        $node = $arg->getNode();
36
        if (!($node instanceof DOMNode)) {
37
            throw new Exception\LogicException('Expected node of type ' . DOMNode::class);
38
        }
39
40
        $varTranslation  = $arg->getVarTranslation();
41
        $translatedValue = $arg->getHelper()->translateValue($spec['value'], $varTranslation, $spec);
42
        $escapeHtml      = $this->getEscapeHtml();
43
        $node->nodeValue = $escapeHtml->__invoke($varTranslation->removeVars($translatedValue));
44
45
        $varKey = $varTranslation->makeVar($varTranslation->makeExtraVarKey($node::NODE_VALUE_PROPERTY));
46
        $varTranslation->offsetSet($varKey, $node->nodeValue);
47
48
        ($node instanceof Element)
49
            and $varTranslation->merge(
50
                $this->createNodeHtmlTranslation($node, $spec)->getVarTranslation()->getArrayCopy()
51
            );
52
    }
53
}
54