ICalendar.php 2.9 KB

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