diff --git a/src/Calendars/Calendar.php b/src/Calendars/Calendar.php index b7b5313..a353b2a 100644 --- a/src/Calendars/Calendar.php +++ b/src/Calendars/Calendar.php @@ -97,6 +97,38 @@ public function addCalendar(array $params): array // ------------------------------------------------------------------------------ + /** + * update calendar + * + * @param array $params + * + * @return array + */ + public function updateCalendar(array $params): array + { + $rules = $this->addCalendarRules(); + $rules[] = V::key('id', V::stringType()->notEmpty()); + + $accessToken = $this->options->getAccessToken(); + + V::doValidate(V::keySet(...$rules), $params); + V::doValidate(V::stringType()->notEmpty(), $accessToken); + + $path = $params['id']; + $header = ['Authorization' => $accessToken]; + + unset($params['id']); + + return $this->options + ->getSync() + ->setPath($path) + ->setFormParams($params) + ->setHeaderParams($header) + ->put(API::LIST['oneCalendar']); + } + + // ------------------------------------------------------------------------------ + /** * get calendar * @@ -138,6 +170,47 @@ public function getCalendar($calendarId): array // ------------------------------------------------------------------------------ + /** + * delete calendar + * + * @param mixed $contactId string|string[] + * + * @return array + */ + public function deleteCalendar($contactId): array + { + $calendarId = Helper::fooToArray($contactId); + $accessToken = $this->options->getAccessToken(); + + $rule = V::simpleArray(V::stringType()->notEmpty()); + + V::doValidate($rule, $calendarId); + V::doValidate(V::stringType()->notEmpty(), $accessToken); + + $queues = []; + $target = API::LIST['oneCalendar']; + $header = ['Authorization' => $accessToken]; + + foreach ($calendarId as $id) + { + $request = $this->options + ->getAsync() + ->setPath($id) + ->setHeaderParams($header); + + $queues[] = static function () use ($request, $target) + { + return $request->delete($target); + }; + } + + $pools = $this->options->getAsync()->pool($queues, false); + + return Helper::concatPoolInfos($calendarId, $pools); + } + + // ------------------------------------------------------------------------------ + /** * rules for add calendar * diff --git a/tests/CalendarTest.php b/tests/CalendarTest.php index 8a97d30..e582f03 100644 --- a/tests/CalendarTest.php +++ b/tests/CalendarTest.php @@ -52,6 +52,47 @@ public function testAddCalendar(): void // ------------------------------------------------------------------------------ + public function testUpdateCalendar(): void + { + $data = $this->client->Calendars()->Calendar()->addCalendar($this->getCalendarInfo()); + + $params = + [ + 'id' => $data['id'], + 'name' => 'Different Calendar', + 'description' => 'This is now a different calendar.', + 'timezone' => 'America/Los_Angeles', + ]; + + $data = $this->client->Calendars()->Calendar()->updateContact($params); + + $this->assertEquals($data['id'], $params['id']); + $this->assertEquals($data['name'], $params['name']); + $this->assertEquals($data['description'], $params['description']); + $this->assertEquals($data['timezone'], $params['timezone']); + } + + // ------------------------------------------------------------------------------ + + public function testDeleteCalendar(): void + { + $data = $this->client->Calendars()->Calendar()->addCalendar($this->getCalendarInfo()); + + try + { + $back = true; + $this->client->Calendars()->Calendar()->deleteCalendar($data['id']); + } + catch (Exception $e) + { + $back = false; + } + + $this->assertTrue($back); + } + + // ------------------------------------------------------------------------------ + /** * @return array */ @@ -60,7 +101,7 @@ private function getCalendarInfo(): array return [ 'name' => 'Test Calendar', 'description' => 'This is a test calendar.', - 'timezone' => 'America/New_York' + 'timezone' => 'America/New_York', 'location' => 'Front Conference Room', ]; }