ICalendar.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Sabre\VObject\Splitter;
  3. use Sabre\VObject;
  4. use Sabre\VObject\Component\VCalendar;
  5. /**
  6. * Splitter.
  7. *
  8. * This class is responsible for splitting up iCalendar objects.
  9. *
  10. * This class expects a single VCALENDAR object with one or more
  11. * calendar-objects inside. Objects with identical UID's will be combined into
  12. * a single object.
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author Dominik Tobschall (http://tobschall.de/)
  16. * @author Armin Hackmann
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. class ICalendar implements SplitterInterface
  20. {
  21. /**
  22. * Timezones.
  23. *
  24. * @var array
  25. */
  26. protected $vtimezones = [];
  27. /**
  28. * iCalendar objects.
  29. *
  30. * @var array
  31. */
  32. protected $objects = [];
  33. /**
  34. * Constructor.
  35. *
  36. * The splitter should receive an readable file stream as its input.
  37. *
  38. * @param resource $input
  39. * @param int $options parser options, see the OPTIONS constants
  40. */
  41. public function __construct($input, $options = 0)
  42. {
  43. $data = VObject\Reader::read($input, $options);
  44. if (!$data instanceof VObject\Component\VCalendar) {
  45. throw new VObject\ParseException('Supplied input could not be parsed as VCALENDAR.');
  46. }
  47. foreach ($data->children() as $component) {
  48. if (!$component instanceof VObject\Component) {
  49. continue;
  50. }
  51. // Get all timezones
  52. if ('VTIMEZONE' === $component->name) {
  53. $this->vtimezones[(string) $component->TZID] = $component;
  54. continue;
  55. }
  56. // Get component UID for recurring Events search
  57. if (!$component->UID) {
  58. $component->UID = sha1(microtime()).'-vobjectimport';
  59. }
  60. $uid = (string) $component->UID;
  61. // Take care of recurring events
  62. if (!array_key_exists($uid, $this->objects)) {
  63. $this->objects[$uid] = new VCalendar();
  64. }
  65. $this->objects[$uid]->add(clone $component);
  66. }
  67. }
  68. /**
  69. * Every time getNext() is called, a new object will be parsed, until we
  70. * hit the end of the stream.
  71. *
  72. * When the end is reached, null will be returned.
  73. *
  74. * @return \Sabre\VObject\Component|null
  75. */
  76. public function getNext()
  77. {
  78. if ($object = array_shift($this->objects)) {
  79. // create our baseobject
  80. $object->version = '2.0';
  81. $object->prodid = '-//Sabre//Sabre VObject '.VObject\Version::VERSION.'//EN';
  82. $object->calscale = 'GREGORIAN';
  83. // add vtimezone information to obj (if we have it)
  84. foreach ($this->vtimezones as $vtimezone) {
  85. $object->add($vtimezone);
  86. }
  87. return $object;
  88. } else {
  89. return;
  90. }
  91. }
  92. }