|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\GoogleCalendar; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use Google_Service_Calendar; |
|
8
|
|
|
use Google_Service_Calendar_Event; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
|
|
11
|
|
|
class GoogleCalendar |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var \Spatie\GoogleCalendar\Google_Service_Calendar */ |
|
14
|
|
|
protected $calendarService; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $calendarId; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Google_Service_Calendar $calendarService, $calendarId) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->calendarService = $calendarService; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
$this->calendarId = $calendarId; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getCalendarId() : string |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->calendarId; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* List events. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Carbon $startDateTime |
|
35
|
|
|
* @param Carbon $endDateTime |
|
36
|
|
|
* @param array $queryParameters |
|
37
|
|
|
* |
|
38
|
|
|
* @link https://developers.google.com/google-apps/calendar/v3/reference/events/list |
|
39
|
|
|
* |
|
40
|
|
|
* @return Collection |
|
41
|
|
|
*/ |
|
42
|
|
|
public function listEvents(Carbon $startDateTime = null, Carbon $endDateTime = null, $queryParameters = []) : Collection |
|
43
|
|
|
{ |
|
44
|
|
|
$parameters = ['singleEvents' => true]; |
|
45
|
|
|
|
|
46
|
|
|
if (is_null($startDateTime)) { |
|
47
|
|
|
$startDateTime = Carbon::now()->startOfDay(); |
|
48
|
|
|
} |
|
49
|
|
|
$parameters['timeMin'] = $startDateTime->format(DateTime::RFC3339); |
|
50
|
|
|
|
|
51
|
|
|
if (is_null($endDateTime)) { |
|
52
|
|
|
$endDateTime = Carbon::now()->addYear()->endOfDay(); |
|
53
|
|
|
} |
|
54
|
|
|
$parameters['timeMax'] = $endDateTime->format(DateTime::RFC3339); |
|
55
|
|
|
|
|
56
|
|
|
$parameters = array_merge($parameters, $queryParameters); |
|
57
|
|
|
|
|
58
|
|
|
$googleEvents = $this |
|
59
|
|
|
->calendarService |
|
60
|
|
|
->events |
|
61
|
|
|
->listEvents($this->calendarId, $parameters) |
|
62
|
|
|
->getItems(); |
|
63
|
|
|
|
|
64
|
|
|
$events = collect($googleEvents) |
|
65
|
|
|
->map(function (Google_Service_Calendar_Event $event) { |
|
66
|
|
|
return Event::createFromGoogleCalendarEvent($event, $this->calendarId); |
|
67
|
|
|
}) |
|
68
|
|
|
->sortBy(function (Event $event) { |
|
69
|
|
|
return $event->startDateTime->format(DATE_ISO8601); |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
/* |
|
|
|
|
|
|
73
|
|
|
$debug = $events->map(function (Event $event) { |
|
74
|
|
|
return $event->startDateTime->format('Y-m-d H:i:s') . ' - ' . $event->startDateTime->format('Y-m-d H:i:s') . $event->name . PHP_EOL; |
|
75
|
|
|
}); |
|
76
|
|
|
*/ |
|
77
|
|
|
|
|
78
|
|
|
return $events; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $eventId |
|
83
|
|
|
* @return static |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getEvent($eventId) |
|
86
|
|
|
{ |
|
87
|
|
|
$googleEvent = $this->calendarService->events->get($this->calendarId, $eventId); |
|
88
|
|
|
|
|
89
|
|
|
return Event::createFromGoogleCalendarEvent($googleEvent, $this->calendarId); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Insert an event |
|
94
|
|
|
* |
|
95
|
|
|
* @param \Spatie\GoogleCalendar\Event|Google_Service_Calendar_Event $event |
|
96
|
|
|
* |
|
97
|
|
|
* @link https://developers.google.com/google-apps/calendar/v3/reference/events/insert |
|
98
|
|
|
* |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
|
|
public function insertEvent($event) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($event instanceof Event) { |
|
104
|
|
|
$event = $event->convertToGoogleEvent(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->calendarService->events->insert($this->calendarId, $event); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string|\Spatie\GoogleCalendar\Event $eventId |
|
112
|
|
|
*/ |
|
113
|
|
|
public function deleteEvent($eventId) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($eventId instanceof Event) { |
|
116
|
|
|
$eventId = $eventId->id; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->calendarService->events->delete($this->calendarId, $eventId); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getService() : Google_Service_Calendar |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->calendarService; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..