Cdata::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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-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 DOMElement;
14
use WebinoDraw\Exception;
15
use WebinoDraw\Instructions\InstructionsRenderer;
16
17
/**
18
 *
19
 */
20
class Cdata implements InLoopPluginInterface
21
{
22
    /**
23
     * @var InstructionsRenderer
24
     */
25
    protected $instructionsRenderer;
26
27
    /**
28
     * @param InstructionsRenderer $instructionsRenderer
29
     */
30
    public function __construct(InstructionsRenderer $instructionsRenderer)
31
    {
32
        $this->instructionsRenderer = $instructionsRenderer;
33
    }
34
35
    /**
36
     * @param PluginArgument $arg
37
     */
38
    public function inLoop(PluginArgument $arg)
39
    {
40
        $spec = $arg->getSpec();
41
        if (!array_key_exists('cdata', $spec) || null === $spec['cdata']) {
42
            return;
43
        }
44
45
        $node = $arg->getNode();
46
        if (!($node instanceof DOMElement)) {
47
            throw new Exception\LogicException('Expected node of type DOMElement');
48
        }
49
50
        $node->nodeValue = '';
51
        $translatedCdata = $arg->getHelper()->translateValue($spec['cdata'], $arg->getVarTranslation(), $spec);
52
        if (empty($translatedCdata)) {
53
            return;
54
        }
55
56
        $node->appendChild($node->ownerDocument->createCdataSection($translatedCdata));
57
    }
58
}
59