Skip to content

Commit

Permalink
Add raw() method to execute raw request to TMDB API (for non implem…
Browse files Browse the repository at this point in the history
…ented methods)
  • Loading branch information
ewilan-riviere committed Sep 24, 2024
1 parent e3ec58d commit 1f7fe9c
Show file tree
Hide file tree
Showing 10 changed files with 1,686 additions and 10 deletions.
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ $collection = Tmdb::client('API_KEY')
->details(collection_id: 119); // ?\Kiwilan\Tmdb\Models\TmdbCollection
```

#### [Collection: Images](https://developer.themoviedb.org/reference/collection-images)

Get the images that belong to a collection.

```php
$images = Tmdb::client('API_KEY')
->collections()
->images(collection_id: 119); // ?\Kiwilan\Tmdb\Models\TmdbCollectionImages
```

#### [Collection: Translations](https://developer.themoviedb.org/reference/collection-translations)

Get the translations that belong to a collection.

```php
$translations = Tmdb::client('API_KEY')
->collections()
->translations(collection_id: 119); // ?\Kiwilan\Tmdb\Models\TmdbCollectionTranslations
```

### Companies

#### [Companies: Details](https://developer.themoviedb.org/reference/company-details)
Expand Down Expand Up @@ -480,6 +500,23 @@ $raw_data = $movie->getRawData(); // array
$raw_title_key = $movie->getRawDataKey('title'); // mixed
```

### Send raw request

If you want to send a raw request to TMDB API, you can use `raw()` method, API key will be added automatically.

```php
use Kiwilan\Tmdb\Tmdb;

$response = Tmdb::client(apiKey())
->raw()
->url('/movie/now_playing', ['language' => 'en-US', 'page' => 1]); // ?Kiwi\Tmdb\Repositories\RawRepository

$response->isSuccess(); // bool
$response->getStatusCode(); // int
$response->getBody(); // array
$response->getUrl(); // string
```

## Testing

