|
1
|
|
|
<?php |
|
2
|
|
|
namespace Goetas\TwitalBundle\Attribute; |
|
3
|
|
|
|
|
4
|
|
|
use Goetas\Twital\Attribute; |
|
5
|
|
|
use Goetas\Twital\Compiler; |
|
6
|
|
|
use DOMAttr; |
|
7
|
|
|
use Goetas\Twital\Helper\ParserHelper; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* @author Asmir Mustafic <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
class TranslateAttrNAttribute implements Attribute |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
6 |
|
public static function getVarname(\DOMNode $node) |
|
18
|
|
|
{ |
|
19
|
6 |
|
return "__a" . str_replace("-", "_", spl_object_hash($node)); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
6 |
|
public function visit(DOMAttr $att, Compiler $context) |
|
23
|
|
|
{ |
|
24
|
6 |
|
$node = $att->ownerElement; |
|
25
|
6 |
|
$expressions = ParserHelper::staticSplitExpression($att->value, ","); |
|
26
|
6 |
|
$varName = self::getVarname($node); |
|
27
|
|
|
|
|
28
|
6 |
|
$parts = array(); |
|
29
|
|
|
|
|
30
|
6 |
|
foreach ($expressions as $expression) { |
|
31
|
|
|
|
|
32
|
6 |
|
$attrExpr = ParserHelper::staticSplitExpression($expression, ":", 2); |
|
33
|
|
|
|
|
34
|
6 |
|
if (!$node->hasAttribute($attrExpr[0])) { |
|
35
|
|
|
throw new \Exception("non trovo l'attributo " . $attrExpr[0] . " da tradurre"); |
|
36
|
|
|
} |
|
37
|
6 |
|
$attNode = $node->getAttributeNode($attrExpr[0]); |
|
38
|
|
|
|
|
39
|
6 |
|
$transParams = isset($attrExpr[1]) ? trim($attrExpr[1], "\n\t\r []") : array(); |
|
40
|
6 |
|
$transParams = ParserHelper::staticSplitExpression($transParams, ",", 2); |
|
41
|
|
|
|
|
42
|
6 |
|
$parts[$attrExpr[0]] = "['" . addcslashes($attNode->value, "'") . "'|transchoice($transParams[0]" . (isset($transParams[1]) ? (", " . $transParams[1]) : '') . "" . (isset($transParams[2]) ? ", $transParams[2]" : "") . ")]"; |
|
43
|
|
|
|
|
44
|
6 |
|
$node->removeAttributeNode($attNode); |
|
45
|
6 |
|
} |
|
46
|
|
|
|
|
47
|
6 |
|
$code = "set $varName = $varName|default({})|merge({" . ParserHelper::implodeKeyed(",", $parts, true) . "})"; |
|
48
|
|
|
|
|
49
|
6 |
|
$node->setAttribute("__attr__", $varName); |
|
50
|
|
|
|
|
51
|
6 |
|
$pi = $context->createControlNode($code); |
|
52
|
|
|
|
|
53
|
6 |
|
$node->parentNode->insertBefore($pi, $node); |
|
54
|
|
|
|
|
55
|
6 |
|
$node->removeAttributeNode($att); |
|
56
|
6 |
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|