ODD.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Open Data Definition (ODD) superclass.
  4. *
  5. * @package Elgg.Core
  6. * @subpackage ODD
  7. * @deprecated 1.9
  8. */
  9. abstract class ODD {
  10. /**
  11. * Attributes.
  12. */
  13. private $attributes = array();
  14. /**
  15. * Optional body.
  16. */
  17. private $body;
  18. /**
  19. * Construct an ODD document with initial values.
  20. */
  21. public function __construct() {
  22. $this->body = "";
  23. }
  24. /**
  25. * Returns an array of attributes
  26. *
  27. * @return array
  28. */
  29. public function getAttributes() {
  30. return $this->attributes;
  31. }
  32. /**
  33. * Sets an attribute
  34. *
  35. * @param string $key Name
  36. * @param mixed $value Value
  37. *
  38. * @return void
  39. */
  40. public function setAttribute($key, $value) {
  41. $this->attributes[$key] = $value;
  42. }
  43. /**
  44. * Returns an attribute
  45. *
  46. * @param string $key Name
  47. *
  48. * @return mixed
  49. */
  50. public function getAttribute($key) {
  51. if (isset($this->attributes[$key])) {
  52. return $this->attributes[$key];
  53. }
  54. return null;
  55. }
  56. /**
  57. * Sets the body of the ODD.
  58. *
  59. * @param mixed $value Value
  60. *
  61. * @return void
  62. */
  63. public function setBody($value) {
  64. $this->body = $value;
  65. }
  66. /**
  67. * Gets the body of the ODD.
  68. *
  69. * @return mixed
  70. */
  71. public function getBody() {
  72. return $this->body;
  73. }
  74. /**
  75. * Set the published time.
  76. *
  77. * @param int $time Unix timestamp
  78. *
  79. * @return void
  80. */
  81. public function setPublished($time) {
  82. $this->attributes['published'] = date("r", $time);
  83. }
  84. /**
  85. * Return the published time as a unix timestamp.
  86. *
  87. * @return int or false on failure.
  88. */
  89. public function getPublishedAsTime() {
  90. return strtotime($this->attributes['published']);
  91. }
  92. /**
  93. * For serialisation, implement to return a string name of the tag eg "header" or "metadata".
  94. *
  95. * @return string
  96. */
  97. abstract protected function getTagName();
  98. /**
  99. * Magic function to generate valid ODD XML for this item.
  100. *
  101. * @return string
  102. */
  103. public function __toString() {
  104. // Construct attributes
  105. $attr = "";
  106. foreach ($this->attributes as $k => $v) {
  107. $attr .= ($v != "") ? "$k=\"$v\" " : "";
  108. }
  109. $body = $this->getBody();
  110. $tag = $this->getTagName();
  111. $end = "/>";
  112. if ($body != "") {
  113. $end = "><![CDATA[$body]]></{$tag}>";
  114. }
  115. return "<{$tag} $attr" . $end . "\n";
  116. }
  117. }