1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\GoogleCalendar; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Google_Service_Calendar_Event; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Google_Service_Calendar_EventDateTime; |
12
|
|
|
|
13
|
|
|
class Event |
14
|
|
|
{ |
15
|
|
|
/** @var \Google_Service_Calendar_Event */ |
16
|
|
|
public $googleEvent; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $calendarId; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
protected $attendees; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->attendees = []; |
27
|
|
|
$this->googleEvent = new Google_Service_Calendar_Event; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Google_Service_Calendar_Event $googleEvent |
32
|
|
|
* @param $calendarId |
33
|
|
|
* |
34
|
|
|
* @return static |
35
|
|
|
*/ |
36
|
|
|
public static function createFromGoogleCalendarEvent(Google_Service_Calendar_Event $googleEvent, $calendarId) |
37
|
|
|
{ |
38
|
|
|
$event = new static; |
39
|
|
|
|
40
|
|
|
$event->googleEvent = $googleEvent; |
41
|
|
|
$event->calendarId = $calendarId; |
42
|
|
|
|
43
|
|
|
return $event; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $properties |
48
|
|
|
* @param string|null $calendarId |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public static function create(array $properties, string $calendarId = null, $optParams = []) |
53
|
|
|
{ |
54
|
|
|
$event = new static; |
55
|
|
|
|
56
|
|
|
$event->calendarId = static::getGoogleCalendar($calendarId)->getCalendarId(); |
57
|
|
|
|
58
|
|
|
foreach ($properties as $name => $value) { |
59
|
|
|
$event->$name = $value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $event->save('insertEvent', $optParams); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection |
66
|
|
|
{ |
67
|
|
|
$googleCalendar = static::getGoogleCalendar($calendarId); |
68
|
|
|
|
69
|
|
|
$googleEvents = $googleCalendar->listEvents($startDateTime, $endDateTime, $queryParameters); |
70
|
|
|
|
71
|
|
|
$useUserOrder = isset($queryParameters['orderBy']); |
72
|
|
|
|
73
|
|
|
return collect($googleEvents) |
74
|
|
|
->map(function (Google_Service_Calendar_Event $event) use ($calendarId) { |
75
|
|
|
return static::createFromGoogleCalendarEvent($event, $calendarId); |
76
|
|
|
}) |
77
|
|
|
->sortBy(function (self $event, $index) use ($useUserOrder) { |
78
|
|
|
if ($useUserOrder) { |
79
|
|
|
return $index; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $event->sortDate; |
83
|
|
|
}) |
84
|
|
|
->values(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function find($eventId, string $calendarId = null): self |
88
|
|
|
{ |
89
|
|
|
$googleCalendar = static::getGoogleCalendar($calendarId); |
90
|
|
|
|
91
|
|
|
$googleEvent = $googleCalendar->getEvent($eventId); |
92
|
|
|
|
93
|
|
|
return static::createFromGoogleCalendarEvent($googleEvent, $calendarId); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function __get($name) |
97
|
|
|
{ |
98
|
|
|
$name = $this->getFieldName($name); |
99
|
|
|
|
100
|
|
|
if ($name === 'sortDate') { |
101
|
|
|
return $this->getSortDate(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$value = Arr::get($this->googleEvent, $name); |
105
|
|
|
|
106
|
|
|
if (in_array($name, ['start.date', 'end.date']) && $value) { |
107
|
|
|
$value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime']) && $value) { |
111
|
|
|
$value = Carbon::createFromFormat(DateTime::RFC3339, $value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function __set($name, $value) |
118
|
|
|
{ |
119
|
|
|
$name = $this->getFieldName($name); |
120
|
|
|
|
121
|
|
|
if (in_array($name, ['start.date', 'end.date', 'start.dateTime', 'end.dateTime'])) { |
122
|
|
|
$this->setDateProperty($name, $value); |
123
|
|
|
|
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
Arr::set($this->googleEvent, $name, $value); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function exists(): bool |
131
|
|
|
{ |
132
|
|
|
return $this->id != ''; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function isAllDayEvent(): bool |
136
|
|
|
{ |
137
|
|
|
return is_null($this->googleEvent['start']['dateTime']); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function save(string $method = null, $optParams = []): self |
141
|
|
|
{ |
142
|
|
|
$method = $method ?? ($this->exists() ? 'updateEvent' : 'insertEvent'); |
143
|
|
|
|
144
|
|
|
$googleCalendar = $this->getGoogleCalendar($this->calendarId); |
145
|
|
|
|
146
|
|
|
$this->googleEvent->setAttendees($this->attendees); |
147
|
|
|
|
148
|
|
|
$googleEvent = $googleCalendar->$method($this, $optParams); |
149
|
|
|
|
150
|
|
|
return static::createFromGoogleCalendarEvent($googleEvent, $googleCalendar->getCalendarId()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function update(array $attributes, $optParams = []): self |
154
|
|
|
{ |
155
|
|
|
foreach ($attributes as $name => $value) { |
156
|
|
|
$this->$name = $value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->save('updateEvent', $optParams); |
160
|
|
|
} |
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
|
|
|
public function getCalendarId(): string |
186
|
|
|
{ |
187
|
|
|
return $this->calendarId; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
protected static function getGoogleCalendar(string $calendarId = null): GoogleCalendar |
191
|
|
|
{ |
192
|
|
|
$calendarId = $calendarId ?? config('google-calendar.calendar_id'); |
193
|
|
|
|
194
|
|
|
return GoogleCalendarFactory::createForCalendarId($calendarId); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
protected function setDateProperty(string $name, Carbon $date) |
198
|
|
|
{ |
199
|
|
|
$eventDateTime = new Google_Service_Calendar_EventDateTime; |
200
|
|
|
|
201
|
|
|
if (in_array($name, ['start.date', 'end.date'])) { |
202
|
|
|
$eventDateTime->setDate($date->format('Y-m-d')); |
203
|
|
|
$eventDateTime->setTimezone($date->getTimezone()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (in_array($name, ['start.dateTime', 'end.dateTime'])) { |
207
|
|
|
$eventDateTime->setDateTime($date->format(DateTime::RFC3339)); |
208
|
|
|
$eventDateTime->setTimezone($date->getTimezone()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (Str::startsWith($name, 'start')) { |
212
|
|
|
$this->googleEvent->setStart($eventDateTime); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (Str::startsWith($name, 'end')) { |
216
|
|
|
$this->googleEvent->setEnd($eventDateTime); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
protected function getFieldName(string $name): string |
221
|
|
|
{ |
222
|
|
|
return [ |
223
|
|
|
'name' => 'summary', |
224
|
|
|
'description' => 'description', |
225
|
|
|
'startDate' => 'start.date', |
226
|
|
|
'endDate' => 'end.date', |
227
|
|
|
'startDateTime' => 'start.dateTime', |
228
|
|
|
'endDateTime' => 'end.dateTime', |
229
|
|
|
][$name] ?? $name; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: