Node.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * A node is the root class for every element in an iCalendar of vCard object.
  5. *
  6. * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
  7. * @author Evert Pot (http://evertpot.com/)
  8. * @license http://sabre.io/license/ Modified BSD License
  9. */
  10. abstract class Node implements \IteratorAggregate, \ArrayAccess, \Countable {
  11. /**
  12. * The following constants are used by the validate() method.
  13. */
  14. const REPAIR = 1;
  15. /**
  16. * Reference to the parent object, if this is not the top object.
  17. *
  18. * @var Node
  19. */
  20. public $parent;
  21. /**
  22. * Iterator override
  23. *
  24. * @var ElementList
  25. */
  26. protected $iterator = null;
  27. /**
  28. * The root document
  29. *
  30. * @var Component
  31. */
  32. protected $root;
  33. /**
  34. * Serializes the node into a mimedir format
  35. *
  36. * @return string
  37. */
  38. abstract public function serialize();
  39. /**
  40. * This method returns an array, with the representation as it should be
  41. * encoded in json. This is used to create jCard or jCal documents.
  42. *
  43. * @return array
  44. */
  45. abstract public function jsonSerialize();
  46. /* {{{ IteratorAggregator interface */
  47. /**
  48. * Returns the iterator for this object
  49. *
  50. * @return ElementList
  51. */
  52. public function getIterator() {
  53. if (!is_null($this->iterator))
  54. return $this->iterator;
  55. return new ElementList(array($this));
  56. }
  57. /**
  58. * Sets the overridden iterator
  59. *
  60. * Note that this is not actually part of the iterator interface
  61. *
  62. * @param ElementList $iterator
  63. * @return void
  64. */
  65. public function setIterator(ElementList $iterator) {
  66. $this->iterator = $iterator;
  67. }
  68. /**
  69. * Validates the node for correctness.
  70. *
  71. * The following options are supported:
  72. * Node::REPAIR - May attempt to automatically repair the problem.
  73. *
  74. * This method returns an array with detected problems.
  75. * Every element has the following properties:
  76. *
  77. * * level - problem level.
  78. * * message - A human-readable string describing the issue.
  79. * * node - A reference to the problematic node.
  80. *
  81. * The level means:
  82. * 1 - The issue was repaired (only happens if REPAIR was turned on)
  83. * 2 - An inconsequential issue
  84. * 3 - A severe issue.
  85. *
  86. * @param int $options
  87. * @return array
  88. */
  89. public function validate($options = 0) {
  90. return array();
  91. }
  92. /* }}} */
  93. /* {{{ Countable interface */
  94. /**
  95. * Returns the number of elements
  96. *
  97. * @return int
  98. */
  99. public function count() {
  100. $it = $this->getIterator();
  101. return $it->count();
  102. }
  103. /* }}} */
  104. /* {{{ ArrayAccess Interface */
  105. /**
  106. * Checks if an item exists through ArrayAccess.
  107. *
  108. * This method just forwards the request to the inner iterator
  109. *
  110. * @param int $offset
  111. * @return bool
  112. */
  113. public function offsetExists($offset) {
  114. $iterator = $this->getIterator();
  115. return $iterator->offsetExists($offset);
  116. }
  117. /**
  118. * Gets an item through ArrayAccess.
  119. *
  120. * This method just forwards the request to the inner iterator
  121. *
  122. * @param int $offset
  123. * @return mixed
  124. */
  125. public function offsetGet($offset) {
  126. $iterator = $this->getIterator();
  127. return $iterator->offsetGet($offset);
  128. }
  129. /**
  130. * Sets an item through ArrayAccess.
  131. *
  132. * This method just forwards the request to the inner iterator
  133. *
  134. * @param int $offset
  135. * @param mixed $value
  136. * @return void
  137. */
  138. public function offsetSet($offset, $value) {
  139. $iterator = $this->getIterator();
  140. $iterator->offsetSet($offset,$value);
  141. // @codeCoverageIgnoreStart
  142. //
  143. // This method always throws an exception, so we ignore the closing
  144. // brace
  145. }
  146. // @codeCoverageIgnoreEnd
  147. /**
  148. * Sets an item through ArrayAccess.
  149. *
  150. * This method just forwards the request to the inner iterator
  151. *
  152. * @param int $offset
  153. * @return void
  154. */
  155. public function offsetUnset($offset) {
  156. $iterator = $this->getIterator();
  157. $iterator->offsetUnset($offset);
  158. // @codeCoverageIgnoreStart
  159. //
  160. // This method always throws an exception, so we ignore the closing
  161. // brace
  162. }
  163. // @codeCoverageIgnoreEnd
  164. /* }}} */
  165. }