xmlstringer.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Generator for in-memory XML
  18. *
  19. * @package GNUsocial
  20. * @category Output
  21. *
  22. * @author Evan Prodromou <evan@status.net>
  23. * @copyright 2009-2019 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Create in-memory XML
  29. *
  30. * @see Action
  31. * @see HTMLOutputter
  32. *
  33. * @copyright 2009-2019 Free Software Foundation, Inc http://www.fsf.org
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class xmlstringer extends XMLOutputter
  37. {
  38. /**
  39. * XMLStringer constructor.
  40. *
  41. * @param bool $indent
  42. */
  43. public function __construct(bool $indent = false)
  44. {
  45. $this->xw = new XMLWriter();
  46. $this->xw->openMemory();
  47. $this->xw->setIndent($indent);
  48. }
  49. /**
  50. * @param string $tag Element type or tagname
  51. * @param null|array|string $attrs Array of element attributes, as key-value pairs
  52. * @param null|string $content string content of the element
  53. *
  54. * @return string
  55. */
  56. public static function estring(string $tag, $attrs = null, ?string $content = null): string
  57. {
  58. $xs = new self();
  59. $xs->element($tag, $attrs, $content);
  60. return $xs->getString();
  61. }
  62. /**
  63. * Utility for quickly creating XML-strings
  64. *
  65. * @return string
  66. */
  67. public function getString()
  68. {
  69. return $this->xw->outputMemory();
  70. }
  71. }