Attribs::inLoop()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.0835
c 0
b 0
f 0
cc 8
nc 7
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
16
/**
17
 * Class Attribs
18
 */
19
class Attribs extends AbstractPlugin implements InLoopPluginInterface
20
{
21
    /**
22
     * @param PluginArgument $arg
23
     */
24
    public function inLoop(PluginArgument $arg)
25
    {
26
        $spec = $arg->getSpec();
27
        if (empty($spec['attribs'])) {
28
            return;
29
        }
30
31
        $helper         = $arg->getHelper();
32
        $node           = $arg->getNode();
33
        $varTranslation = $arg->getVarTranslation();
34
35
        if (!($node instanceof DOMElement)) {
36
            throw new Exception\LogicException('Expected node of type DOMAttr');
37
        }
38
39
        foreach ($spec['attribs'] as $attribName => $attribValue) {
40
            if (empty($node->parentNode)) {
41
                // node no longer exists
42
                continue;
43
            }
44
            
45
            $newAttribValue = $varTranslation->removeVars(
46
                $helper->translateValue($attribValue, $varTranslation, $spec)
47
            );
48
49
            $varKey = $varTranslation->makeVar($varTranslation->makeExtraVarKey($attribName));
50
51
            if (empty($newAttribValue) && !is_numeric($newAttribValue)) {
52
                $node->removeAttribute($attribName);
53
                $varTranslation->offsetExists($varKey) and $varTranslation->offsetUnset($varKey);
54
            } else {
55
                $newAttribValue = trim($newAttribValue);
56
                $node->setAttribute($attribName, $newAttribValue);
57
                $varTranslation->offsetSet($varKey, $newAttribValue);
58
            }
59
        }
60
    }
61
}
62