diff --git a/docs/03-supported-endpoints.md b/docs/03-supported-endpoints.md index 88d778f..54c1738 100644 --- a/docs/03-supported-endpoints.md +++ b/docs/03-supported-endpoints.md @@ -1678,22 +1678,17 @@ $response = $api->timezones()->getAll(); ### Types - [Official documentation](https://docs.sportmonks.com/football/v/core-api/endpoints/types) -- Cache default max age: `1 day` #### `getAll` ```php -getAll(int $page = 1, int $perPage = 25, string $order = 'asc'): TypeCollection +getAll(): TypeCollection ``` Get all types: ```php -$types = $sportMonksFootball->types()->getAll(); - -foreach ($types->getData() as $type) { - echo $type->getName(); -} +$response = $api->types()->getAll(); ``` #### `getById` @@ -1705,9 +1700,7 @@ getById(int $id): TypeItem Get type by id: ```php -$type = $sportMonksFootball->types()->getById(1); - -echo $type->getData()->getName(); +$response = $api->types()->getById(1); ``` #### `getAllByEntity` @@ -1719,15 +1712,7 @@ getAllByEntity(): TypeEntityCollection Get all types grouped by entity: ```php -$entities = $sportMonksFootball->types()->getAllByEntity(); - -foreach ($entities->getData() as $entity) { - echo $entity->getName(); - - foreach ($entity->getTypes() as $type) { - echo $type->getName() - } -} +$response = $api->types()->getAllByEntity(); ``` ## Pagination diff --git a/src/Endpoint/TypeEndpoint.php b/src/Endpoint/TypeEndpoint.php deleted file mode 100644 index ec5ad52..0000000 --- a/src/Endpoint/TypeEndpoint.php +++ /dev/null @@ -1,77 +0,0 @@ -validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: '/v3/core/types', - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new TypeCollection($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - */ - public function getById(int $id): TypeItem - { - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/core/types/{id}', [ - 'id' => $id - ]) - ); - - return new TypeItem($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - */ - public function getAllByEntity(): TypeEntityCollection - { - $response = $this->sendRequest( - method: 'GET', - path: '/v3/core/types/entities' - ); - - return new TypeEntityCollection($response); - } -} \ No newline at end of file diff --git a/src/Entity/TypeEntity.php b/src/Entity/TypeEntity.php index 8d27ed7..d6cf8f5 100644 --- a/src/Entity/TypeEntity.php +++ b/src/Entity/TypeEntity.php @@ -18,7 +18,7 @@ class TypeEntity public function __construct(array $data) { // "_key" index is injected in data to get the key from an associative array response - // Check the EntityTrait + // check the EntityTrait $this->name = $data['_key']; $this->updatedAt = new \DateTimeImmutable($data['updated_at']); $this->types = $this->createEntityCollection(Type::class, $data['types']); diff --git a/src/Resource/TypeResource.php b/src/Resource/TypeResource.php new file mode 100644 index 0000000..7811512 --- /dev/null +++ b/src/Resource/TypeResource.php @@ -0,0 +1,55 @@ +api->request( + method: 'GET', + path: '/v3/core/types' + ); + + return new TypeCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getById(int $id): TypeItem + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/core/types/{id}', [ + 'id' => $id + ]) + ); + + return new TypeItem($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getAllByEntity(): TypeEntityCollection + { + $data = $this->api->request( + method: 'GET', + path: '/v3/core/types/entities' + ); + + return new TypeEntityCollection($data); + } +} \ No newline at end of file diff --git a/src/SportMonksFootball.php b/src/SportMonksFootball.php index 70e50d4..71eaf3e 100644 --- a/src/SportMonksFootball.php +++ b/src/SportMonksFootball.php @@ -55,6 +55,7 @@ use ProgrammatorDev\SportMonksFootball\Resource\TopscorerResource; use ProgrammatorDev\SportMonksFootball\Resource\TransferResource; use ProgrammatorDev\SportMonksFootball\Resource\TvStationResource; +use ProgrammatorDev\SportMonksFootball\Resource\TypeResource; class SportMonksFootball extends Api { @@ -216,11 +217,11 @@ public function tvStations(): TvStationResource return new TvStationResource($this); } -// public function types(): TypeEndpoint -// { -// return new TypeEndpoint($this); -// } -// + public function types(): TypeResource + { + return new TypeResource($this); + } + // public function venues(): VenueEndpoint // { // return new VenueEndpoint($this); diff --git a/tests/Integration/SportMonksFootballTest.php b/tests/Integration/SportMonksFootballTest.php index cac3361..1609cea 100644 --- a/tests/Integration/SportMonksFootballTest.php +++ b/tests/Integration/SportMonksFootballTest.php @@ -31,6 +31,7 @@ use ProgrammatorDev\SportMonksFootball\Resource\TopscorerResource; use ProgrammatorDev\SportMonksFootball\Resource\TransferResource; use ProgrammatorDev\SportMonksFootball\Resource\TvStationResource; +use ProgrammatorDev\SportMonksFootball\Resource\TypeResource; use ProgrammatorDev\SportMonksFootball\Test\AbstractTest; class SportMonksFootballTest extends AbstractTest @@ -66,5 +67,6 @@ public function testMethods() $this->assertInstanceOf(TopscorerResource::class, $this->api->topscorers()); $this->assertInstanceOf(TransferResource::class, $this->api->transfers()); $this->assertInstanceOf(TvStationResource::class, $this->api->tvStations()); + $this->assertInstanceOf(TypeResource::class, $this->api->types()); } } \ No newline at end of file diff --git a/tests/Integration/TypeResourceTest.php b/tests/Integration/TypeResourceTest.php new file mode 100644 index 0000000..e2bd8b2 --- /dev/null +++ b/tests/Integration/TypeResourceTest.php @@ -0,0 +1,44 @@ + [ + TypeItem::class, + MockResponse::TYPE_ITEM_DATA, + 'types', + 'getById', + [1] + ]; + } + + public static function provideCollectionResponseData(): \Generator + { + yield 'get all' => [ + TypeCollection::class, + MockResponse::TYPE_COLLECTION_DATA, + 'types', + 'getAll' + ]; + yield 'get all by entity' => [ + TypeEntityCollection::class, + MockResponse::TYPE_ENTITY_COLLECTION_DATA, + 'types', + 'getAllByEntity' + ]; + } +} \ No newline at end of file diff --git a/tests/TypeEndpointTest_.php b/tests/TypeEndpointTest_.php deleted file mode 100644 index 98c3323..0000000 --- a/tests/TypeEndpointTest_.php +++ /dev/null @@ -1,67 +0,0 @@ - [ - MockResponse::TYPE_ITEM_DATA, - 'types', - 'getById', - [1], - 'assertTypeResponse' - ]; - } - - public static function provideEndpointCollectionResponseData(): \Generator - { - yield 'get all' => [ - MockResponse::TYPE_COLLECTION_DATA, - 'types', - 'getAll', - [], - 'assertTypeResponse' - ]; - yield 'get all by entity' => [ - MockResponse::TYPE_ENTITY_COLLECTION_DATA, - 'types', - 'getAllByEntity', - [], - 'assertEntityResponse' - ]; - } - - public static function provideEndpointInvalidPaginationData(): \Generator - { - yield 'get all' => ['types', 'getAll', []]; - } - - private function assertTypeResponse(Type $type): void - { - $this->assertSame(1, $type->getId()); - $this->assertSame('1st Half', $type->getName()); - $this->assertSame('1st-half', $type->getCode()); - $this->assertSame('1ST_HALF', $type->getDeveloperName()); - $this->assertSame('period', $type->getModelType()); - $this->assertSame(null, $type->getStatGroup()); - } - - private function assertEntityResponse(TypeEntity $entity): void - { - $this->assertSame('CoachStatisticDetail', $entity->getName()); - $this->assertSame('2023-09-21 16:20:39', $entity->getUpdatedAt()->format('Y-m-d H:i:s')); - $this->assertContainsOnlyInstancesOf(Type::class, $entity->getTypes()); - } -} \ No newline at end of file diff --git a/tests/Unit/TypeEntityTest.php b/tests/Unit/TypeEntityTest.php new file mode 100644 index 0000000..ebf3b46 --- /dev/null +++ b/tests/Unit/TypeEntityTest.php @@ -0,0 +1,32 @@ + 'PlayerStatisticDetail', + 'updated_at' => '2024-01-01 16:00:00', + 'types' => [ + [ + 'id' => 1, + 'name' => '1st Half', + 'code' => '1st-half', + 'developer_name' => '1ST_HALF', + 'model_type' => 'period', + 'stat_group' => 'overall' + ] + ] + ]); + + $this->assertSame('PlayerStatisticDetail', $entity->getName()); + $this->assertInstanceOf(\DateTimeImmutable::class, $entity->getUpdatedAt()); + $this->assertContainsOnlyInstancesOf(Type::class, $entity->getTypes()); + } +} diff --git a/tests/Unit/TypeTest.php b/tests/Unit/TypeTest.php new file mode 100644 index 0000000..d7181a2 --- /dev/null +++ b/tests/Unit/TypeTest.php @@ -0,0 +1,28 @@ + 1, + 'name' => '1st Half', + 'code' => '1st-half', + 'developer_name' => '1ST_HALF', + 'model_type' => 'period', + 'stat_group' => 'overall' + ]); + + $this->assertSame(1, $entity->getId()); + $this->assertSame('1st Half', $entity->getName()); + $this->assertSame('1st-half', $entity->getCode()); + $this->assertSame('1ST_HALF', $entity->getDeveloperName()); + $this->assertSame('period', $entity->getModelType()); + $this->assertSame('overall', $entity->getStatGroup()); + } +}