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