From c29a639c78cc1abd6f96739341a7ebed4e5b9418 Mon Sep 17 00:00:00 2001 From: Marc Beinder <50760632+onairmarc@users.noreply.github.com> Date: Sun, 1 Dec 2024 13:36:37 -0600 Subject: [PATCH] [Core] add for{ObjectType}Id Support (#28) Co-authored-by: onairmarc --- .git-blame-ignore-revs | 1 + src/Objects/Calendar/EventInstance.php | 40 ++++++++++++++++++++-- src/Objects/Calendar/Tag.php | 14 ++++---- src/Objects/Calendar/TagGroup.php | 9 ++++- src/Objects/Groups/Event.php | 13 ++++++- src/Objects/Groups/Group.php | 2 +- src/Objects/Groups/GroupEnrollment.php | 7 ++++ src/Objects/Groups/Tag.php | 2 +- src/Objects/People/Email.php | 16 ++++++++- src/Objects/People/Person.php | 8 ++++- src/Objects/People/Traits/HasEmails.php | 2 +- tests/Unit/Calendar/EventInstanceTests.php | 11 +++--- tests/Unit/Calendar/EventTests.php | 13 +++++-- tests/Unit/Calendar/TagGroupTests.php | 5 +-- tests/Unit/Groups/GroupTests.php | 26 ++++++++------ tests/Unit/People/EmailTests.php | 17 +++++---- tests/Unit/People/PersonTests.php | 16 ++++++--- 17 files changed, 156 insertions(+), 46 deletions(-) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index a1be384..a124cba 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -5,3 +5,4 @@ de929a45798d68147fec3ab9527979a5e5f0fce1 c90d7383de6e86260b6cc997b3d0af8e64826a11 baa146cd798ade580c6a6f4f2cbf98d5b72cc19f cbe36f817a31ddbeb1ef59120b2406f6e9d4a689 +8bc4a90640e6ca09b522425509a0a147837a83e2 diff --git a/src/Objects/Calendar/EventInstance.php b/src/Objects/Calendar/EventInstance.php index 98caf74..18d8fad 100644 --- a/src/Objects/Calendar/EventInstance.php +++ b/src/Objects/Calendar/EventInstance.php @@ -15,6 +15,7 @@ use EncoreDigitalGroup\PlanningCenter\Support\PlanningCenterApiVersion; use EncoreDigitalGroup\PlanningCenter\Support\RelationshipMapper; use EncoreDigitalGroup\PlanningCenter\Traits\HasPlanningCenterClient; +use EncoreDigitalGroup\StdLib\Exceptions\NullExceptions\NullException; /** @api */ class EventInstance @@ -36,10 +37,39 @@ public static function make(string $clientId, string $clientSecret): EventInstan return $event; } + public function forEventInstanceId(string $eventInstanceId): static + { + $this->attributes->eventInstanceId = $eventInstanceId; + + return $this; + } + + public function forEventId(string $eventId): static + { + $this->setupEventRelationship(); + + if ($this->relationships->event !== null && $this->relationships->event->data !== null) { + $this->relationships->event->data->id = $eventId; + } + + return $this; + } + public function all(array $query = []): ClientResponse { - $this->relationships->event = $this->relationships->event ?? new BasicRelationship; - $this->relationships->event->data = $this->relationships->event->data ?? new BasicRelationshipData; + $this->setupEventRelationship(); + + if ($this->relationships->event === null) { + throw new NullException("relationships->event"); + } + + if ($this->relationships->event->data === null) { + throw new NullException("relationships->event->data"); + } + + if ($this->relationships->event->data->id === null) { + throw new NullException("relationships->event->data->id"); + } $http = $this->client() ->get($this->hostname() . Event::EVENT_ENDPOINT . "/{$this->relationships->event->data->id}/event_instances", $query); @@ -88,4 +118,10 @@ private function mapFromPco(mixed $pco): void RelationshipMapper::from($pco, $this->relationships, $relationshipMap); } + + private function setupEventRelationship(): void + { + $this->relationships->event = $this->relationships->event ?? new BasicRelationship; + $this->relationships->event->data = $this->relationships->event->data ?? new BasicRelationshipData; + } } diff --git a/src/Objects/Calendar/Tag.php b/src/Objects/Calendar/Tag.php index 2fc0b5a..45a6202 100644 --- a/src/Objects/Calendar/Tag.php +++ b/src/Objects/Calendar/Tag.php @@ -28,19 +28,19 @@ public static function make(string $clientId, string $clientSecret): Tag return $tagGroup; } - public function all(array $query = []): ClientResponse + public function forTagGroupId(string $tagGroupId): static { - $http = $this->client() - ->get($this->hostname() . TagGroup::TAG_GROUP_ENDPOINT . "/{$this->tagGroupId}/tags", $query); + $this->tagGroupId = $tagGroupId; - return $this->processResponse($http); + return $this; } - public function inTagGroup(string $tagGroupId): static + public function all(array $query = []): ClientResponse { - $this->tagGroupId = $tagGroupId; + $http = $this->client() + ->get($this->hostname() . TagGroup::TAG_GROUP_ENDPOINT . "/{$this->tagGroupId}/tags", $query); - return $this; + return $this->processResponse($http); } /** @internal */ diff --git a/src/Objects/Calendar/TagGroup.php b/src/Objects/Calendar/TagGroup.php index 21e2185..a108162 100644 --- a/src/Objects/Calendar/TagGroup.php +++ b/src/Objects/Calendar/TagGroup.php @@ -29,6 +29,13 @@ public static function make(string $clientId, string $clientSecret): TagGroup return $tagGroup; } + public function forTagGroupId(string $tagGroupId): static + { + $this->attributes->tagGroupId = $tagGroupId; + + return $this; + } + public function all(array $query = []): ClientResponse { $http = $this->client() @@ -40,7 +47,7 @@ public function all(array $query = []): ClientResponse public function tags(array $query = []): ClientResponse { return Tag::make($this->clientId, $this->clientSecret) - ->inTagGroup($this->attributes->tagGroupId) + ->forTagGroupId($this->attributes->tagGroupId) ->all($query); } diff --git a/src/Objects/Groups/Event.php b/src/Objects/Groups/Event.php index bc4553f..3a2d0cb 100644 --- a/src/Objects/Groups/Event.php +++ b/src/Objects/Groups/Event.php @@ -30,6 +30,13 @@ public static function make(string $clientId, string $clientSecret): Event return $event; } + public function forEventId(string $eventId): static + { + $this->attributes->eventId = $eventId; + + return $this; + } + public function all(array $query = []): ClientResponse { $http = $this->client() @@ -56,7 +63,11 @@ public function get(array $query = []): ClientResponse private function mapFromPco(mixed $pco): void { - $pco = objectify($pco); + $pco = pco_objectify($pco); + + if (is_null($pco)) { + return; + } $attributeMap = [ "eventId" => "id", diff --git a/src/Objects/Groups/Group.php b/src/Objects/Groups/Group.php index 1e71bf9..5b7d42c 100644 --- a/src/Objects/Groups/Group.php +++ b/src/Objects/Groups/Group.php @@ -100,7 +100,7 @@ public function people(array $query = []): ClientResponse public function tags(array $query = []): ClientResponse { return Tag::make($this->clientId, $this->clientSecret) - ->forGroup($this->attributes->groupId) + ->forGroupId($this->attributes->groupId) ->groups($query); } diff --git a/src/Objects/Groups/GroupEnrollment.php b/src/Objects/Groups/GroupEnrollment.php index 348e3b4..bd5a433 100644 --- a/src/Objects/Groups/GroupEnrollment.php +++ b/src/Objects/Groups/GroupEnrollment.php @@ -32,6 +32,13 @@ public static function make(string $clientId, string $clientSecret): GroupEnroll return $group; } + public function forGroupId(string $groupId): static + { + $this->attributes->groupId = $groupId; + + return $this; + } + public function get(array $query = []): ClientResponse { $http = $this->client() diff --git a/src/Objects/Groups/Tag.php b/src/Objects/Groups/Tag.php index a24655b..5d8b37f 100644 --- a/src/Objects/Groups/Tag.php +++ b/src/Objects/Groups/Tag.php @@ -31,7 +31,7 @@ public static function make(string $clientId, string $clientSecret): Tag return $tag; } - public function forGroup(string $groupId): static + public function forGroupId(string $groupId): static { $this->groupId = $groupId; diff --git a/src/Objects/People/Email.php b/src/Objects/People/Email.php index 995a906..d513bb8 100644 --- a/src/Objects/People/Email.php +++ b/src/Objects/People/Email.php @@ -31,6 +31,20 @@ public static function make(?string $clientId = null, ?string $clientSecret = nu return $email; } + public function forEmailAddressId(string $emailAddressId): static + { + $this->attributes->emailAddressId = $emailAddressId; + + return $this; + } + + public function forPersonId(string $personId): static + { + $this->attributes->personId = $personId; + + return $this; + } + public function get(): ClientResponse { $http = $this->client() @@ -39,7 +53,7 @@ public function get(): ClientResponse return $this->processResponse($http); } - public function forPerson(): ClientResponse + public function person(): ClientResponse { $http = $this->client() ->get($this->hostname() . Person::PEOPLE_ENDPOINT . "/{$this->attributes->personId}/emails"); diff --git a/src/Objects/People/Person.php b/src/Objects/People/Person.php index f418e4b..ee2c085 100644 --- a/src/Objects/People/Person.php +++ b/src/Objects/People/Person.php @@ -177,7 +177,13 @@ private function mapToPco(): array ], ]; - unset($person["data"]["attributes"]["id"], $person["data"]["attributes"]["created_at"], $person["data"]["attributes"]["updated_at"], $person["data"]["attributes"]["name"], $person["data"]["attributes"]["demographic_avatar_url"]); + unset( + $person["data"]["attributes"]["id"], + $person["data"]["attributes"]["created_at"], + $person["data"]["attributes"]["updated_at"], + $person["data"]["attributes"]["name"], + $person["data"]["attributes"]["demographic_avatar_url"] + ); return Arr::whereNotNull($person["data"]["attributes"]); } diff --git a/src/Objects/People/Traits/HasEmails.php b/src/Objects/People/Traits/HasEmails.php index 589018f..deb83b3 100644 --- a/src/Objects/People/Traits/HasEmails.php +++ b/src/Objects/People/Traits/HasEmails.php @@ -29,6 +29,6 @@ public function email(): ClientResponse $email = Email::make($this->clientId, $this->clientSecret); $email->attributes->personId = $this->attributes->personId; - return $email->forPerson(); + return $email->person(); } } \ No newline at end of file diff --git a/tests/Unit/Calendar/EventInstanceTests.php b/tests/Unit/Calendar/EventInstanceTests.php index 7bbae40..11e8a8b 100644 --- a/tests/Unit/Calendar/EventInstanceTests.php +++ b/tests/Unit/Calendar/EventInstanceTests.php @@ -10,9 +10,11 @@ describe("Calendar EventInstance Tests", function (): void { test("EventInstance: Can Get EventInstance By ID", function (): void { $eventInstance = EventInstance::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $eventInstance->attributes->eventInstanceId = "1"; - $response = $eventInstance->get(); + $response = $eventInstance + ->forEventInstanceId("1") + ->get(); + /** @var EventInstance $calendarEventInstance */ $calendarEventInstance = $response->data->first(); @@ -23,9 +25,10 @@ test("EventInstance: Can List All EventInstances", function (): void { $eventInstance = EventInstance::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $eventInstance->relationships->event->data->id = "1"; - $response = $eventInstance->all(); + $response = $eventInstance + ->forEventId("1") + ->all(); expect($response)->toBeInstanceOf(ClientResponse::class) ->and($response->data)->toBeInstanceOf(Collection::class) diff --git a/tests/Unit/Calendar/EventTests.php b/tests/Unit/Calendar/EventTests.php index c7d4aa0..7d3f410 100644 --- a/tests/Unit/Calendar/EventTests.php +++ b/tests/Unit/Calendar/EventTests.php @@ -13,7 +13,10 @@ test("Event: Can Get Event By ID", function (): void { $event = Event::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $response = $event->forEventId("1")->get(); + $response = $event + ->forEventId("1") + ->get(); + /** @var Event $calendarEvent */ $calendarEvent = $response->data->first(); @@ -35,7 +38,9 @@ test("Event: Can List All Event Instances For A Specific Event", function (): void { $event = Event::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $response = $event->forEventId("1")->instances(); + $response = $event + ->forEventId("1") + ->instances(); /** @var EventInstance $eventInstance */ $eventInstance = $response->data->first(); @@ -50,7 +55,9 @@ test("Event: Can List All Tags Associated with an Event", function (): void { $event = Event::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $response = $event->forEventId("1")->tags(); + $response = $event + ->forEventId("1") + ->tags(); /** @var Event $calendarEvent */ $calendarEvent = $response->data->first(); diff --git a/tests/Unit/Calendar/TagGroupTests.php b/tests/Unit/Calendar/TagGroupTests.php index 6aefacc..c437268 100644 --- a/tests/Unit/Calendar/TagGroupTests.php +++ b/tests/Unit/Calendar/TagGroupTests.php @@ -26,9 +26,10 @@ test("TagGroup: Can List All Tags For A Specific TagGroup", function (): void { $tagGroup = TagGroup::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $tagGroup->attributes->tagGroupId = "1"; - $response = $tagGroup->tags(); + $response = $tagGroup + ->forTagGroupId("1") + ->tags(); /** @var TagAttributes $tagAttributes */ $tagAttributes = $response->data->first()->attributes; diff --git a/tests/Unit/Groups/GroupTests.php b/tests/Unit/Groups/GroupTests.php index 2c98931..008b08c 100644 --- a/tests/Unit/Groups/GroupTests.php +++ b/tests/Unit/Groups/GroupTests.php @@ -31,9 +31,11 @@ test("Group: Can Get Group By ID", function (): void { $group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $group->attributes->groupId = "1"; - $response = $group->get(); + $response = $group + ->forGroupId("1") + ->get(); + /** @var GroupAttributes $groupAttributes */ $groupAttributes = $response->data->first()->attributes; @@ -54,9 +56,10 @@ test("Group: Can List Group Memberships", function (): void { $group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $group->attributes->groupId = "1"; - $response = $group->membership(); + $response = $group + ->forGroupId("1") + ->membership(); /** @var GroupMembership $membership */ $membership = $response->data->first(); @@ -71,9 +74,10 @@ test("Group: Can List Group People", function (): void { $group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $group->attributes->groupId = "1"; - $response = $group->people(); + $response = $group + ->forGroupId("1") + ->people(); /** @var GroupMemberPersonAttributes $groupMember */ $groupMember = $response->data->first()->attributes; @@ -88,9 +92,10 @@ test("Group: Can List Tags Assigned to Group", function (): void { $group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $group->attributes->groupId = "1"; - $response = $group->tags(); + $response = $group + ->forGroupId("1") + ->tags(); /** @var Tag $tag */ $tag = $response->data->first(); @@ -105,9 +110,10 @@ test("Group: Can Get Group Enrollment Details", function (): void { $group = Group::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $group->attributes->groupId = "1"; - $response = $group->enrollment(); + $response = $group + ->forGroupId("1") + ->enrollment(); /** @var GroupEnrollmentAttributes $enrollmentAttributes */ $enrollmentAttributes = $response->data->first()->attributes; diff --git a/tests/Unit/People/EmailTests.php b/tests/Unit/People/EmailTests.php index 85f3630..5b8515a 100644 --- a/tests/Unit/People/EmailTests.php +++ b/tests/Unit/People/EmailTests.php @@ -10,9 +10,11 @@ describe("People Email Tests", function (): void { test("Email: Can Get Email By ID", function (): void { $email = Email::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $email->attributes->emailAddressId = "1"; - $response = $email->get(); + $response = $email + ->forEmailAddressId("1") + ->get(); + /** @var Email $emailAddress */ $emailAddress = $response->data->first(); @@ -23,9 +25,10 @@ test("Email: Can List All Emails For Person", function (): void { $email = Email::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $email->attributes->personId = "1"; - $response = $email->forPerson(); + $response = $email + ->forPersonId("1") + ->person(); expect($response)->toBeInstanceOf(ClientResponse::class) ->and($response->data)->toBeInstanceOf(Collection::class) @@ -34,10 +37,12 @@ test("Email: Can Update Email", function (): void { $email = Email::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $email->attributes->emailAddressId = "1"; $email->attributes->address = "john.smith@example.com"; - $response = $email->update(); + $response = $email + ->forEmailAddressId("1") + ->update(); + /** @var Email $emailAddress */ $emailAddress = $response->data->first(); diff --git a/tests/Unit/People/PersonTests.php b/tests/Unit/People/PersonTests.php index b7366ab..84ee0f9 100644 --- a/tests/Unit/People/PersonTests.php +++ b/tests/Unit/People/PersonTests.php @@ -35,7 +35,10 @@ test("People: Can Get Person By ID", function (): void { $person = Person::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $response = $person->forPersonId("1")->get(); + $response = $person + ->forPersonId("1") + ->get(); + /** @var Person $personProfile */ $personProfile = $response->data->first(); @@ -47,9 +50,11 @@ test("People: Can Update Person Profile", function (): void { $person = Person::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $person->attributes->personId = "1"; - $response = $person->update(); + $response = $person + ->forPersonId("1") + ->update(); + /** @var Person $personProfile */ $personProfile = $response->data->first(); @@ -61,9 +66,10 @@ test("People: Can Delete Person Profile", function (): void { $person = Person::make(TestConstants::CLIENT_ID, TestConstants::CLIENT_SECRET); - $person->attributes->personId = "1"; - $response = $person->delete(); + $response = $person + ->forPersonId("1") + ->delete(); expect($response->data->isEmpty())->toBeTrue(); });