Skip to content

Commit

Permalink
feat: added type resource
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 22, 2024
1 parent ca1212b commit 5972081
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 169 deletions.
23 changes: 4 additions & 19 deletions docs/03-supported-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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
Expand Down
77 changes: 0 additions & 77 deletions src/Endpoint/TypeEndpoint.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Entity/TypeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
55 changes: 55 additions & 0 deletions src/Resource/TypeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Resource;

use ProgrammatorDev\SportMonksFootball\Entity\Response\TypeCollection;
use ProgrammatorDev\SportMonksFootball\Entity\Response\TypeEntityCollection;
use ProgrammatorDev\SportMonksFootball\Entity\Response\TypeItem;
use ProgrammatorDev\SportMonksFootball\Resource\Util\PaginationTrait;
use Psr\Http\Client\ClientExceptionInterface;

class TypeResource extends Resource
{
use PaginationTrait;

/**
* @throws ClientExceptionInterface
*/
public function getAll(): TypeCollection
{
$data = $this->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);
}
}
11 changes: 6 additions & 5 deletions src/SportMonksFootball.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/SportMonksFootballTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
44 changes: 44 additions & 0 deletions tests/Integration/TypeResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Test\Integration;

use ProgrammatorDev\SportMonksFootball\Entity\Response\TypeCollection;
use ProgrammatorDev\SportMonksFootball\Entity\Response\TypeEntityCollection;
use ProgrammatorDev\SportMonksFootball\Entity\Response\TypeItem;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;
use ProgrammatorDev\SportMonksFootball\Test\MockResponse;
use ProgrammatorDev\SportMonksFootball\Test\Util\TestCollectionResponseTrait;
use ProgrammatorDev\SportMonksFootball\Test\Util\TestItemResponseTrait;

class TypeResourceTest extends AbstractTest
{
use TestItemResponseTrait;
use TestCollectionResponseTrait;

public static function provideItemResponseData(): \Generator
{
yield 'get by id' => [
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'
];
}
}
67 changes: 0 additions & 67 deletions tests/TypeEndpointTest_.php

This file was deleted.

32 changes: 32 additions & 0 deletions tests/Unit/TypeEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Test\Unit;

use ProgrammatorDev\SportMonksFootball\Entity\Type;
use ProgrammatorDev\SportMonksFootball\Entity\TypeEntity;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;

class TypeEntityTest extends AbstractTest
{
public function testMethods(): void
{
$entity = new TypeEntity([
'_key' => '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());
}
}
Loading

0 comments on commit 5972081

Please sign in to comment.