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 its input.
42     *
43     * @param resource $input
44     * @param int      $options parser options, see the OPTIONS constants
45     */
46    public 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 ('VTIMEZONE' === $component->name) {
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     * Every time getNext() is called, a new object will be parsed, until we
82     * hit the end of the stream.
83     *
84     * When the end is reached, null will be returned.
85     *
86     * @return \Sabre\VObject\Component|null
87     */
88    public function getNext()
89    {
90        if ($object = array_shift($this->objects)) {
91            // create our baseobject
92            $object->version = '2.0';
93            $object->prodid = '-//Sabre//Sabre VObject '.VObject\Version::VERSION.'//EN';
94            $object->calscale = 'GREGORIAN';
95
96            // add vtimezone information to obj (if we have it)
97            foreach ($this->vtimezones as $vtimezone) {
98                $object->add($vtimezone);
99            }
100
101            return $object;
102        } else {
103            return;
104        }
105    }
106}
107