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