Add types
This commit is contained in:
parent
07d1e67698
commit
3c3d0df50e
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Activity extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public static function fromNami($item)
|
||||
{
|
||||
$item = collect($item)
|
||||
->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);
|
||||
}
|
||||
}
|
37
src/Api.php
37
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<int, Subactivity>
|
||||
*/
|
||||
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<int, Group>
|
||||
*/
|
||||
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<int, Activity>
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami\Casters;
|
||||
|
||||
use Spatie\LaravelData\Casts\Cast;
|
||||
use Spatie\LaravelData\Support\DataProperty;
|
||||
|
||||
class GroupCast implements Cast
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $context
|
||||
*/
|
||||
public function cast(DataProperty $property, mixed $value, array $context): ?string
|
||||
{
|
||||
if ($property->type->isNullable && !$value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami\Data;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Zoomyboy\LaravelNami\Nami;
|
||||
|
||||
class Activity extends EnumData
|
||||
{
|
||||
/**
|
||||
* @return Collection<int, Subactivity>
|
||||
*/
|
||||
public function subactivities(): Collection
|
||||
{
|
||||
return Nami::subactivitiesOf($this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami\Data;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Zoomyboy\LaravelNami\Nami;
|
||||
|
||||
class Group extends EnumData
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
#[MapInputName('descriptor')]
|
||||
public string $name,
|
||||
public ?int $parentId,
|
||||
) {}
|
||||
|
||||
|
||||
/**
|
||||
* @return Collection<int, Activity>
|
||||
*/
|
||||
public function activities(): Collection
|
||||
{
|
||||
return Nami::activities($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, self>
|
||||
*/
|
||||
public function children(): Collection {
|
||||
return Nami::groups($this);
|
||||
}
|
||||
}
|
|
@ -49,4 +49,9 @@ class Membership extends Data
|
|||
'untergliederungId' => $this->subactivityId,
|
||||
];
|
||||
}
|
||||
|
||||
public function group(): Group {
|
||||
return Group::from(['name' => $this->group ?: '', 'id' => $this->groupId]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami\Data;
|
||||
|
||||
class Subactivity extends EnumData
|
||||
{
|
||||
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class Group implements Arrayable
|
||||
{
|
||||
public string $name;
|
||||
public int $id;
|
||||
public ?int $parentId;
|
||||
|
||||
public static function fromResponse(array $response, ?int $parent): self
|
||||
{
|
||||
return (new self())
|
||||
->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);
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Subactivity extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public static function fromNami($item)
|
||||
{
|
||||
$item = collect($item)
|
||||
->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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue