Skip to content

Commit

Permalink
feat: added season resource
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 22, 2024
1 parent 1d9f340 commit 3b9ba11
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 215 deletions.
26 changes: 6 additions & 20 deletions docs/03-supported-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,22 +753,17 @@ $response = $api->schedules()->getAllBySeasonIdAndTeamId(1, 1);
### Seasons

- [Official documentation](https://docs.sportmonks.com/football/endpoints-and-entities/endpoints/seasons)
- Cache default max age: `1 day`

#### `getAll`

```php
getAll(int $page = 1, int $perPage = 25, string $order = 'asc'): SeasonCollection
getAll(): SeasonCollection
```

Get all seasons:

```php
$seasons = $sportMonksFootball->seasons()->getAll();

foreach ($seasons->getData() as $season) {
echo $season->getName();
}
$response = $api->seasons()->getAll();
```

#### `getById`
Expand All @@ -780,8 +775,7 @@ getById(int $id): SeasonItem
Get season by id:

```php
$seasons = $sportMonksFootball->seasons()->getById(1);
echo $season->getData()->getName();
$response = $api->seasons()->getById(1);
```

#### `getAllByTeamId`
Expand All @@ -793,27 +787,19 @@ getAllByTeamId(int $teamId): SeasonCollection
Get all seasons by team id:

```php
$seasons = $sportMonksFootball->seasons()->getAllByTeamId(1);

foreach ($seasons->getData() as $season) {
echo $season->getName();
}
$response = $api->seasons()->getAllByTeamId(1);
```

#### `getAllBySearchQuery`

```php
getAllBySearchQuery(string $query, int $page = 1, int $perPage = 25, string $order = 'asc'): SeasonCollection
getAllBySearchQuery(string $query): SeasonCollection
```

Get all seasons by search query:

```php
$seasons = $sportMonksFootball->seasons()->getAllBySearchQuery('2023/2024');

foreach ($seasons->getData() as $season) {
echo $season->getName();
}
$response = $api->seasons()->getAllBySearchQuery('2024');
```

### Stages
Expand Down
114 changes: 0 additions & 114 deletions src/Endpoint/SeasonEndpoint.php

This file was deleted.

75 changes: 75 additions & 0 deletions src/Resource/SeasonResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Resource;

use ProgrammatorDev\SportMonksFootball\Entity\Response\SeasonCollection;
use ProgrammatorDev\SportMonksFootball\Entity\Response\SeasonItem;
use ProgrammatorDev\SportMonksFootball\Resource\Util\PaginationTrait;
use ProgrammatorDev\Validator\Exception\ValidationException;
use Psr\Http\Client\ClientExceptionInterface;

class SeasonResource extends Resource
{
use PaginationTrait;

/**
* @throws ClientExceptionInterface
*/
public function getAll(): SeasonCollection
{
$data = $this->api->request(
method: 'GET',
path: '/v3/football/seasons'
);

return new SeasonCollection($data);
}

/**
* @throws ClientExceptionInterface
*/
public function getById(int $id): SeasonItem
{
$data = $this->api->request(
method: 'GET',
path: $this->api->buildPath('/v3/football/seasons/{id}', [
'id' => $id
])
);

return new SeasonItem($data);
}

/**
* @throws ClientExceptionInterface
*/
public function getAllByTeamId(int $teamId): SeasonCollection
{
$data = $this->api->request(
method: 'GET',
path: $this->api->buildPath('/v3/football/seasons/teams/{teamId}', [
'teamId' => $teamId
])
);

return new SeasonCollection($data);
}

/**
* @throws ValidationException
* @throws ClientExceptionInterface
*/
public function getAllBySearchQuery(string $query): SeasonCollection
{
$this->validateQuery($query, 'query');

$data = $this->api->request(
method: 'GET',
path: $this->api->buildPath('/v3/football/seasons/search/{query}', [
'query' => $query
])
);

return new SeasonCollection($data);
}
}
11 changes: 6 additions & 5 deletions src/SportMonksFootball.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use ProgrammatorDev\SportMonksFootball\Resource\RivalResource;
use ProgrammatorDev\SportMonksFootball\Resource\RoundResource;
use ProgrammatorDev\SportMonksFootball\Resource\ScheduleResource;
use ProgrammatorDev\SportMonksFootball\Resource\SeasonResource;

class SportMonksFootball extends Api
{
Expand Down Expand Up @@ -150,11 +151,11 @@ public function schedules(): ScheduleResource
return new ScheduleResource($this);
}

// public function seasons(): SeasonEndpoint
// {
// return new SeasonEndpoint($this);
// }
//
public function seasons(): SeasonResource
{
return new SeasonResource($this);
}

// public function stages(): StageEndpoint
// {
// return new StageEndpoint($this);
Expand Down
62 changes: 62 additions & 0 deletions tests/Integration/SeasonResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Test\Integration;

use ProgrammatorDev\SportMonksFootball\Entity\Response\SeasonCollection;
use ProgrammatorDev\SportMonksFootball\Entity\Response\SeasonItem;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;
use ProgrammatorDev\SportMonksFootball\Test\MockResponse;
use ProgrammatorDev\SportMonksFootball\Test\Util\TestCollectionResponseTrait;
use ProgrammatorDev\SportMonksFootball\Test\Util\TestItemResponseTrait;
use ProgrammatorDev\SportMonksFootball\Test\Util\TestValidationExceptionTrait;

class SeasonResourceTest extends AbstractTest
{
use TestItemResponseTrait;
use TestCollectionResponseTrait;
use TestValidationExceptionTrait;

public static function provideItemResponseData(): \Generator
{
yield 'get by id' => [
SeasonItem::class,
MockResponse::SEASON_ITEM_DATA,
'seasons',
'getById',
[1]
];
}

public static function provideCollectionResponseData(): \Generator
{
yield 'get all' => [
SeasonCollection::class,
MockResponse::SEASON_COLLECTION_DATA,
'seasons',
'getAll'
];
yield 'get all by team id' => [
SeasonCollection::class,
MockResponse::SEASON_COLLECTION_DATA,
'seasons',
'getAllByTeamId',
[1]
];
yield 'get all by search query' => [
SeasonCollection::class,
MockResponse::SEASON_COLLECTION_DATA,
'seasons',
'getAllBySearchQuery',
['test']
];
}

public static function provideValidationExceptionData(): \Generator
{
yield 'get all by search query, blank query' => [
'seasons',
'getAllBySearchQuery',
['']
];
}
}
2 changes: 2 additions & 0 deletions tests/Integration/SportMonksFootballTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ProgrammatorDev\SportMonksFootball\Resource\RivalResource;
use ProgrammatorDev\SportMonksFootball\Resource\RoundResource;
use ProgrammatorDev\SportMonksFootball\Resource\ScheduleResource;
use ProgrammatorDev\SportMonksFootball\Resource\SeasonResource;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;

class SportMonksFootballTest extends AbstractTest
Expand All @@ -44,5 +45,6 @@ public function testMethods()
$this->assertInstanceOf(RivalResource::class, $this->api->rivals());
$this->assertInstanceOf(RoundResource::class, $this->api->rounds());
$this->assertInstanceOf(ScheduleResource::class, $this->api->schedules());
$this->assertInstanceOf(SeasonResource::class, $this->api->seasons());
}
}
Loading

0 comments on commit 3b9ba11

Please sign in to comment.