1
|
|
|
<?php |
2
|
|
|
namespace Goetas\Twital\Attribute; |
3
|
|
|
|
4
|
|
|
use Goetas\Twital\Attribute; |
5
|
|
|
use Goetas\Twital\Compiler; |
6
|
|
|
use Goetas\Twital\Exception; |
7
|
|
|
use Goetas\Twital\Helper\ParserHelper; |
8
|
|
|
use Goetas\Twital\Twital; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @author Asmir Mustafic <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class AttrAttribute implements Attribute |
16
|
|
|
{ |
17
|
13 |
|
public static function getVarname(\DOMElement $node) |
18
|
|
|
{ |
19
|
13 |
|
return "__a" . sha1($node->getAttributeNS(Twital::NS, '__internal-id__') . spl_object_hash($node)); |
20
|
|
|
} |
21
|
|
|
|
22
|
12 |
|
public function visit(\DOMAttr $att, Compiler $context) |
23
|
|
|
{ |
24
|
12 |
|
$node = $att->ownerElement; |
25
|
12 |
|
$expressions = ParserHelper::staticSplitExpression($att->value, ","); |
26
|
|
|
|
27
|
12 |
|
$attributes = array(); |
28
|
12 |
|
foreach ($expressions as $k => $expression) { |
29
|
12 |
|
$expressions[$k] = $attrExpr = self::splitAttrExpression($expression); |
30
|
10 |
|
$attNode = null; |
31
|
|
|
|
32
|
10 |
|
if ($node->hasAttribute($attrExpr['name'])) { |
33
|
3 |
|
$attNode = $node->getAttributeNode($attrExpr['name']); |
34
|
3 |
|
$node->removeAttributeNode($attNode); |
35
|
3 |
|
} |
36
|
|
|
|
37
|
10 |
|
if ($attrExpr['test'] === "true" || $attrExpr['test'] === "1") { |
38
|
8 |
|
unset($expressions[$k]); |
39
|
8 |
|
$attributes[$attrExpr['name']] = "[{$attrExpr['expr']}]"; |
40
|
10 |
|
} elseif ($attNode) { |
41
|
1 |
|
$attributes[$attrExpr['name']] = "['" . addcslashes($attNode->value, "'") . "']"; |
42
|
1 |
|
} else { |
43
|
1 |
|
$attributes[$attrExpr['name']] = "[]"; |
44
|
|
|
} |
45
|
10 |
|
} |
46
|
|
|
|
47
|
10 |
|
$varName = self::getVarname($node); |
48
|
10 |
|
$code = array(); |
49
|
10 |
|
$code[] = $context->createControlNode("if $varName is not defined"); |
50
|
10 |
|
$code[] = $context->createControlNode("set $varName = {" . ParserHelper::implodeKeyed(",", $attributes, true) . " }"); |
51
|
10 |
|
$code[] = $context->createControlNode("else"); |
52
|
10 |
|
$code[] = $context->createControlNode("set $varName = $varName|merge({" . ParserHelper::implodeKeyed(",", $attributes, true) . "})"); |
53
|
10 |
|
$code[] = $context->createControlNode("endif"); |
54
|
|
|
|
55
|
10 |
|
foreach ($expressions as $attrExpr) { |
56
|
2 |
|
$code[] = $context->createControlNode("if {$attrExpr['test']}"); |
57
|
2 |
|
$code[] = $context->createControlNode("set {$varName} = {$varName}|merge({ '{$attrExpr['name']}':[{$attrExpr['expr']}] })"); |
58
|
2 |
|
$code[] = $context->createControlNode("endif"); |
59
|
10 |
|
} |
60
|
|
|
|
61
|
10 |
|
$this->addSpecialAttr($node, $varName, $code); |
62
|
10 |
|
$node->removeAttributeNode($att); |
63
|
10 |
|
} |
64
|
|
|
|
65
|
15 |
|
public static function splitAttrExpression($str) |
66
|
|
|
{ |
67
|
15 |
|
$parts = ParserHelper::staticSplitExpression($str, "?"); |
68
|
15 |
|
if (count($parts) == 1) { |
69
|
7 |
|
$attr = self::findAttrParts($parts[0]); |
70
|
7 |
|
$attr['test'] = 'true'; |
71
|
|
|
|
72
|
7 |
|
return $attr; |
73
|
8 |
|
} elseif (count($parts) == 2) { |
74
|
7 |
|
$attr = self::findAttrParts($parts[1]); |
75
|
6 |
|
$attr['test'] = $parts[0]; |
76
|
|
|
|
77
|
6 |
|
return $attr; |
78
|
|
|
} else { |
79
|
1 |
|
throw new Exception(__CLASS__ . "::splitAttrExpression error in '$str'"); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
13 |
|
protected function addSpecialAttr(\DOMElement $node, $varName, array $code) |
84
|
|
|
{ |
85
|
13 |
|
$node->setAttribute("__attr__", $varName); |
86
|
|
|
|
87
|
13 |
|
$ref = $node; |
88
|
13 |
|
foreach (array_reverse($code) as $line) { |
89
|
13 |
|
$node->parentNode->insertBefore($line, $ref); |
90
|
13 |
|
$ref = $line; |
91
|
13 |
|
} |
92
|
13 |
|
} |
93
|
|
|
|
94
|
14 |
|
protected static function findAttrParts($str) |
95
|
|
|
{ |
96
|
14 |
|
$mch = array(); |
97
|
14 |
|
if (preg_match("/^([a-z_][a-z0-9\\-_]*:[a-z][a-z0-9\\-_]*)\\s*=\\s*/i", $str, $mch)) { |
98
|
|
|
return array( |
99
|
|
|
'name' => $mch[1], |
100
|
|
|
'expr' => trim(substr($str, strlen($mch[0]))) |
101
|
|
|
); |
102
|
14 |
|
} elseif (preg_match("/^([a-z_][a-z0-9\\-_]*)\\s*=\\s*/i", $str, $mch)) { |
103
|
|
|
return array( |
104
|
13 |
|
'name' => $mch[1], |
105
|
13 |
|
'expr' => trim(substr($str, strlen($mch[0]))) |
106
|
13 |
|
); |
107
|
|
|
} else { |
108
|
1 |
|
throw new Exception(__CLASS__ . "::findAttrParts error in '$str'"); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|