Element.inc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Memcache_UI\Core {
  3. /**
  4. * A wrapper for XML elements.
  5. */
  6. class Element {
  7. public $attributes = array();
  8. public $name = NULL;
  9. public $new_line; // Add a new line after element
  10. public $value = NULL;
  11. public function __construct($name, $attr = NULL, $value = NULL) {
  12. $this->name = $name;
  13. $this->attributes = $attr;
  14. $this->value = $value;
  15. }
  16. /**
  17. * @link drupal7/includes/common.inc#check_plain()
  18. */
  19. public static function check_plain($text) {
  20. return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
  21. }
  22. /**
  23. * @link drupal7/includes/common.inc#drupal_attributes()
  24. */
  25. public static function getSerializedAttributes(array $attributes = array()) {
  26. foreach ($attributes as $attribute => &$data) {
  27. $data = implode(' ', (array) $data);
  28. $data = $attribute . '="' . self::check_plain($data) . '"';
  29. }
  30. return $attributes ? ' ' . implode(' ', $attributes) : '';
  31. }
  32. public function __toString() {
  33. $ret = '<'. $this->name;
  34. if (!empty($this->attributes) && is_array($this->attributes)) {
  35. $ret .= self::getSerializedAttributes($this->attributes);
  36. }
  37. if (empty($this->value)) {
  38. $ret .= ' />';
  39. }
  40. else {
  41. $ret .= '>';
  42. if ($this->value instanceof Element) {
  43. $ret .= (string) $this->value; // force __toString()
  44. }
  45. elseif (is_array($this->value)) {
  46. $ret .= implode("\n", $this->value);
  47. }
  48. else {
  49. $ret .= $this->value;
  50. }
  51. $ret .= "</$this->name>";
  52. }
  53. return $ret;
  54. }
  55. }
  56. }