diff --git a/src/Calendars/Calendar.php b/src/Calendars/Calendar.php index 85cde2e..b7b5313 100644 --- a/src/Calendars/Calendar.php +++ b/src/Calendars/Calendar.php @@ -2,6 +2,7 @@ namespace Nylas\Calendars; +use DateTimeZone; use Nylas\Utilities\API; use Nylas\Utilities\Helper; use Nylas\Utilities\Options; @@ -69,6 +70,33 @@ public function getCalendarsList(array $params = []): array // ------------------------------------------------------------------------------ + /** + * add calendar + * + * @param array $params + * + * @return array + */ + public function addCalendar(array $params): array + { + $rules = $this->addCalendarRules(); + + $accessToken = $this->options->getAccessToken(); + + V::doValidate(V::keySet(...$rules), $params); + V::doValidate(V::stringType()->notEmpty(), $accessToken); + + $header = ['Authorization' => $accessToken]; + + return $this->options + ->getSync() + ->setFormParams($params) + ->setHeaderParams($header) + ->post(API::LIST['calendars']); + } + + // ------------------------------------------------------------------------------ + /** * get calendar * @@ -109,4 +137,22 @@ public function getCalendar($calendarId): array } // ------------------------------------------------------------------------------ + + /** + * rules for add calendar + * + * @return array + */ + private function addCalendarRules(): array + { + return + [ + V::key('name', V::stringType()->notEmpty()), + V::keyOptional('description', V::stringType()->notEmpty()), + V::keyOptional('location', V::stringType()->notEmpty()), + V::keyOptional('timezone', V::in(DateTimeZone::listIdentifiers())), + ]; + } + + // ------------------------------------------------------------------------------ } diff --git a/tests/CalendarTest.php b/tests/CalendarTest.php index 2f37fe4..b5f1f14 100644 --- a/tests/CalendarTest.php +++ b/tests/CalendarTest.php @@ -40,4 +40,28 @@ public function testGetCalendar(): void } // ------------------------------------------------------------------------------ + + public function testAddCalendar(): void + { + $params = $this->getCalendarInfo(); + + $data = $this->client->Calendars()->Calendar()->addCalendar($params); + + $this->assertArrayHasKey('id', $data); + } + + // ------------------------------------------------------------------------------ + + /** + * @return array + */ + private function getCalendarInfo(): array + { + return [ + 'name' => 'Test Calendar', + 'description' => 'This is a test calendar.', + 'timezone' => 'America/New_York', + 'location' => 'Front Conference Room', + ]; + } }