IPXSLTemplate.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. class tagRegistration {
  3. public $namespace;
  4. public $tagName;
  5. public $function;
  6. public function __construct($namespace, $tagName, $function) {
  7. $this->namespace = $namespace;
  8. $this->tagName = $tagName;
  9. $this->function = $function;
  10. }
  11. public function process($node) {
  12. $x = $node;
  13. eval($this->function);
  14. }
  15. }
  16. class IPXSLTemplate {
  17. const X_NAMESPACE = "http://schema.jool.nl/xmltemplate/";
  18. public $dom = null;
  19. public $xsltproc = null;
  20. public $customTags = Array();
  21. public function __construct($file) {
  22. $this->dom = new DOMDocument();
  23. $this->dom->load($file);
  24. $this->xsltproc = new XsltProcessor();
  25. $this->xsltproc->registerPHPFunctions();
  26. }
  27. public function execute($model) {
  28. //process custom tags
  29. foreach($this->customTags as $reg) {
  30. $nodelist = $this->dom->getElementsByTagNameNS($reg->namespace, $reg->tagName);
  31. for($i = $nodelist->length; $i > 0; $i--){
  32. $reg->process($nodelist->item($i-1));
  33. }
  34. }
  35. $this->xsltproc->importStyleSheet($this->dom);
  36. $modelDom = new DomDocument();
  37. $modelDom->appendChild($modelDom->createElement("model")); //root node
  38. IPXSLTemplate::makeXML($model, $modelDom->documentElement);
  39. //echo $modelDom->saveXML();
  40. return $this->xsltproc->transformToXml($modelDom);
  41. }
  42. /** Add a new custom tag registration */
  43. public function registerTag($namespace, $tagName, $function) {
  44. $this->customTags[] = new tagRegistration($namespace, $tagName, $function);
  45. }
  46. /** Makes a XML node from an object/ array / text */
  47. static function makeXML($model, $parent, $addToParent = false) {
  48. if(is_array($model)){
  49. foreach($model as $name => $value){
  50. if(!is_numeric($name)) {
  51. $node = $parent->ownerDocument->createElement($name);
  52. $parent->appendChild($node);
  53. IPXSLTemplate::makeXml($value, $node, true);
  54. } else {
  55. $node = $parent;
  56. IPXSLTemplate::makeXml($value, $node);
  57. }
  58. }
  59. } elseif (is_object($model)) {
  60. if($addToParent)
  61. $node = $parent;
  62. else{
  63. $node = $parent->ownerDocument->createElement(get_class($model));
  64. $parent->appendChild($node);
  65. }
  66. foreach($model as $propertyName => $propertyValue){
  67. $property = $parent->ownerDocument->createElement($propertyName);
  68. $node->appendChild($property);
  69. IPXSLTemplate::makeXml($propertyValue, $property);
  70. }
  71. } else {
  72. $parent->appendChild($parent->ownerDocument->createTextNode($model));
  73. }
  74. }
  75. }
  76. ?>