Node.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace Sabre\VObject;
  3. use Sabre\Xml;
  4. /**
  5. * A node is the root class for every element in an iCalendar of vCard object.
  6. *
  7. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  8. * @author Evert Pot (http://evertpot.com/)
  9. * @license http://sabre.io/license/ Modified BSD License
  10. */
  11. abstract class Node implements \IteratorAggregate, \ArrayAccess, \Countable, \JsonSerializable, Xml\XmlSerializable
  12. {
  13. /**
  14. * The following constants are used by the validate() method.
  15. *
  16. * If REPAIR is set, the validator will attempt to repair any broken data
  17. * (if possible).
  18. */
  19. const REPAIR = 1;
  20. /**
  21. * If this option is set, the validator will operate on the vcards on the
  22. * assumption that the vcards need to be valid for CardDAV.
  23. *
  24. * This means for example that the UID is required, whereas it is not for
  25. * regular vcards.
  26. */
  27. const PROFILE_CARDDAV = 2;
  28. /**
  29. * If this option is set, the validator will operate on iCalendar objects
  30. * on the assumption that the vcards need to be valid for CalDAV.
  31. *
  32. * This means for example that calendars can only contain objects with
  33. * identical component types and UIDs.
  34. */
  35. const PROFILE_CALDAV = 4;
  36. /**
  37. * Reference to the parent object, if this is not the top object.
  38. *
  39. * @var Node
  40. */
  41. public $parent;
  42. /**
  43. * Iterator override.
  44. *
  45. * @var ElementList
  46. */
  47. protected $iterator = null;
  48. /**
  49. * The root document.
  50. *
  51. * @var Component
  52. */
  53. protected $root;
  54. /**
  55. * Serializes the node into a mimedir format.
  56. *
  57. * @return string
  58. */
  59. abstract public function serialize();
  60. /**
  61. * This method returns an array, with the representation as it should be
  62. * encoded in JSON. This is used to create jCard or jCal documents.
  63. *
  64. * @return array
  65. */
  66. #[\ReturnTypeWillChange]
  67. abstract public function jsonSerialize();
  68. /**
  69. * This method serializes the data into XML. This is used to create xCard or
  70. * xCal documents.
  71. *
  72. * @param Xml\Writer $writer XML writer
  73. */
  74. abstract public function xmlSerialize(Xml\Writer $writer): void;
  75. /**
  76. * Call this method on a document if you're done using it.
  77. *
  78. * It's intended to remove all circular references, so PHP can easily clean
  79. * it up.
  80. */
  81. public function destroy()
  82. {
  83. $this->parent = null;
  84. $this->root = null;
  85. }
  86. /* {{{ IteratorAggregator interface */
  87. /**
  88. * Returns the iterator for this object.
  89. *
  90. * @return ElementList
  91. */
  92. #[\ReturnTypeWillChange]
  93. public function getIterator()
  94. {
  95. if (!is_null($this->iterator)) {
  96. return $this->iterator;
  97. }
  98. return new ElementList([$this]);
  99. }
  100. /**
  101. * Sets the overridden iterator.
  102. *
  103. * Note that this is not actually part of the iterator interface
  104. */
  105. public function setIterator(ElementList $iterator)
  106. {
  107. $this->iterator = $iterator;
  108. }
  109. /**
  110. * Validates the node for correctness.
  111. *
  112. * The following options are supported:
  113. * Node::REPAIR - May attempt to automatically repair the problem.
  114. *
  115. * This method returns an array with detected problems.
  116. * Every element has the following properties:
  117. *
  118. * * level - problem level.
  119. * * message - A human-readable string describing the issue.
  120. * * node - A reference to the problematic node.
  121. *
  122. * The level means:
  123. * 1 - The issue was repaired (only happens if REPAIR was turned on)
  124. * 2 - An inconsequential issue
  125. * 3 - A severe issue.
  126. *
  127. * @param int $options
  128. *
  129. * @return array
  130. */
  131. public function validate($options = 0)
  132. {
  133. return [];
  134. }
  135. /* }}} */
  136. /* {{{ Countable interface */
  137. /**
  138. * Returns the number of elements.
  139. *
  140. * @return int
  141. */
  142. #[\ReturnTypeWillChange]
  143. public function count()
  144. {
  145. $it = $this->getIterator();
  146. return $it->count();
  147. }
  148. /* }}} */
  149. /* {{{ ArrayAccess Interface */
  150. /**
  151. * Checks if an item exists through ArrayAccess.
  152. *
  153. * This method just forwards the request to the inner iterator
  154. *
  155. * @param int $offset
  156. *
  157. * @return bool
  158. */
  159. #[\ReturnTypeWillChange]
  160. public function offsetExists($offset)
  161. {
  162. $iterator = $this->getIterator();
  163. return $iterator->offsetExists($offset);
  164. }
  165. /**
  166. * Gets an item through ArrayAccess.
  167. *
  168. * This method just forwards the request to the inner iterator
  169. *
  170. * @param int $offset
  171. *
  172. * @return mixed
  173. */
  174. #[\ReturnTypeWillChange]
  175. public function offsetGet($offset)
  176. {
  177. $iterator = $this->getIterator();
  178. return $iterator->offsetGet($offset);
  179. }
  180. /**
  181. * Sets an item through ArrayAccess.
  182. *
  183. * This method just forwards the request to the inner iterator
  184. *
  185. * @param int $offset
  186. * @param mixed $value
  187. *
  188. * @return void
  189. */
  190. #[\ReturnTypeWillChange]
  191. public function offsetSet($offset, $value)
  192. {
  193. $iterator = $this->getIterator();
  194. $iterator->offsetSet($offset, $value);
  195. // @codeCoverageIgnoreStart
  196. //
  197. // This method always throws an exception, so we ignore the closing
  198. // brace
  199. }
  200. // @codeCoverageIgnoreEnd
  201. /**
  202. * Sets an item through ArrayAccess.
  203. *
  204. * This method just forwards the request to the inner iterator
  205. *
  206. * @param int $offset
  207. *
  208. * @return void
  209. */
  210. #[\ReturnTypeWillChange]
  211. public function offsetUnset($offset)
  212. {
  213. $iterator = $this->getIterator();
  214. $iterator->offsetUnset($offset);
  215. // @codeCoverageIgnoreStart
  216. //
  217. // This method always throws an exception, so we ignore the closing
  218. // brace
  219. }
  220. // @codeCoverageIgnoreEnd
  221. /* }}} */
  222. }