comparison class.template.php @ 6:f80e9010e64d

Fix du clonage (removeAttributeNS et pas removeAttribute), refactoring
author Franck Deroche <webmaster@defr.org>
date Sun, 21 Oct 2007 13:21:57 +0200
parents a76a46cbb401
children 61dee7e34a9d
comparison
equal deleted inserted replaced
5:888b9807b2b3 6:f80e9010e64d
3 private $xmlDocument; 3 private $xmlDocument;
4 private $xmlXPath; 4 private $xmlXPath;
5 5
6 const NS = 'http://defr.net/2007/template'; 6 const NS = 'http://defr.net/2007/template';
7 7
8 function __construct($fileName) { 8 function __construct($fileName, $shouldValidate = false) {
9 $this->xmlDocument = new DOMDocument(); 9 $this->xmlDocument = new DOMDocument();
10 $this->xmlDocument->validateOnParse = true; 10 $this->xmlDocument->validateOnParse = $shouldValidate;
11 $this->xmlDocument->loadXML(file_get_contents($fileName)); 11 $this->xmlDocument->loadXML(file_get_contents($fileName));
12 $this->xmlXPath = new DOMXPath($this->xmlDocument); 12 $this->xmlXPath = new DOMXPath($this->xmlDocument);
13 } 13 }
14 14
15 function apply($selector, $obj, DOMElement $root = null) { 15 function apply($selector, $obj, DOMElement $root = null) {
16 if(!($root instanceof DOMElement)) { 16 if(!($root instanceof DOMElement))
17 $root = $this->xmlDocument->documentElement; 17 $root = $this->xmlDocument->documentElement;
18 }
19 $rootSelector = $this->parseSelector($selector, $root); 18 $rootSelector = $this->parseSelector($selector, $root);
20 // Cloning test 19 if(is_array($obj))
21 if(is_array($obj)) { 20 $this->applyClone($rootSelector, $obj, $root);
22 foreach($obj as $array) {
23 foreach($rootSelector->nodes as $node) {
24 $nodeName = key($array);
25 $tmp = $this->getClonedNode($node, $nodeName);
26 $futureNode = $tmp->clone;
27 $node->insertBefore($futureNode, $tmp->orig);
28 foreach($array as $sel => $test) {
29 $content = $this->parseReplacement($test);
30 $localSelector = $this->parseSelector($sel, $node);
31 if(isset($localSelector->attribute))
32 $futureNode->setAttribute($localSelector->attribute, $test);
33 else {
34 if($futureNode->namespaceURI == Template::NS)
35 $this->replaceNode($futureNode, $content);
36 else
37 $this->setNodeContent($futureNode, $content);
38 }
39 }
40 }
41 }
42 }
43 else { 21 else {
44 foreach($rootSelector->nodes as $node) { 22 foreach($rootSelector->nodes as $node) {
45 $content = $this->parseReplacement($obj); 23 $content = $this->parseReplacement($obj);
46 if(isset($rootSelector->attribute)) { 24 if(isset($rootSelector->attribute)) {
47 $node->setAttribute($rootSelector->attribute, $obj); 25 $node->setAttribute($rootSelector->attribute, $obj);
52 $this->setNodeContent($node, $content); 30 $this->setNodeContent($node, $content);
53 } 31 }
54 } 32 }
55 } 33 }
56 } 34 }
35
36 function applyClone($rootSelector, $obj, $root) {
37 foreach($obj as $array) {
38 $nodeName = key($array);
39 foreach($rootSelector->nodes as $node) {
40 $tmp = $this->getClonedNode($node, $nodeName);
41 $futureNode = $tmp->clone;
42 $futureNode = $node->insertBefore($futureNode, $tmp->orig);
43 foreach($array as $sel => $test) {
44 $locSelector = $this->parseSelector($sel, $node);
45 if(isset($localSelector->attribute))
46 $futureNode->setAttribute($locSelector->attribute, $test);
47 else {
48 $content = $this->parseReplacement($test);
49 $node->insertBefore($content, $tmp->orig);
50 if($futureNode->namespaceURI == Template::NS)
51 $this->replaceNode($futureNode, $content);
52 else
53 $this->setNodeContent($futureNode, $content);
54 }
55 }
56 }
57 }
58 }
57 59
58 function getClonedNode($node, $childNodeName) { 60 function getClonedNode($node, $childNodeName) {
59 $candidates = $this->parseSelector($childNodeName, $node)->nodes; 61 $candidates = $this->parseSelector($childNodeName, $node)->nodes;
60 foreach($candidates as $candidate) { 62 foreach($candidates as $candidate) {
61 if($candidate->getAttributeNS(Template::NS, 'toClone') == 'true') { 63 if($candidate->getAttributeNS(Template::NS, 'toClone') == 'true') {
62 $tmp = array(); 64 $tmp = array();
63 $tmp['orig'] = $candidate; 65 $tmp['orig'] = $candidate;
64 $cnode = $candidate->cloneNode(true); 66 $cnode = $candidate->cloneNode(true);
65 $cnode->removeAttribute('toClone'); 67 $cnode->removeAttributeNS(Template::NS, 'toClone');
66 $tmp['clone'] = $cnode; 68 $tmp['clone'] = $cnode;
67 return (object)$tmp; 69 return (object)$tmp;
68 } 70 }
69 } 71 }
70 } 72 }