diff --git a/src/Activity.php b/src/Activity.php deleted file mode 100644 index dae7d1b..0000000 --- a/src/Activity.php +++ /dev/null @@ -1,30 +0,0 @@ -only(['descriptor', 'id']) - ->mapWithKeys(function ($item, $key) { - if ('id' == $key) { - return ['id' => $item]; - } - - return ['name' => $item]; - })->toArray(); - - return new self($item); - } - - public function subactivities() - { - return Nami::subactivitiesOf($this->id); - } -} diff --git a/src/Api.php b/src/Api.php index b3c7b55..2f07d83 100644 --- a/src/Api.php +++ b/src/Api.php @@ -10,6 +10,9 @@ use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; use Zoomyboy\LaravelNami\Authentication\Authenticator; +use Zoomyboy\LaravelNami\Data\Activity; +use Zoomyboy\LaravelNami\Data\Group; +use Zoomyboy\LaravelNami\Data\Subactivity; use Zoomyboy\LaravelNami\Data\Baustein; use Zoomyboy\LaravelNami\Data\Course; use Zoomyboy\LaravelNami\Data\Member; @@ -198,14 +201,17 @@ class Api } } - public function subactivitiesOf(int $activityId): Collection + /** + * @return Collection + */ + public function subactivitiesOf(Activity $activity): Collection { $this->assertLoggedIn(); return $this->fetchCollection( - '/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/'.$activityId, + '/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/'.$activity->id, 'Fetch subactivities failed' - )->map(fn ($subactivity) => Subactivity::fromNami($subactivity)); + )->map(fn ($subactivity) => Subactivity::from($subactivity)); } public function membership(int $memberId, int $membershipId): Membership @@ -318,14 +324,17 @@ class Api return false !== $this->groups()->search(fn ($group) => $group->id == $groupId); } - public function groups(int $parentGroupId = null): Collection + /** + * @return Collection + */ + public function groups(?Group $parentGroup = null): Collection { $this->assertLoggedIn(); return $this->fetchCollection( - '/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/'.($parentGroupId ?: 'root'), + '/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/'.($parentGroup ? $parentGroup->id : 'root'), 'Group fetch failed' - )->map(fn ($group) => Group::fromResponse($group, $parentGroupId)); + )->map(fn ($group) => Group::from([...$group, 'parentId' => $parentGroup ? $parentGroup->id : null])); } public function group(int $groupId): ?Group @@ -335,13 +344,6 @@ class Api return $this->groups()->first(fn ($group) => $group->id == $groupId); } - public function subgroupsOf(int $groupId): Collection - { - $this->assertLoggedIn(); - - return $this->groups($groupId); - } - public function genders(): Collection { $this->assertLoggedIn(); @@ -392,12 +394,15 @@ class Api ->map(fn ($confession) => Confession::fromNami($confession)); } - public function activities(int $groupId): Collection + /** + * @return Collection + */ + public function activities(Group $group): Collection { $this->assertLoggedIn(); - return $this->fetchCollection("/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/{$groupId}", 'Fetch activities failed') - ->map(fn ($activity) => Activity::fromNami($activity)); + return $this->fetchCollection("/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/{$group->id}", 'Fetch activities failed') + ->map(fn ($activity) => Activity::from($activity)); } /** diff --git a/src/Casters/GroupCast.php b/src/Casters/GroupCast.php new file mode 100644 index 0000000..883c1be --- /dev/null +++ b/src/Casters/GroupCast.php @@ -0,0 +1,25 @@ + $context + */ + public function cast(DataProperty $property, mixed $value, array $context): ?string + { + if ($property->type->isNullable && !$value) { + return null; + } + + return $value; + } +} diff --git a/src/Data/Activity.php b/src/Data/Activity.php new file mode 100644 index 0000000..88cbd7f --- /dev/null +++ b/src/Data/Activity.php @@ -0,0 +1,17 @@ + + */ + public function subactivities(): Collection + { + return Nami::subactivitiesOf($this); + } +} diff --git a/src/Data/Group.php b/src/Data/Group.php new file mode 100644 index 0000000..2d272b2 --- /dev/null +++ b/src/Data/Group.php @@ -0,0 +1,33 @@ + + */ + public function activities(): Collection + { + return Nami::activities($this); + } + + /** + * @return Collection + */ + public function children(): Collection { + return Nami::groups($this); + } +} diff --git a/src/Data/Membership.php b/src/Data/Membership.php index e656421..1f998d3 100644 --- a/src/Data/Membership.php +++ b/src/Data/Membership.php @@ -49,4 +49,9 @@ class Membership extends Data 'untergliederungId' => $this->subactivityId, ]; } + + public function group(): Group { + return Group::from(['name' => $this->group ?: '', 'id' => $this->groupId]); + } + } diff --git a/src/Data/Subactivity.php b/src/Data/Subactivity.php new file mode 100644 index 0000000..37a8648 --- /dev/null +++ b/src/Data/Subactivity.php @@ -0,0 +1,8 @@ +setName($response['descriptor']) - ->setId($response['id']) - ->setParentId($parent); - } - - public function setName(string $name): self - { - $this->name = $name; - - return $this; - } - - public function setId(int $id): self - { - $this->id = $id; - - return $this; - } - - public function setParentId(?int $parentId = null): self - { - $this->parentId = $parentId; - - return $this; - } - - public function toArray() - { - return ['id' => $this->id, 'name' => $this->name, 'parent_id' => $this->parentId]; - } - - public function subgroups() - { - return Nami::subgroupsOf($this->id); - } - - public function fees() - { - return Nami::feesOf($this->id); - } - - public function activities(): Collection - { - return Nami::activities($this->id); - } -} diff --git a/src/Subactivity.php b/src/Subactivity.php deleted file mode 100644 index 9c2cd3f..0000000 --- a/src/Subactivity.php +++ /dev/null @@ -1,35 +0,0 @@ -only(['descriptor', 'id']) - ->mapWithKeys(function ($item, $key) { - if ('id' == $key) { - return ['id' => $item]; - } - - return ['name' => $item]; - })->toArray(); - - return new self($item); - } - - public function getNameAttribute() - { - return ucfirst($this->attributes['name']); - } - - public function getIsNullAttribute() - { - return $this->attributes['id'] == self::getNullValue(); - } -} diff --git a/tests/Unit/Api/MembershipTest.php b/tests/Unit/Api/MembershipTest.php index 6e95c53..c02ff02 100644 --- a/tests/Unit/Api/MembershipTest.php +++ b/tests/Unit/Api/MembershipTest.php @@ -35,7 +35,8 @@ class MembershipTest extends TestCase $this->assertSame(1000, $membership->groupId); $this->assertSame(15, $membership->activityId); $this->assertSame(16, $membership->subactivityId); - $this->assertSame('::group::', $membership->group); + $this->assertSame('::group::', $membership->group()->name); + $this->assertSame(1000, $membership->group()->id); } public function testFetchesMembership(): void diff --git a/tests/Unit/GetGroupsTest.php b/tests/Unit/GetGroupsTest.php index f579886..e9e93af 100644 --- a/tests/Unit/GetGroupsTest.php +++ b/tests/Unit/GetGroupsTest.php @@ -3,7 +3,7 @@ namespace Zoomyboy\LaravelNami\Tests\Unit; use Illuminate\Support\Facades\Http; -use Zoomyboy\LaravelNami\Group; +use Zoomyboy\LaravelNami\Data\Group; use Zoomyboy\LaravelNami\Tests\TestCase; class GetGroupsTest extends TestCase @@ -18,11 +18,11 @@ class GetGroupsTest extends TestCase 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' => Http::response($this->groupsResponse, 200), ]); - $groups = $this->login()->groups(); + $group = $this->login()->groups()->first(); - $this->assertEquals([ - ['id' => 100, 'name' => 'Group', 'parent_id' => null], - ], $groups->toArray()); + $this->assertSame(100, $group->id); + $this->assertSame('Group', $group->name); + $this->assertNull($group->parentId); Http::assertSent(function ($request) { return 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' == $request->url(); }); @@ -49,13 +49,13 @@ class GetGroupsTest extends TestCase 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/100' => Http::response($this->subgroupsResponse, 200), ]); - $subgroups = $this->login()->group(100)->subgroups(); + $subgroups = $this->login()->group(100)->children(); $this->assertEquals([ - ['id' => 101300, 'parent_id' => 100, 'name' => 'Siebengebirge'], - ['id' => 100900, 'parent_id' => 100, 'name' => 'Sieg'], - ['id' => 100900, 'parent_id' => 100, 'name' => 'Sieg'], - ['id' => 101000, 'parent_id' => 100, 'name' => 'Voreifel'], + ['id' => 101300, 'parentId' => 100, 'name' => 'Siebengebirge'], + ['id' => 100900, 'parentId' => 100, 'name' => 'Sieg'], + ['id' => 100900, 'parentId' => 100, 'name' => 'Sieg'], + ['id' => 101000, 'parentId' => 100, 'name' => 'Voreifel'], ], $subgroups->toArray()); $subgroups->each(function ($group) { $this->assertInstanceOf(Group::class, $group); diff --git a/tests/Unit/GroupsTest.php b/tests/Unit/GroupsTest.php index 53b6eca..c153b7d 100644 --- a/tests/Unit/GroupsTest.php +++ b/tests/Unit/GroupsTest.php @@ -7,7 +7,7 @@ use Zoomyboy\LaravelNami\Exceptions\NoJsonReceivedException; use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException; use Zoomyboy\LaravelNami\Exceptions\NotSuccessfulException; use Zoomyboy\LaravelNami\Fakes\GroupFake; -use Zoomyboy\LaravelNami\Group; +use Zoomyboy\LaravelNami\Data\Group; use Zoomyboy\LaravelNami\Nami; use Zoomyboy\LaravelNami\Tests\TestCase; @@ -32,6 +32,7 @@ class GroupsTest extends TestCase $this->assertInstanceOf(Group::class, $group); $this->assertEquals('testgroup', $group->name); $this->assertEquals(1234, $group->id); + $this->assertNull($group->parentId); app(GroupFake::class)->assertRootFetched(); } @@ -45,10 +46,11 @@ class GroupsTest extends TestCase 555 => ['name' => 'ABC'], ]); - $group = Nami::login(12345, 'secret')->groups(1234)->first(); + $group = Nami::login(12345, 'secret')->groups(Group::from(['id' => 1234, 'name' => 'lorem', 'parentId' => null]))->first(); $this->assertEquals('ABC', $group->name); $this->assertEquals(555, $group->id); + $this->assertEquals(1234, $group->parentId); app(GroupFake::class)->assertFetched(1234); } @@ -77,7 +79,7 @@ class GroupsTest extends TestCase ]); app(GroupFake::class)->failsToFetch(1234); - Nami::login(12345, 'secret')->groups(1234); + Nami::login(12345, 'secret')->groups(Group::from(['id' => 1234, 'name' => 'lorem', 'parentId' => null])); } public function testItDoesntReturnGroupWhenNoJsonIsReturned(): void diff --git a/tests/Unit/PullActivitiesTest.php b/tests/Unit/PullActivitiesTest.php index d7d03f9..76c1146 100644 --- a/tests/Unit/PullActivitiesTest.php +++ b/tests/Unit/PullActivitiesTest.php @@ -3,6 +3,7 @@ namespace Zoomyboy\LaravelNami\Tests\Unit; use Illuminate\Support\Facades\Http; +use Zoomyboy\LaravelNami\Data\Activity; use Zoomyboy\LaravelNami\Exceptions\NoJsonReceivedException; use Zoomyboy\LaravelNami\Exceptions\NotSuccessfulException; use Zoomyboy\LaravelNami\Fakes\SubactivityFake; @@ -22,8 +23,8 @@ class PullActivitiesTest extends TestCase $activities = $this->login()->group(103)->activities(); $this->assertSame([ - ['name' => 'Ac1', 'id' => 4], - ['name' => 'Ac2', 'id' => 3], + ['id' => 4, 'name' => 'Ac1'], + ['id' => 3, 'name' => 'Ac2'], ], $activities->toArray()); Http::assertSentCount(2); } @@ -39,8 +40,8 @@ class PullActivitiesTest extends TestCase $subactivities = $this->login()->group(103)->activities()->first()->subactivities(); $this->assertSame([ - ['name' => 'Biber', 'id' => 40], - ['name' => 'Wölfling', 'id' => 30], + ['id' => 40, 'name' => 'Biber'], + ['id' => 30, 'name' => 'Wölfling'], ], $subactivities->toArray()); Http::assertSentCount(3); } @@ -50,7 +51,7 @@ class PullActivitiesTest extends TestCase $this->expectException(NotSuccessfulException::class); app(SubactivityFake::class)->fetchFails(4, 'sorry dude'); - $subactivities = $this->login()->subactivitiesOf(4); + $this->login()->subactivitiesOf(Activity::from(['id' => 4])); } public function testContinueIfSubactivitiesRequestReturnsHtml(): void @@ -58,6 +59,6 @@ class PullActivitiesTest extends TestCase $this->expectException(NoJsonReceivedException::class); app(SubactivityFake::class)->fetchFailsWithoutJson(4); - $this->login()->subactivitiesOf(4); + $this->login()->subactivitiesOf(Activity::from(['id' => 4])); } }