ElementList.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * VObject ElementList
  5. *
  6. * This class represents a list of elements. Lists are the result of queries,
  7. * such as doing $vcalendar->vevent where there's multiple VEVENT objects.
  8. *
  9. * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class ElementList implements \Iterator, \Countable, \ArrayAccess {
  14. /**
  15. * Inner elements
  16. *
  17. * @var array
  18. */
  19. protected $elements = array();
  20. /**
  21. * Creates the element list.
  22. *
  23. * @param array $elements
  24. */
  25. public function __construct(array $elements) {
  26. $this->elements = $elements;
  27. }
  28. /* {{{ Iterator interface */
  29. /**
  30. * Current position
  31. *
  32. * @var int
  33. */
  34. private $key = 0;
  35. /**
  36. * Returns current item in iteration
  37. *
  38. * @return Element
  39. */
  40. public function current() {
  41. return $this->elements[$this->key];
  42. }
  43. /**
  44. * To the next item in the iterator
  45. *
  46. * @return void
  47. */
  48. public function next() {
  49. $this->key++;
  50. }
  51. /**
  52. * Returns the current iterator key
  53. *
  54. * @return int
  55. */
  56. public function key() {
  57. return $this->key;
  58. }
  59. /**
  60. * Returns true if the current position in the iterator is a valid one
  61. *
  62. * @return bool
  63. */
  64. public function valid() {
  65. return isset($this->elements[$this->key]);
  66. }
  67. /**
  68. * Rewinds the iterator
  69. *
  70. * @return void
  71. */
  72. public function rewind() {
  73. $this->key = 0;
  74. }
  75. /* }}} */
  76. /* {{{ Countable interface */
  77. /**
  78. * Returns the number of elements
  79. *
  80. * @return int
  81. */
  82. public function count() {
  83. return count($this->elements);
  84. }
  85. /* }}} */
  86. /* {{{ ArrayAccess Interface */
  87. /**
  88. * Checks if an item exists through ArrayAccess.
  89. *
  90. * @param int $offset
  91. * @return bool
  92. */
  93. public function offsetExists($offset) {
  94. return isset($this->elements[$offset]);
  95. }
  96. /**
  97. * Gets an item through ArrayAccess.
  98. *
  99. * @param int $offset
  100. * @return mixed
  101. */
  102. public function offsetGet($offset) {
  103. return $this->elements[$offset];
  104. }
  105. /**
  106. * Sets an item through ArrayAccess.
  107. *
  108. * @param int $offset
  109. * @param mixed $value
  110. * @return void
  111. */
  112. public function offsetSet($offset, $value) {
  113. throw new \LogicException('You can not add new objects to an ElementList');
  114. }
  115. /**
  116. * Sets an item through ArrayAccess.
  117. *
  118. * This method just forwards the request to the inner iterator
  119. *
  120. * @param int $offset
  121. * @return void
  122. */
  123. public function offsetUnset($offset) {
  124. throw new \LogicException('You can not remove objects from an ElementList');
  125. }
  126. /* }}} */
  127. }