From cace46df51041a8cbe84a052f8077ea9e960d766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Mon, 20 May 2024 17:29:15 +0100 Subject: [PATCH] feat: added country resource --- docs/03-supported-endpoints.md | 20 ++--- src/Endpoint/CountryEndpoint.php | 98 --------------------- src/Resource/CountryResource.php | 60 +++++++++++++ src/SportMonksFootball.php | 11 +-- tests/CountryEndpointTest_.php | 69 --------------- tests/Integration/BookmakerResourceTest.php | 6 +- tests/Integration/CityResourceTest.php | 6 +- tests/Integration/CoachResourceTest.php | 6 +- tests/Integration/CountryResourceTest.php | 55 ++++++++++++ 9 files changed, 141 insertions(+), 190 deletions(-) delete mode 100644 src/Endpoint/CountryEndpoint.php create mode 100644 src/Resource/CountryResource.php delete mode 100644 tests/CountryEndpointTest_.php create mode 100644 tests/Integration/CountryResourceTest.php diff --git a/docs/03-supported-endpoints.md b/docs/03-supported-endpoints.md index ff19ba1..47e8220 100644 --- a/docs/03-supported-endpoints.md +++ b/docs/03-supported-endpoints.md @@ -1971,22 +1971,17 @@ $response = $api->continents()->getById(1); ### Countries - [Official documentation](https://docs.sportmonks.com/football/v/core-api/endpoints/countries) -- Cache default max age: `1 day` #### `getAll` ```php -getAll(int $page = 1, int $perPage = 25, string $order = 'asc'): CountryCollection +getAll(): CountryCollection ``` Get all countries: ```php -$countries = $sportMonksFootball->countries()->getAll(); - -foreach ($countries->getData() as $country) { - echo $country->getName(); -} +$response = $api->countries()->getAll(); ``` #### `getById` @@ -1998,24 +1993,19 @@ getById(int $id): CountryItem Get country by id: ```php -$country = $sportMonksFootball->countries()->getById(1); -echo $country->getData()->getName(); +$response = $api->countries()->getById(1); ``` #### `getAllBySearchQuery` ```php -getAllBySearchQuery(string $query, int $page = 1, int $perPage = 25, string $order = 'asc'): CountryCollection +getAllBySearchQuery(string $query): CountryCollection ``` Get all countries by search query: ```php -$countries = $sportMonksFootball->countries()->getAllBySearchQuery('portugal'); - -foreach ($countries->getData() as $country) { - echo $country->getName(); -} +$response = $api->countries()->getAllBySearchQuery('country'); ``` ### Filters diff --git a/src/Endpoint/CountryEndpoint.php b/src/Endpoint/CountryEndpoint.php deleted file mode 100644 index 3b49815..0000000 --- a/src/Endpoint/CountryEndpoint.php +++ /dev/null @@ -1,98 +0,0 @@ -validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: '/v3/core/countries', - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new CountryCollection($response); - } - - /** - * @throws Exception - * @throws ApiErrorException - */ - public function getById(int $id): CountryItem - { - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/core/countries/{id}', [ - 'id' => $id - ]) - ); - - return new CountryItem($response); - } - - /** - * @throws Exception - * @throws ValidationException - * @throws ApiErrorException - */ - public function getAllBySearchQuery( - string $query, - int $page = 1, - int $perPage = Pagination::PER_PAGE, - string $order = Pagination::ORDER_ASC - ): CountryCollection - { - $this->validateSearchQuery($query); - $this->validatePagination($page, $perPage, $order); - - $response = $this->sendRequest( - method: 'GET', - path: $this->formatPath('/v3/core/countries/search/{query}', [ - 'query' => $query - ]), - query: [ - 'page' => $page, - 'per_page' => $perPage, - 'order' => $order - ] - ); - - return new CountryCollection($response); - } -} \ No newline at end of file diff --git a/src/Resource/CountryResource.php b/src/Resource/CountryResource.php new file mode 100644 index 0000000..702740d --- /dev/null +++ b/src/Resource/CountryResource.php @@ -0,0 +1,60 @@ +api->request( + method: 'GET', + path: '/v3/core/countries' + ); + + return new CountryCollection($data); + } + + /** + * @throws ClientExceptionInterface + */ + public function getById(int $id): CountryItem + { + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/core/countries/{id}', [ + 'id' => $id + ]) + ); + + return new CountryItem($data); + } + + /** + * @throws ValidationException + * @throws ClientExceptionInterface + */ + public function getAllBySearchQuery(string $query): CountryCollection + { + $this->validateQuery($query, 'query'); + + $data = $this->api->request( + method: 'GET', + path: $this->api->buildPath('/v3/core/countries/search/{query}', [ + 'query' => $query + ]) + ); + + return new CountryCollection($data); + } +} \ No newline at end of file diff --git a/src/SportMonksFootball.php b/src/SportMonksFootball.php index 75975b0..dead5de 100644 --- a/src/SportMonksFootball.php +++ b/src/SportMonksFootball.php @@ -32,6 +32,7 @@ use ProgrammatorDev\SportMonksFootball\Resource\CoachResource; use ProgrammatorDev\SportMonksFootball\Resource\CommentaryResource; use ProgrammatorDev\SportMonksFootball\Resource\ContinentResource; +use ProgrammatorDev\SportMonksFootball\Resource\CountryResource; class SportMonksFootball extends Api { @@ -73,11 +74,11 @@ public function continents(): ContinentResource return new ContinentResource($this); } -// public function countries(): CountryEndpoint -// { -// return new CountryEndpoint($this); -// } -// + public function countries(): CountryResource + { + return new CountryResource($this); + } + // public function filters(): FilterEndpoint // { // return new FilterEndpoint($this); diff --git a/tests/CountryEndpointTest_.php b/tests/CountryEndpointTest_.php deleted file mode 100644 index bd8e0cc..0000000 --- a/tests/CountryEndpointTest_.php +++ /dev/null @@ -1,69 +0,0 @@ - [ - MockResponse::COUNTRY_ITEM_DATA, - 'countries', - 'getById', - [1] - ]; - } - - public static function provideEndpointCollectionResponseData(): \Generator - { - yield 'get all' => [ - MockResponse::COUNTRY_COLLECTION_DATA, - 'countries', - 'getAll', - [] - ]; - yield 'get all by search query' => [ - MockResponse::COUNTRY_COLLECTION_DATA, - 'countries', - 'getAllBySearchQuery', - ['test'] - ]; - } - - public static function provideEndpointInvalidPaginationData(): \Generator - { - yield 'get all' => ['countries', 'getAll', []]; - yield 'get all by search query' => ['countries', 'getAllBySearchQuery', ['test']]; - } - - public static function provideEndpointInvalidSearchQueryData(): \Generator - { - yield 'get all by search query' => ['countries', 'getAllBySearchQuery']; - } - - private function assertResponse(Country $country): void - { - $this->assertSame(2, $country->getId()); - $this->assertSame(1, $country->getContinentId()); - $this->assertSame('Poland', $country->getName()); - $this->assertSame('Republic of Poland', $country->getOfficialName()); - $this->assertSame('POL', $country->getFifaName()); - $this->assertSame('PL', $country->getIso2()); - $this->assertSame('POL', $country->getIso3()); - $this->assertSame(52.147850036621094, $country->getLatitude()); - $this->assertSame(19.37775993347168, $country->getLongitude()); - $this->assertSame(["BLR","CZE","DEU","LTU","RUS","SVK","UKR"], $country->getBorders()); - $this->assertSame('https://cdn.sportmonks.com/images/countries/png/short/pl.png', $country->getImagePath()); - } -} \ No newline at end of file diff --git a/tests/Integration/BookmakerResourceTest.php b/tests/Integration/BookmakerResourceTest.php index 6f4ae23..cc86316 100644 --- a/tests/Integration/BookmakerResourceTest.php +++ b/tests/Integration/BookmakerResourceTest.php @@ -53,6 +53,10 @@ public static function provideCollectionResponseData(): \Generator public static function provideValidationExceptionData(): \Generator { - yield 'get all by search query, blank query' => ['bookmakers', 'getAllBySearchQuery', ['']]; + yield 'get all by search query, blank query' => [ + 'bookmakers', + 'getAllBySearchQuery', + [''] + ]; } } \ No newline at end of file diff --git a/tests/Integration/CityResourceTest.php b/tests/Integration/CityResourceTest.php index adfb55e..19fb9b4 100644 --- a/tests/Integration/CityResourceTest.php +++ b/tests/Integration/CityResourceTest.php @@ -46,6 +46,10 @@ public static function provideCollectionResponseData(): \Generator public static function provideValidationExceptionData(): \Generator { - yield 'get all by search query, blank query' => ['cities', 'getAllBySearchQuery', ['']]; + yield 'get all by search query, blank query' => [ + 'cities', + 'getAllBySearchQuery', + [''] + ]; } } \ No newline at end of file diff --git a/tests/Integration/CoachResourceTest.php b/tests/Integration/CoachResourceTest.php index 75a8530..6629046 100644 --- a/tests/Integration/CoachResourceTest.php +++ b/tests/Integration/CoachResourceTest.php @@ -59,6 +59,10 @@ public static function provideCollectionResponseData(): \Generator public static function provideValidationExceptionData(): \Generator { - yield 'get all by search query, blank query' => ['coaches', 'getAllBySearchQuery', ['']]; + yield 'get all by search query, blank query' => [ + 'coaches', + 'getAllBySearchQuery', + [''] + ]; } } \ No newline at end of file diff --git a/tests/Integration/CountryResourceTest.php b/tests/Integration/CountryResourceTest.php new file mode 100644 index 0000000..4d393e2 --- /dev/null +++ b/tests/Integration/CountryResourceTest.php @@ -0,0 +1,55 @@ + [ + CountryItem::class, + MockResponse::COUNTRY_ITEM_DATA, + 'countries', + 'getById', + [1] + ]; + } + + public static function provideCollectionResponseData(): \Generator + { + yield 'get all' => [ + CountryCollection::class, + MockResponse::COUNTRY_COLLECTION_DATA, + 'countries', + 'getAll' + ]; + yield 'get all by search query' => [ + CountryCollection::class, + MockResponse::COUNTRY_COLLECTION_DATA, + 'countries', + 'getAllBySearchQuery', + ['test'] + ]; + } + + public static function provideValidationExceptionData(): \Generator + { + yield 'get all by search query, blank query' => [ + 'countries', + 'getAllBySearchQuery', + [''] + ]; + } +} \ No newline at end of file