1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\GoogleCalendar; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Google_Service_Calendar_Event; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Google_Service_Calendar_EventDateTime; |
10
|
|
|
|
11
|
|
|
class Event |
12
|
|
|
{ |
13
|
|
|
/** @var \Google_Service_Calendar_Event */ |
14
|
|
|
public $googleEvent; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $calendarId; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $attendees; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->attendees = []; |
25
|
|
|
$this->googleEvent = new Google_Service_Calendar_Event; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Google_Service_Calendar_Event $googleEvent |
30
|
|
|
* @param $calendarId |
31
|
|
|
* |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
|
|
public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId) |
35
|
|
|
{ |
36
|
|
|
$event = new static; |
37
|
|
|
|
38
|
|
|
$event->googleEvent = $googleEvent; |
39
|
|
|
$event->calendarId = $calendarId; |
40
|
|
|
|
41
|
|
|
return $event; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $properties |
46
|
|
|
* @param string|null $calendarId |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public static function create(array $properties, string $calendarId = null) |
51
|
|
|
{ |
52
|
|
|
$event = new static; |
53
|
|
|
|
54
|
|
|
$event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId(); |
55
|
|
|
|
56
|
|
|
foreach ($properties as $name => $value) { |
57
|
|
|
$event->$name = $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $event->save('insertEvent'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection |
64
|
|
|
{ |
65
|
|
|
$googleCalendar = static::getGoogleCalendar($calendarId); |
66
|
|
|
|
67
|
|
|
$googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
68
|
|
|
|
69
|
|
|
return collect($googleEvents) |
70
|
|
|
->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
71
|
|
|
return static::createFromGoogleCalendarEvent($event, $calendarId); |
72
|
|
|
}) |
73
|
|
|
->sortBy(function (Event $event) { |
74
|
|
|
return $event->sortDate; |
|
|
|
|
75
|
|
|
}) |
76
|
|
|
->values(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function find($eventId, string $calendarId = null): Event |
80
|
|
|
{ |
81
|
|
|
$googleCalendar = static::getGoogleCalendar($calendarId); |
82
|
|
|
|
83
|
|
|
$googleEvent = $googleCalendar->getEvent($eventId); |
84
|
|
|
|
85
|
|
|
return static::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function __get($name) |
89
|
|
|
{ |
90
|
|
|
$name = $this->getFieldName($name); |
91
|
|
|
|
92
|
|
|
if ($name === 'sortDate') { |
93
|
|
|
return $this->getSortDate(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$value = array_get($this->googleEvent, $name); |
97
|
|
|
|
98
|
|
|
if (in_array($name, ['start.date', 'end.date']) && $value) { |
99
|
|
|
$value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
103
|
|
|
$value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $value; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function __set($name, $value) |
110
|
|
|
{ |
111
|
|
|
$name = $this->getFieldName($name); |
112
|
|
|
|
113
|
|
|
if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
114
|
|
|
$this->setDateProperty($name, $value); |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
array_set($this->googleEvent, $name, $value); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function exists(): bool |
123
|
|
|
{ |
124
|
|
|
return $this->id != ''; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function isAllDayEvent(): bool |
128
|
|
|
{ |
129
|
|
|
return is_null($this->googleEvent['start']['dateTime']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function save(string $method = null): Event |
133
|
|
|
{ |
134
|
|
|
$method = $method ?? ($this->exists() ? 'updateEvent' : 'insertEvent'); |
135
|
|
|
|
136
|
|
|
$googleCalendar = $this->getGoogleCalendar($this->calendarId); |
137
|
|
|
|
138
|
|
|
$this->googleEvent->setAttendees($this->attendees); |
139
|
|
|
|
140
|
|
|
$googleEvent = $googleCalendar->$method($this); |
141
|
|
|
|
142
|
|
|
return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $properties |
147
|
|
|
* |
148
|
|
|
* @return Event |
149
|
|
|
*/ |
150
|
|
|
public function update(array $properties): Event |
151
|
|
|
{ |
152
|
|
|
foreach ($properties as $name => $value) { |
153
|
|
|
$this->$name = $value; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->save('updateEvent'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $eventId |
161
|
|
|
*/ |
162
|
|
|
public function delete(string $eventId = null) |
163
|
|
|
{ |
164
|
|
|
$this->getGoogleCalendar($this->calendarId)->deleteEvent($eventId ?? $this->id); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function addAttendee(array $attendees) |
168
|
|
|
{ |
169
|
|
|
$this->attendees[] = $attendees; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getSortDate(): string |
173
|
|
|
{ |
174
|
|
|
if ($this->startDate) { |
|
|
|
|
175
|
|
|
return $this->startDate; |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ($this->startDateTime) { |
|
|
|
|
179
|
|
|
return $this->startDateTime; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return ''; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected static function getGoogleCalendar(string $calendarId = null): GoogleCalendar |
186
|
|
|
{ |
187
|
|
|
$calendarId = $calendarId ?? config('google-calendar.calendar_id'); |
188
|
|
|
|
189
|
|
|
return GoogleCalendarFactory::createForCalendarId($calendarId); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function setDateProperty(string $name, Carbon $date) |
193
|
|
|
{ |
194
|
|
|
$eventDateTime = new Google_Service_Calendar_EventDateTime; |
195
|
|
|
|
196
|
|
|
if (in_array($name, ['start.date', 'end.date'])) { |
197
|
|
|
$eventDateTime->setDate($date->format('Y-m-d')); |
198
|
|
|
$eventDateTime->setTimezone($date->getTimezone()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
202
|
|
|
$eventDateTime->setDateTime($date->format(DateTime::RFC3339)); |
203
|
|
|
$eventDateTime->setTimezone($date->getTimezone()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (starts_with($name, 'start')) { |
207
|
|
|
$this->googleEvent->setStart($eventDateTime); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (starts_with($name, 'end')) { |
211
|
|
|
$this->googleEvent->setEnd($eventDateTime); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
protected function getFieldName(string $name): string |
216
|
|
|
{ |
217
|
|
|
return [ |
218
|
|
|
'name' => 'summary', |
219
|
|
|
'description' => 'description', |
220
|
|
|
'startDate' => 'start.date', |
221
|
|
|
'endDate' => 'end.date', |
222
|
|
|
'startDateTime' => 'start.dateTime', |
223
|
|
|
'endDateTime' => 'end.dateTime', |
224
|
|
|
][$name] ?? $name; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.