1: <?php // BUILD: Remove line
2:
3: namespace intouch\ical;
4:
5: use \DateTime;
6:
7: /**
8: * A wrapper for recurrence rules in iCalendar. Parses the given line and puts the
9: * recurrence rules in the correct field of this object.
10: *
11: * See http://tools.ietf.org/html/rfc2445 for more information. Page 39 and onward contains more
12: * information on the recurrence rules themselves. Page 116 and onward contains
13: * some great examples which were often used for testing.
14: *
15: * @author Steven Oxley
16: * @author Michael Kahn (C) 2013
17: * @license http://creativecommons.org/licenses/by-sa/2.5/dk/deed.en_GB CC-BY-SA-DK
18: */
19: class Recurrence {
20:
21: public $rrule;
22:
23: protected $freq;
24:
25: protected $until;
26: protected $count;
27:
28: protected $interval;
29: protected $bysecond;
30: protected $byminute;
31: protected $byhour;
32: protected $byday;
33: protected $bymonthday;
34: protected $byyearday;
35: protected $byweekno;
36: protected $bymonth;
37: protected $bysetpos;
38:
39: protected $wkst;
40:
41: /**
42: * A list of the properties that can have comma-separated lists for values.
43: * @var array
44: */
45: protected $listProperties = array(
46: 'bysecond', 'byminute', 'byhour', 'byday', 'bymonthday',
47: 'byyearday', 'byweekno', 'bymonth', 'bysetpos'
48: );
49:
50: /**
51: * Creates an recurrence object with a passed in line. Parses the line.
52: * @param object $line an intouch\ical\Line object which will be parsed to get the
53: * desired information.
54: */
55: public function __construct(Line $line) {
56: $this->parseLine($line->getData());
57: }
58:
59: /**
60: * Parses an 'RRULE' line and sets the member variables of this object.
61: * Expects a string that looks like this: 'FREQ=WEEKLY;INTERVAL=2;BYDAY=SU,TU,WE'
62: * @param string $line the line to be parsed
63: */
64: protected function parseLine($line) {
65: $this->rrule = $line;
66:
67: //split up the properties
68: $recurProperties = explode(';', $line);
69: $recur = array();
70:
71: //loop through the properties in the line and set their associated
72: //member variables
73: foreach ($recurProperties as $property) {
74: $nameAndValue = explode('=', $property);
75:
76: //need the lower-case name for setting the member variable
77: $propertyName = strtolower($nameAndValue[0]);
78: $propertyValue = $nameAndValue[1];
79:
80: //split up the list of values into an array (if it's a list)
81: if (in_array($propertyName, $this->listProperties)) {
82: $propertyValue = explode(',', $propertyValue);
83: }
84: $this->$propertyName = $propertyValue;
85: }
86: }
87:
88: /**
89: * Set the $until member
90: * @param mixed timestamp (int) / Valid DateTime format (string)
91: */
92: public function setUntil($ts) {
93: if ( is_int($ts) )
94: $dt = new DateTime('@'.$ts);
95: else
96: $dt = new DateTime($ts);
97: $this->until = $dt->format('Ymd\THisO');
98: }
99:
100: /**
101: * Retrieves the desired member variable and returns it (if it's set)
102: * @param string $member name of the member variable
103: * @return mixed the variable value (if set), false otherwise
104: */
105: protected function getMember($member)
106: {
107: if (isset($this->$member)) {
108: return $this->$member;
109: }
110: return false;
111: }
112:
113: /**
114: * Returns the frequency - corresponds to FREQ in RFC 2445.
115: * @return mixed string if the member has been set, false otherwise
116: */
117: public function getFreq() {
118: return $this->getMember('freq');
119: }
120:
121: /**
122: * Returns when the event will go until - corresponds to UNTIL in RFC 2445.
123: * @return mixed string if the member has been set, false otherwise
124: */
125: public function getUntil() {
126: return $this->getMember('until');
127: }
128:
129: /**
130: * Returns the count of the times the event will occur (should only appear if 'until'
131: * does not appear) - corresponds to COUNT in RFC 2445.
132: * @return mixed string if the member has been set, false otherwise
133: */
134: public function getCount() {
135: return $this->getMember('count');
136: }
137:
138: /**
139: * Returns the interval - corresponds to INTERVAL in RFC 2445.
140: * @return mixed string if the member has been set, false otherwise
141: */
142: public function getInterval() {
143: return $this->getMember('interval');
144: }
145:
146: /**
147: * Returns the bysecond part of the event - corresponds to BYSECOND in RFC 2445.
148: * @return mixed string if the member has been set, false otherwise
149: */
150: public function getBySecond() {
151: return $this->getMember('bysecond');
152: }
153:
154: /**
155: * Returns the byminute information for the event - corresponds to BYMINUTE in RFC 2445.
156: * @return mixed string if the member has been set, false otherwise
157: */
158: public function getByMinute() {
159: return $this->getMember('byminute');
160: }
161:
162: /**
163: * Corresponds to BYHOUR in RFC 2445.
164: * @return mixed string if the member has been set, false otherwise
165: */
166: public function getByHour() {
167: return $this->getMember('byhour');
168: }
169:
170: /**
171: *Corresponds to BYDAY in RFC 2445.
172: * @return mixed string if the member has been set, false otherwise
173: */
174: public function getByDay() {
175: return $this->getMember('byday');
176: }
177:
178: /**
179: * Corresponds to BYMONTHDAY in RFC 2445.
180: * @return mixed string if the member has been set, false otherwise
181: */
182: public function getByMonthDay() {
183: return $this->getMember('bymonthday');
184: }
185:
186: /**
187: * Corresponds to BYYEARDAY in RFC 2445.
188: * @return mixed string if the member has been set, false otherwise
189: */
190: public function getByYearDay() {
191: return $this->getMember('byyearday');
192: }
193:
194: /**
195: * Corresponds to BYWEEKNO in RFC 2445.
196: * @return mixed string if the member has been set, false otherwise
197: */
198: public function getByWeekNo() {
199: return $this->getMember('byweekno');
200: }
201:
202: /**
203: * Corresponds to BYMONTH in RFC 2445.
204: * @return mixed string if the member has been set, false otherwise
205: */
206: public function getByMonth() {
207: return $this->getMember('bymonth');
208: }
209:
210: /**
211: * Corresponds to BYSETPOS in RFC 2445.
212: * @return mixed string if the member has been set, false otherwise
213: */
214: public function getBySetPos() {
215: return $this->getMember('bysetpos');
216: }
217:
218: /**
219: * Corresponds to WKST in RFC 2445.
220: * @return mixed string if the member has been set, false otherwise
221: */
222: public function getWkst() {
223: return $this->getMember('wkst');
224: }
225: }