AbstractPlugin::createNodeHtmlTranslation()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2568
c 0
b 0
f 0
cc 5
nc 6
nop 2
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 WebinoDraw\Dom\Element;
14
use WebinoDraw\Dom\NodeInterface;
15
use WebinoDraw\VarTranslator\Translation;
16
17
/**
18
 *
19
 */
20
abstract class AbstractPlugin
21
{
22
    /**
23
     * @param NodeInterface $node
24
     * @param PluginArgument $arg
25
     * @return self
26
     */
27
    protected function updateNodeVarTranslation(NodeInterface $node, PluginArgument $arg)
28
    {
29
        $arg->getVarTranslation()->merge($this->createNodeVarTranslationArray($node, $arg->getSpec()));
30
        return $this;
31
    }
32
33
    /**
34
     * @param NodeInterface $node
35
     * @param array $spec
36
     * @return array
37
     */
38
    public function createNodeVarTranslationArray(NodeInterface $node, array $spec)
39
    {
40
        return $this->createNodeTranslation($node, $spec)->getVarTranslation()->getArrayCopy();
41
    }
42
43
    /**
44
     * @param NodeInterface $node
45
     * @param array $spec
46
     * @return Translation
47
     */
48
    public function createNodeTranslation(NodeInterface $node, array $spec)
49
    {
50
        $translation = new Translation($node->getProperties(Translation::EXTRA_VAR_PREFIX));
51
        if (!($node instanceof Element)) {
52
            return $translation;
53
        }
54
55
        $htmlTranslation = $this->createNodeHtmlTranslation($node, $spec);
56
        $translation->merge($htmlTranslation->getArrayCopy());
57
        return $translation;
58
    }
59
60
    /**
61
     * @param Element $node
62
     * @param array $spec
63
     * @return Translation
64
     */
65
    public function createNodeHtmlTranslation(Element $node, array $spec)
66
    {
67
        $translation  = new Translation;
68
        $innerHtmlKey = $translation->makeExtraVarKey('innerHtml');
69
        $outerHtmlKey = $translation->makeExtraVarKey('outerHtml');
70
71
        foreach (['html', 'replace'] as $key) {
72
            if (empty($spec[$key])) {
73
                continue;
74
            }
75
76
            if (false !== strpos($spec[$key], $innerHtmlKey)) {
77
                $translation[$innerHtmlKey] = $node->getInnerHtml();
78
            }
79
80
            if (false !== strpos($spec[$key], $outerHtmlKey)) {
81
                $translation[$outerHtmlKey] = $node->getOuterHtml();
82
            }
83
        }
84
85
        return $translation;
86
    }
87
}
88