|
1
|
|
|
<?php |
|
2
|
|
|
namespace Goetas\Twital\Attribute; |
|
3
|
|
|
|
|
4
|
|
|
use Goetas\Twital\Compiler; |
|
5
|
|
|
use Goetas\Twital\Helper\ParserHelper; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
* @author Asmir Mustafic <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
class AttrAppendAttribute extends AttrAttribute |
|
13
|
|
|
{ |
|
14
|
6 |
|
public function visit(\DOMAttr $att, Compiler $context) |
|
15
|
|
|
{ |
|
16
|
6 |
|
$node = $att->ownerElement; |
|
17
|
6 |
|
$expressions = ParserHelper::staticSplitExpression($att->value, ","); |
|
18
|
|
|
|
|
19
|
6 |
|
$attributes = array(); |
|
20
|
6 |
|
foreach ($expressions as $k => $expression) { |
|
21
|
6 |
|
$expressions[$k] = $attrExpr = self::splitAttrExpression($expression); |
|
22
|
6 |
|
$attNode = null; |
|
23
|
6 |
|
if (!isset($attributes[$attrExpr['name']])) { |
|
24
|
6 |
|
$attributes[$attrExpr['name']] = array(); |
|
25
|
6 |
|
} |
|
26
|
6 |
|
if ($node->hasAttribute($attrExpr['name'])) { |
|
27
|
1 |
|
$attNode = $node->getAttributeNode($attrExpr['name']); |
|
28
|
1 |
|
$node->removeAttributeNode($attNode); |
|
29
|
1 |
|
$attributes[$attrExpr['name']][] = "'" . addcslashes($attNode->value, "'") . "'"; |
|
30
|
1 |
|
} |
|
31
|
6 |
|
if ($attrExpr['test'] === "true" || $attrExpr['test'] === "1") { |
|
32
|
3 |
|
unset($expressions[$k]); |
|
33
|
3 |
|
$attributes[$attrExpr['name']][] = $attrExpr['expr']; |
|
34
|
3 |
|
} |
|
35
|
6 |
|
} |
|
36
|
|
|
|
|
37
|
6 |
|
$code = array(); |
|
38
|
|
|
|
|
39
|
6 |
|
$varName = self::getVarname($node); |
|
40
|
6 |
|
$code[] = $context->createControlNode("if $varName is not defined"); |
|
41
|
6 |
|
$code[] = $context->createControlNode("set $varName = {" . ParserHelper::implodeKeyedDouble(",", $attributes, true) . " }"); |
|
42
|
6 |
|
$code[] = $context->createControlNode("else"); |
|
43
|
|
|
|
|
44
|
6 |
|
foreach ($attributes as $attribute => $values) { |
|
45
|
6 |
|
$code[] = $context->createControlNode("if {$varName}['{$attribute}'] is defined"); |
|
46
|
6 |
|
$code[] = $context->createControlNode("set $varName = $varName|merge({ '$attribute' : ({$varName}['{$attribute}']|merge([" . implode(",", $values) . "])) })"); |
|
47
|
6 |
|
$code[] = $context->createControlNode("else"); |
|
48
|
6 |
|
$code[] = $context->createControlNode("set $varName = $varName|merge({ '$attribute' : [" . implode(",", $values) . "]})"); |
|
49
|
6 |
|
$code[] = $context->createControlNode("endif"); |
|
50
|
6 |
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
$code[] = $context->createControlNode("endif"); |
|
53
|
|
|
|
|
54
|
6 |
|
foreach ($expressions as $attrExpr) { |
|
55
|
3 |
|
$code[] = $context->createControlNode("if {$attrExpr['test']}"); |
|
56
|
3 |
|
$code[] = $context->createControlNode( |
|
57
|
3 |
|
"set {$varName} = {$varName}|merge({ '{$attrExpr['name']}':{$varName}.{$attrExpr['name']}|merge([{$attrExpr['expr']}]) })" |
|
58
|
3 |
|
); |
|
59
|
3 |
|
$code[] = $context->createControlNode("endif"); |
|
60
|
6 |
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
$this->addSpecialAttr($node, $varName, $code); |
|
63
|
6 |
|
$node->removeAttributeNode($att); |
|
64
|
6 |
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|