| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Package\CalDav\Entities; |
| 4 |
|
| 5 |
use FluentBooking\Package\CalDav\ICal\Event as ICalEvent; |
| 6 |
|
| 7 |
class Calendar implements \JsonSerializable |
| 8 |
{ |
| 9 |
protected $data = []; |
| 10 |
|
| 11 |
protected $client = null; |
| 12 |
|
| 13 |
protected $events = []; |
| 14 |
|
| 15 |
protected $dateTimeFields = ['dtstart', 'dtend']; |
| 16 |
|
| 17 |
public function __construct(array $data, $client) |
| 18 |
{ |
| 19 |
$this->data = $data; |
| 20 |
$this->client = $client; |
| 21 |
} |
| 22 |
|
| 23 |
public function getData() |
| 24 |
{ |
| 25 |
return $this->data; |
| 26 |
} |
| 27 |
|
| 28 |
public function __get($key) |
| 29 |
{ |
| 30 |
if (isset($this->data[$key])) { |
| 31 |
return $this->data[$key]; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
public function createEvent() |
| 36 |
{ |
| 37 |
$event = new Event(new ICalEvent([]), []); |
| 38 |
|
| 39 |
$event->setCalendar($this); |
| 40 |
|
| 41 |
return $event; |
| 42 |
} |
| 43 |
|
| 44 |
public function getEvents($start = null, $end = null) |
| 45 |
{ |
| 46 |
$dateRange = []; |
| 47 |
|
| 48 |
if (isset($start, $end)) { |
| 49 |
$dateRange = [$start, $end]; |
| 50 |
} |
| 51 |
|
| 52 |
$events = $this->client->getEvents( |
| 53 |
$this->data['href'], $dateRange |
| 54 |
); |
| 55 |
|
| 56 |
$meta = [ |
| 57 |
'href' => $events['href'], |
| 58 |
'etag' => $events['etag'], |
| 59 |
'status' => $events['status'], |
| 60 |
]; |
| 61 |
|
| 62 |
$this->events = array_map(function($e) use ($meta) { |
| 63 |
$data = get_object_vars($e); |
| 64 |
$event = new Event(new ICalEvent($data), $meta); |
| 65 |
$event->setCalendar($this); |
| 66 |
return $event; |
| 67 |
}, $events['events']); |
| 68 |
|
| 69 |
return $this->events; |
| 70 |
} |
| 71 |
|
| 72 |
public function getEvent($uid) |
| 73 |
{ |
| 74 |
$url = rtrim($this->data['href'], '/') . '/' . $uid . '.ics'; |
| 75 |
|
| 76 |
$events = $this->client->getEvent($url); |
| 77 |
|
| 78 |
$meta = [ |
| 79 |
'href' => $url, |
| 80 |
'etag' => $events['etag'], |
| 81 |
'status' => $events['status'], |
| 82 |
]; |
| 83 |
|
| 84 |
$this->events = array_map(function($e) use ($meta) { |
| 85 |
$data = get_object_vars($e); |
| 86 |
$event = new Event(new ICalEvent($data), $meta); |
| 87 |
$event->setCalendar($this); |
| 88 |
return $event; |
| 89 |
}, $events['event']); |
| 90 |
|
| 91 |
return $this->events ? reset($this->events) : $this->events; |
| 92 |
} |
| 93 |
|
| 94 |
public function addEvent($event) |
| 95 |
{ |
| 96 |
if ($event instanceof Event) { |
| 97 |
$event = $event->getIcalEvent(); |
| 98 |
} |
| 99 |
|
| 100 |
$response = $this->client->addEvent( |
| 101 |
$this->data['href'], $event |
| 102 |
); |
| 103 |
|
| 104 |
if (is_array($response) && array_key_exists('code', $response)) { |
| 105 |
return intval($response['code'] / 100) == 2; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function deleteEvent($uid) |
| 110 |
{ |
| 111 |
if ($uid instanceof Event) { |
| 112 |
$uid = $uid->getUid(); |
| 113 |
} |
| 114 |
|
| 115 |
$response = $this->client->deleteEvent( |
| 116 |
rtrim($this->data['href'], '/') . '/' . $uid . '.ics' |
| 117 |
); |
| 118 |
|
| 119 |
if (is_array($response) && array_key_exists('code', $response)) { |
| 120 |
return $response['code'] == 204; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
#[\ReturnTypeWillChange] |
| 125 |
public function jsonSerialize() |
| 126 |
{ |
| 127 |
$this->data['events'] = $this->events; |
| 128 |
|
| 129 |
return $this->data; |
| 130 |
} |
| 131 |
} |
| 132 |
|