XML.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  11. * @link http://pear.php.net/package/XML_XRD
  12. */
  13. /**
  14. * Generate XML from a XML_XRD object.
  15. *
  16. * @category XML
  17. * @package XML_XRD
  18. * @author Christian Weiske <cweiske@php.net>
  19. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  20. * @version Release: @package_version@
  21. * @link http://pear.php.net/package/XML_XRD
  22. */
  23. class XML_XRD_Serializer_XML
  24. {
  25. protected $xrd;
  26. /**
  27. * Create new instance
  28. *
  29. * @param XML_XRD $xrd XRD instance to convert to XML
  30. */
  31. public function __construct(XML_XRD $xrd)
  32. {
  33. $this->xrd = $xrd;
  34. }
  35. /**
  36. * Generate XML.
  37. *
  38. * @return string Full XML code
  39. */
  40. public function __toString()
  41. {
  42. $hasXsi = false;
  43. $x = new XMLWriter();
  44. $x->openMemory();
  45. //no encoding means UTF-8
  46. //http://www.w3.org/TR/2008/REC-xml-20081126/#sec-guessing-no-ext-info
  47. $x->startDocument('1.0', 'UTF-8');
  48. $x->setIndent(true);
  49. $x->startElement('XRD');
  50. $x->writeAttribute('xmlns', 'http://docs.oasis-open.org/ns/xri/xrd-1.0');
  51. $x->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  52. if ($this->xrd->id) {
  53. $x->writeAttribute('xml:id', $this->xrd->id);
  54. }
  55. if ($this->xrd->expires !== null) {
  56. $x->writeElement(
  57. 'Expires', gmdate('Y-m-d\TH:i:s\Z', $this->xrd->expires)
  58. );
  59. }
  60. if ($this->xrd->subject !== null) {
  61. $x->writeElement('Subject', $this->xrd->subject);
  62. }
  63. foreach ($this->xrd->aliases as $alias) {
  64. $x->writeElement('Alias', $alias);
  65. }
  66. foreach ($this->xrd->properties as $property) {
  67. $this->writeProperty($x, $property, $hasXsi);
  68. }
  69. foreach ($this->xrd->links as $link) {
  70. $x->startElement('Link');
  71. $x->writeAttribute('rel', $link->rel);
  72. if ($link->type !== null) {
  73. $x->writeAttribute('type', $link->type);
  74. }
  75. if ($link->href !== null) {
  76. $x->writeAttribute('href', $link->href);
  77. }
  78. //template only when no href
  79. if ($link->template !== null && $link->href === null) {
  80. $x->writeAttribute('template', $link->template);
  81. }
  82. foreach ($link->titles as $lang => $value) {
  83. $x->startElement('Title');
  84. if ($lang) {
  85. $x->writeAttribute('xml:lang', $lang);
  86. }
  87. $x->text($value);
  88. $x->endElement();
  89. }
  90. foreach ($link->properties as $property) {
  91. $this->writeProperty($x, $property, $hasXsi);
  92. }
  93. $x->endElement();
  94. }
  95. $x->endElement();
  96. $x->endDocument();
  97. $s = $x->flush();
  98. if (!$hasXsi) {
  99. $s = str_replace(
  100. ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', '', $s
  101. );
  102. }
  103. return $s;
  104. }
  105. /**
  106. * Write a property in the XMLWriter stream output
  107. *
  108. * @param XMLWriter $x Writer object to write to
  109. * @param XML_XRD_Element_Property $property Property to write
  110. * @param boolean &$hasXsi If an xsi: attribute is used
  111. *
  112. * @return void
  113. */
  114. protected function writeProperty(
  115. XMLWriter $x, XML_XRD_Element_Property $property, &$hasXsi
  116. ) {
  117. $x->startElement('Property');
  118. $x->writeAttribute('type', $property->type);
  119. if ($property->value === null) {
  120. $x->writeAttribute('xsi:nil', 'true');
  121. $hasXsi = true;
  122. } else {
  123. $x->text($property->value);
  124. }
  125. $x->endElement();
  126. }
  127. }
  128. ?>