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
|
|
|
|