Node.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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) fruux GmbH (https://fruux.com/)
  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. * If REPAIR is set, the validator will attempt to repair any broken data
  15. * (if possible).
  16. */
  17. const REPAIR = 1;
  18. /**
  19. * If this option is set, the validator will operate on the vcards on the
  20. * assumption that the vcards need to be valid for CardDAV.
  21. *
  22. * This means for example that the UID is required, whereas it is not for
  23. * regular vcards.
  24. */
  25. const PROFILE_CARDDAV = 2;
  26. /**
  27. * If this option is set, the validator will operate on iCalendar objects
  28. * on the assumption that the vcards need to be valid for CalDAV.
  29. *
  30. * This means for example that calendars can only contain objects with
  31. * identical component types and UIDs.
  32. */
  33. const PROFILE_CALDAV = 4;
  34. /**
  35. * Reference to the parent object, if this is not the top object.
  36. *
  37. * @var Node
  38. */
  39. public $parent;
  40. /**
  41. * Iterator override
  42. *
  43. * @var ElementList
  44. */
  45. protected $iterator = null;
  46. /**
  47. * The root document
  48. *
  49. * @var Component
  50. */
  51. protected $root;
  52. /**
  53. * Serializes the node into a mimedir format
  54. *
  55. * @return string
  56. */
  57. abstract public function serialize();
  58. /**
  59. * This method returns an array, with the representation as it should be
  60. * encoded in json. This is used to create jCard or jCal documents.
  61. *
  62. * @return array
  63. */
  64. abstract public function jsonSerialize();
  65. /* {{{ IteratorAggregator interface */
  66. /**
  67. * Returns the iterator for this object
  68. *
  69. * @return ElementList
  70. */
  71. public function getIterator() {
  72. if (!is_null($this->iterator))
  73. return $this->iterator;
  74. return new ElementList(array($this));
  75. }
  76. /**
  77. * Sets the overridden iterator
  78. *
  79. * Note that this is not actually part of the iterator interface
  80. *
  81. * @param ElementList $iterator
  82. * @return void
  83. */
  84. public function setIterator(ElementList $iterator) {
  85. $this->iterator = $iterator;
  86. }
  87. /**
  88. * Validates the node for correctness.
  89. *
  90. * The following options are supported:
  91. * Node::REPAIR - May attempt to automatically repair the problem.
  92. *
  93. * This method returns an array with detected problems.
  94. * Every element has the following properties:
  95. *
  96. * * level - problem level.
  97. * * message - A human-readable string describing the issue.
  98. * * node - A reference to the problematic node.
  99. *
  100. * The level means:
  101. * 1 - The issue was repaired (only happens if REPAIR was turned on)
  102. * 2 - An inconsequential issue
  103. * 3 - A severe issue.
  104. *
  105. * @param int $options
  106. * @return array
  107. */
  108. public function validate($options = 0) {
  109. return array();
  110. }
  111. /* }}} */
  112. /* {{{ Countable interface */
  113. /**
  114. * Returns the number of elements
  115. *
  116. * @return int
  117. */
  118. public function count() {
  119. $it = $this->getIterator();
  120. return $it->count();
  121. }
  122. /* }}} */
  123. /* {{{ ArrayAccess Interface */
  124. /**
  125. * Checks if an item exists through ArrayAccess.
  126. *
  127. * This method just forwards the request to the inner iterator
  128. *
  129. * @param int $offset
  130. * @return bool
  131. */
  132. public function offsetExists($offset) {
  133. $iterator = $this->getIterator();
  134. return $iterator->offsetExists($offset);
  135. }
  136. /**
  137. * Gets an item through ArrayAccess.
  138. *
  139. * This method just forwards the request to the inner iterator
  140. *
  141. * @param int $offset
  142. * @return mixed
  143. */
  144. public function offsetGet($offset) {
  145. $iterator = $this->getIterator();
  146. return $iterator->offsetGet($offset);
  147. }
  148. /**
  149. * Sets an item through ArrayAccess.
  150. *
  151. * This method just forwards the request to the inner iterator
  152. *
  153. * @param int $offset
  154. * @param mixed $value
  155. * @return void
  156. */
  157. public function offsetSet($offset, $value) {
  158. $iterator = $this->getIterator();
  159. $iterator->offsetSet($offset,$value);
  160. // @codeCoverageIgnoreStart
  161. //
  162. // This method always throws an exception, so we ignore the closing
  163. // brace
  164. }
  165. // @codeCoverageIgnoreEnd
  166. /**
  167. * Sets an item through ArrayAccess.
  168. *
  169. * This method just forwards the request to the inner iterator
  170. *
  171. * @param int $offset
  172. * @return void
  173. */
  174. public function offsetUnset($offset) {
  175. $iterator = $this->getIterator();
  176. $iterator->offsetUnset($offset);
  177. // @codeCoverageIgnoreStart
  178. //
  179. // This method always throws an exception, so we ignore the closing
  180. // brace
  181. }
  182. // @codeCoverageIgnoreEnd
  183. /* }}} */
  184. }