```bash
Expand All @@ -501,10 +538,10 @@ A fix? A new feature? A typo? You're welcome to contribute to this project. Just
- [ ] [Movie List](https://developer.themoviedb.org/reference/changes-movie-list)
- [ ] [People List](https://developer.themoviedb.org/reference/changes-people-list)
- [ ] [TV List](https://developer.themoviedb.org/reference/changes-tv-list)
- [ ] Collections
- [x] ~~Collections~~
- [x] ~~[Details](https://developer.themoviedb.org/reference/collection-details)~~
- [ ] [Images](https://developer.themoviedb.org/reference/collection-images)
- [ ] [Translations](https://developer.themoviedb.org/reference/collection-translations)
- [x] ~~[Images](https://developer.themoviedb.org/reference/collection-images)~~
- [x] ~~[Translations](https://developer.themoviedb.org/reference/collection-translations)~~
- [ ] Companies
- [x] ~~[Details](https://developer.themoviedb.org/reference/company-details)~~
- [ ] [Alternative Names](https://developer.themoviedb.org/reference/company-alternative-names)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kiwilan/php-tmdb",
"description": "PHP wrapper package to interact with the The Movie Database (TMDB) API.",
"version": "0.1.04",
"version": "0.1.05",
"keywords": [
"php",
"tmdb",
Expand Down
30 changes: 30 additions & 0 deletions src/Repositories/RawRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Kiwilan\Tmdb\Repositories;

/**
* Raw repository, used to send raw requests to the API.
*
* @docs https://developer.themoviedb.org/reference/intro/getting-started
*/
class RawRepository extends Repository
{
/**
* Execute a request on the API, the API key will be associated with the request.
*
* @param string $url The URL to send the request, like `/movie/now_playing`
* @param string[] $params The query parameters, like `['language' => 'en-US', 'page' => 1]`
*
* @docs https://developer.themoviedb.org/reference/intro/getting-started
*/
public function url(string $url, ?array $params = null): ?RawRepository
{
if (! $params) {
$params = [];
}

$this->get($url, $params);

return $this->isSuccess ? $this : null;
}
}
27 changes: 26 additions & 1 deletion src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ abstract class Repository

protected ?array $body = null;

protected ?int $statusCode = null;

protected bool $isSuccess = false;

protected string $language = 'en-US';
Expand All @@ -18,6 +20,27 @@ public function __construct(
private string $apiKey,
) {}

/**
* Get URL used in the request
*/
public function getUrl(): ?string
{
return $this->url;
}

/**
* Get the body of the response
*/
public function getBody(): ?array
{
return $this->body;
}

public function isSuccess(): bool
{
return $this->isSuccess;
}

/**
* Merge base URL with the path and execute the request
*
Expand Down Expand Up @@ -69,7 +92,9 @@ protected function execute(): ?array
'http_errors' => false,
]);

if ($response->getStatusCode() !== 200) {
$this->statusCode = $response->getStatusCode();

if ($this->statusCode !== 200) {
return null;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Tmdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kiwilan\Tmdb;

use Kiwilan\Tmdb\Repositories\Repository;

class Tmdb
{
protected function __construct(
Expand Down Expand Up @@ -148,4 +150,14 @@ public function tvEpisodes(): Repositories\TVEpisodesRepository
{
return new Repositories\TVEpisodesRepository($this->apiKey);
}

/**
* Use Raw repository, used to send raw requests to the API (for methods not implemented).
*
* @docs https://developer.themoviedb.org/reference/intro/getting-started
*/
public function raw(): ?Repositories\RawRepository
{
return new Repositories\RawRepository($this->apiKey);
}
}
72 changes: 72 additions & 0 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

use Kiwilan\Tmdb\Enums\TmdbImageType;
use Kiwilan\Tmdb\Models\Images\TmdbImage;
use Kiwilan\Tmdb\Models\Images\TmdbImages;
use Kiwilan\Tmdb\Models\TmdbCollection;
use Kiwilan\Tmdb\Models\TmdbMovie;
use Kiwilan\Tmdb\Models\Translations\TmdbTranslation;
use Kiwilan\Tmdb\Models\Translations\TmdbTranslations;
use Kiwilan\Tmdb\Query\SearchCollectionQuery;
use Kiwilan\Tmdb\Results\CollectionResults;
use Kiwilan\Tmdb\Tmdb;
Expand Down Expand Up @@ -83,3 +88,70 @@
expect($collection->getParts())->not()->toBeEmpty();
expect($collection->getParts())->each(fn (Pest\Expectation $part) => expect($part->value)->toBeInstanceOf(TmdbMovie::class));
});

it('can get collection images', function () {
$images = Tmdb::client(apiKey())
->collections()
->images(collection_id: 119);

expect($images)->not()->toBeNull();
expect($images)->toBeInstanceOf(TmdbImages::class);
expect($images->getBackdrops())->toBeArray();
expect($images->getId())->toBeInt();

$backdrops = $images->getBackdrops();
expect($backdrops)->toBeArray();
expect($backdrops)->not()->toBeEmpty();
expect($backdrops)->each(fn (Pest\Expectation $backdrop) => expect($backdrop->value)->toBeInstanceOf(TmdbImage::class));

$first = $backdrops[0];
expect($first->getAspectRatio())->toBeFloat();
expect($first->getHeight())->toBeInt();
if ($first->getIso6391()) {
expect($first->getIso6391())->toBeString();
}
expect($first->getFilePath())->toBeString();
expect($first->getVoteAverage())->toBeFloat();
expect($first->getVoteCount())->toBeInt();
expect($first->getWidth())->toBeInt();
expect($first->getType())->toBe(TmdbImageType::BACKDROP);

$posters = $images->getPosters();
expect($posters)->toBeArray();
expect($posters)->not()->toBeEmpty();
expect($posters)->each(fn (Pest\Expectation $poster) => expect($poster->value)->toBeInstanceOf(TmdbImage::class));

$first = $posters[0];
expect($first->getAspectRatio())->toBeFloat();
expect($first->getHeight())->toBeInt();
if ($first->getIso6391()) {
expect($first->getIso6391())->toBeString();
}
expect($first->getFilePath())->toBeString();
expect($first->getVoteAverage())->toBeFloat();
expect($first->getVoteCount())->toBeInt();
expect($first->getWidth())->toBeInt();
expect($first->getType())->toBe(TmdbImageType::POSTER);
});

it('can get collection translations', function () {
$translations = Tmdb::client(apiKey())
->collections()
->translations(collection_id: 119);

expect($translations)->not()->toBeNull();
expect($translations)->toBeInstanceOf(TmdbTranslations::class);
expect($translations->getId())->toBe(119);

$translations = $translations->getTranslations();
expect($translations)->toBeArray();
expect($translations)->not()->toBeEmpty();
expect($translations)->each(fn (Pest\Expectation $translation) => expect($translation->value)->toBeInstanceOf(TmdbTranslation::class));

$first = $translations[0];
expect($first->getIso31661())->toBeString();
expect($first->getIso6391())->toBeString();
expect($first->getName())->toBeString();
expect($first->getEnglishName())->toBeString();
expect($first->getData())->toBeArray();
});
Loading

0 comments on commit 1f7fe9c

Please sign in to comment.