Completed
Push — master ( 19a7d3...6b997b )
by Tim
13:45
created

ICalEvent   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 37
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 186
rs 8.6
1
<?php
2
namespace JMBTechnologyLimited\ICalDissect;
3
4
5
/**
6
 *
7
 * @link https://github.com/JMB-Technology-Limited/ICalDissect
8
 * @license https://raw.github.com/JMB-Technology-Limited/ICalDissect/master/LICENSE.txt 3-clause BSD
9
 * @copyright (c) 2014, JMB Technology Limited, http://jmbtechnology.co.uk/
10
 * @author James Baster <[email protected]>
11
 */
12
class ICalEvent
13
{
14
15
	protected $timeZone;
16
	protected $timeZoneUTC;
17
	
18
	/** @var \DateTime **/
19
	protected $start;
20
21
	/** @var \DateTime **/
22
	protected $end;
23
	
24
	protected $summary;
25
	
26
	protected $location;
27
	
28
	protected $description;
29
	
30
	protected $uid;
31
	
32
	protected $deleted = false;
33
34
	protected $url;
35
36
	protected $ical_rrule;
37
38
	protected $exdates = array();
39
40
	public function __construct(\DateTimeZone $timeZone = null) {
41
		$this->timeZoneUTC =  new \DateTimeZone('UTC');
42
		$this->timeZone = $timeZone ? $timeZone : $this->timeZoneUTC;
43
	} 
44
	
45
	public function processLine($keyword, $value, $keywordProperties = "") {
46
		if ($keyword == 'UID') {
47
			$this->uid = $value;
48
		} else if ($keyword == 'LOCATION') {
49
			$this->location = $value;
50
		} else if ($keyword == 'SUMMARY') {
51
			$this->summary = $value;
52
		} else if ($keyword == 'DESCRIPTION') {
53
			$this->description = $value;
54
		} else if ($keyword == 'URL') {
55
			$this->url = $value;
56
		} else if ($keyword == 'DTSTART') {
57
			$this->start = $this->parseDateTime($value, true);
58
		} else if ($keyword == 'DTEND') {
59
			$this->end = $this->parseDateTime($value, false);
60
		} else if ($keyword == 'METHOD' && $value == 'CANCEL') {
61
			$this->deleted = true;
62
		} else if ($keyword == 'STATUS' && $value == 'CANCELLED') {
63
			$this->deleted = true;
64
		} else if ($keyword == 'RRULE') {
65
			$rrule = array();
66
			foreach(explode(";", $value) as $rruleBit) {
67
				list($k, $v) = explode("=",$rruleBit,2);
68
				$rrule[strtoupper($k)] = $v;
69
			}
70
			$this->ical_rrule = $rrule;
71
		} else if ($keyword == "EXDATE") {
72
			$this->exdates[] = new ICalExDate($value, $keywordProperties);
73
		}
74
75
	}
76
	
77
	
78
	/*
79
	 * Based on ....
80
	* @author   Martin Thoma <[email protected]>
81
	* @license  http://www.opensource.org/licenses/mit-license.php  MIT License
82
	* @link     http://code.google.com/p/ics-parser/
83
	**/
84
	protected function parseDateTime($value, $isStart) {
85
        $value = str_replace('Z', '', $value);
86
		$pattern  = '/([0-9]{4})';   // 1: YYYY
87
        $pattern .= '([0-9]{2})';    // 2: MM
88
        $pattern .= '([0-9]{2})';    // 3: DD
89
        
90
		$hasTimePart = false;
91
		if (strpos($value, "T") > 1) {
92
			$value = str_replace('T', '', $value);
93
			$pattern .= '([0-9]{0,2})';  // 4: HH
94
			$pattern .= '([0-9]{0,2})';  // 5: MM
95
			$pattern .= '([0-9]{0,2})/'; // 6: SS
96
			$hasTimePart = true;
97
		} else {
98
			$pattern .= '/';
99
		}
100
        preg_match($pattern, $value, $date);
101
102
        // Unix timestamp can't represent dates before 1970
103
        if ($date[1] <= 1970) {
104
            return null;
105
        }
106
        // Unix timestamps after 03:14:07 UTC 2038-01-19 might cause an overflow
107
        // if 32 bit integers are used.
108
		
109
		$out = new \DateTime('', $this->timeZone);
110
		$out->setDate((int)$date[1], (int)$date[2], (int)$date[3]);
111
		if ($hasTimePart) {
112
			$out->setTime((int)$date[4], (int)$date[5], (int)$date[6]);
113
		} else if ($isStart) {
114
			$out->setTime(0,0,0);
115
		} else if (!$isStart) {
116
			$out->setTime(23,59,59);
117
		}
118
		if ($this->timeZone->getName() != 'UTC') {
119
			$out->setTimezone($this->timeZoneUTC);
120
		}
121
		return $out;
122
	}
123
			
124
	
125
	public function getUid() {
126
		return $this->uid;
127
	}
128
	
129
	public function setUid($uid) {
130
		$this->uid = $uid;
131
	}	
132
	
133
	public function getStart() {
134
		return $this->start;
135
	}
136
137
	public function getEnd() {
138
		return $this->end;
139
	}
140
141
	public function getSummary() {
142
		return $this->summary;
143
	}
144
145
	public function getLocation() {
146
		return $this->location;
147
	}
148
149
	public function getDescription() {
150
		return $this->description;
151
	}
152
	
153
	public function getUrl() {
154
		return $this->url;
155
	}
156
157
	public function isDeleted() {
158
		return $this->deleted;
159
	}
160
161
	/**
162
	 * @return array
163
	 */
164
	public function getRRule()
165
	{
166
		return $this->ical_rrule;
167
	}
168
169
	/**
170
	 * @return array
171
	 */
172
	public function getExDates()
173
	{
174
		return $this->exdates;
175
	}
176
177
	/**
178
	 * @return ICalExDate
179
	 */
180
	public function getExDate($position)
181
	{
182
		return $this->exdates[$position];
183
	}
184
185
186
	/**
187
	 * @return integer
188
	 */
189
	public function getExDatesCount()
190
	{
191
		return count($this->exdates);
192
	}
193
194
195
196
197
}
198
199