Skip to content

Commit

Permalink
feat: added player resource
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 21, 2024
1 parent 2d26cf9 commit 8fba33a
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 273 deletions.
38 changes: 8 additions & 30 deletions docs/03-supported-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,23 +505,17 @@ $response = $api->livescores()->getAllLastUpdated();
### Players

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

#### `getAll`

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

Get all players:

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

foreach ($players->getData() as $player) {
echo $player->getDisplayName();
}
$response = $api->players()->getAll();
```

#### `getById`
Expand All @@ -533,40 +527,31 @@ getById(int $id): PlayerItem
Get player by id:

```php
$player = $sportMonksFootball->players()->getById(1);
echo $player->getData()->getDisplayName();
$response = $api->players()->getById(1);
```

#### `getAllByCountryId`

```php
getAllByCountryId(int $countryId, int $page = 1, int $perPage = 25, string $order = 'asc'): PlayerCollection
getAllByCountryId(int $countryId): PlayerCollection
```

Get all players by country id:

```php
$players = $sportMonksFootball->players()->getAllByCountryId(1);

foreach ($players->getData() as $player) {
echo $player->getDisplayName();
}
$response = $api->players()->getAllByCountryId(1);
```

#### `getAllBySearchQuery`

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

Get all players by search query:

```php
$players = $sportMonksFootball->players()->getAllBySearchQuery('esgaio');

foreach ($players->getData() as $player) {
echo $player->getDisplayName();
}
$response = $api->players()->getAllBySearchQuery('esgaio');
```

#### `getAllLastUpdated`
Expand All @@ -578,16 +563,9 @@ getAllLastUpdated(): PlayerCollection
Get all players that received updates in the last couple of hours:

```php
$players = $sportMonksFootball->players()->getAllLastUpdated();

foreach ($players->getData() as $player) {
echo $player->getDisplayName();
}
$response = $api->players()->getAllLastUpdated();
```

> **Note**
> Cache max age is forced to `1 hour` on this endpoint.
### Referees

- [Official documentation](https://docs.sportmonks.com/football/endpoints-and-entities/endpoints/referees)
Expand Down
2 changes: 1 addition & 1 deletion docs/05-entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@
- `getImagePath()`: `?string`
- `getHeight()`: `?int`
- `getWeight()`: `?int`
- `getDateOfBirth()`: `?\DateTimeImmutable`
- `getBirthdate()`: `?\DateTimeImmutable`
- `getGender()`: `?string`
- `inSquad()`: `?bool`
- `getSport()`: [`?Sport`](#sport) (`sport` include is required)
Expand Down
144 changes: 0 additions & 144 deletions src/Endpoint/PlayerEndpoint.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/Entity/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Player

private ?int $weight;

private ?\DateTimeImmutable $dateOfBirth;
private ?\DateTimeImmutable $birthdate;

private ?string $gender;

Expand Down Expand Up @@ -102,7 +102,7 @@ public function __construct(array $data, string $timezone)
$this->imagePath = $data['image_path'] ?? null;
$this->height = $data['height'] ?? null;
$this->weight = $data['weight'] ?? null;
$this->dateOfBirth = isset($data['date_of_birth']) ? new \DateTimeImmutable($data['date_of_birth']) : null;
$this->birthdate = isset($data['date_of_birth']) ? new \DateTimeImmutable($data['date_of_birth']) : null;
$this->gender = $data['gender'] ?? null;
$this->inSquad = $data['in_squad'] ?? null;

Expand Down Expand Up @@ -203,9 +203,9 @@ public function getWeight(): ?int
return $this->weight;
}

public function getDateOfBirth(): ?\DateTimeImmutable
public function getBirthdate(): ?\DateTimeImmutable
{
return $this->dateOfBirth;
return $this->birthdate;
}

public function getGender(): ?string
Expand Down
88 changes: 88 additions & 0 deletions src/Resource/PlayerResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Resource;

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

class PlayerResource extends Resource
{
use PaginationTrait;

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

return new PlayerCollection($data);
}

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

return new PlayerItem($data);
}

/**
* @throws ClientExceptionInterface
*/
public function getAllByCountryId(int $countryId): PlayerCollection
{
$data = $this->api->request(
method: 'GET',
path: $this->api->buildPath('/v3/football/players/countries/{countryId}', [
'countryId' => $countryId
])
);

return new PlayerCollection($data);
}

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

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

return new PlayerCollection($data);
}

/**
* @throws ClientExceptionInterface
*/
public function getAllLastUpdated(): PlayerCollection
{
$data = $this->api->request(
method: 'GET',
path: '/v3/football/players/latest'
);

return new PlayerCollection($data);
}
}
11 changes: 6 additions & 5 deletions src/SportMonksFootball.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use ProgrammatorDev\SportMonksFootball\Resource\LeagueResource;
use ProgrammatorDev\SportMonksFootball\Resource\LivescoreResource;
use ProgrammatorDev\SportMonksFootball\Resource\MarketResource;
use ProgrammatorDev\SportMonksFootball\Resource\PlayerResource;

class SportMonksFootball extends Api
{
Expand Down Expand Up @@ -109,11 +110,11 @@ public function markets(): MarketResource
return new MarketResource($this);
}

// public function players(): PlayerEndpoint
// {
// return new PlayerEndpoint($this);
// }
//
public function players(): PlayerResource
{
return new PlayerResource($this);
}

// public function preMatchOdds(): PreMatchOddEndpoint
// {
// return new PreMatchOddEndpoint($this);
Expand Down
Loading

0 comments on commit 8fba33a

Please sign in to comment.