Skip to content

Commit

Permalink
Refactor code to use null instead of an empty array in getImage() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Sep 13, 2024
1 parent 1b16d37 commit c13feab
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 14 deletions.
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.0.33",
"version": "0.0.34",
"keywords": [
"php",
"tmdb",
Expand Down
10 changes: 7 additions & 3 deletions src/Models/TmdbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ protected function validateData(?array $data, string $key, Closure $closure): mi
return null;
}

protected function loopOn(mixed $values, string $class): ?array
protected function loopOn(mixed $values, string $class, bool $nullAsDefault = true): ?array
{
if (! $values) {
return null;
if ($nullAsDefault) {
return null;
} else {
return [];
}
}

$items = [];
Expand All @@ -49,7 +53,7 @@ protected function toBool(array $data, string $key, bool $default = false): bool
return $value ? boolval($value) : $default;
}

protected function toInt(array $data, string $key, ?int $default = null): ?int
protected function toInt(?array $data, string $key, ?int $default = null): ?int
{
$value = $data[$key] ?? null;

Expand Down
2 changes: 1 addition & 1 deletion src/Results/CollectionResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(array $data)
{
parent::__construct($data);

$this->results = $this->loopOn($data['results'] ?? null, \Kiwilan\Tmdb\Models\Collection::class);
$this->results = $this->loopOn($data['results'] ?? [], \Kiwilan\Tmdb\Models\Collection::class, false);
}

public function getFirstResult(): ?\Kiwilan\Tmdb\Models\Collection
Expand Down
2 changes: 1 addition & 1 deletion src/Results/MediaResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(array $data)
{
parent::__construct($data);

$this->results = $this->loopOn($data['results'] ?? null, \Kiwilan\Tmdb\Models\Media::class);
$this->results = $this->loopOn($data['results'] ?? [], \Kiwilan\Tmdb\Models\Media::class, false);
}

public function getFirstResult(): ?\Kiwilan\Tmdb\Models\Media
Expand Down
2 changes: 1 addition & 1 deletion src/Results/MovieDateResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(array $data)
parent::__construct($data);

$this->dates = new ResultsDates($data['dates'] ?? null);
$this->results = $this->loopOn($data['results'] ?? null, \Kiwilan\Tmdb\Models\Movie::class);
$this->results = $this->loopOn($data['results'] ?? [], \Kiwilan\Tmdb\Models\Movie::class, false);
}

public function getDates(): ?ResultsDates
Expand Down
6 changes: 3 additions & 3 deletions src/Results/MovieResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
class MovieResults extends Results
{
/** @var \Kiwilan\Tmdb\Models\Movie[] */
protected array $results = [];
protected ?array $results = [];

public function __construct(array $data)
public function __construct(?array $data)
{
parent::__construct($data);

$this->results = $this->loopOn($data['results'] ?? null, \Kiwilan\Tmdb\Models\Movie::class);
$this->results = $this->loopOn($data['results'] ?? [], \Kiwilan\Tmdb\Models\Movie::class, false);
}

public function getFirstResult(): ?\Kiwilan\Tmdb\Models\Movie
Expand Down
2 changes: 1 addition & 1 deletion src/Results/PeopleResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(array $data)
{
parent::__construct($data);

$this->results = $this->loopOn($data['results'] ?? null, \Kiwilan\Tmdb\Models\Credits\Person::class);
$this->results = $this->loopOn($data['results'] ?? [], \Kiwilan\Tmdb\Models\Credits\Person::class, false);
}

public function getFirstResult(): ?\Kiwilan\Tmdb\Models\Credits\Person
Expand Down
2 changes: 1 addition & 1 deletion src/Results/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class Results extends TmdbModel

protected int $total_results;

public function __construct(array $data)
public function __construct(?array $data)
{
$this->page = $this->toInt($data, 'page', 1);
$this->total_pages = $this->toInt($data, 'total_pages', 1);
Expand Down
4 changes: 2 additions & 2 deletions src/Results/TvSerieResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class TvSerieResults extends Results
/** @var \Kiwilan\Tmdb\Models\TvSeries[] */
protected array $results = [];

public function __construct(array $data)
public function __construct(?array $data)
{
parent::__construct($data);

$this->results = $this->loopOn($data['results'] ?? null, \Kiwilan\Tmdb\Models\TvSeries::class);
$this->results = $this->loopOn($data['results'] ?? [], \Kiwilan\Tmdb\Models\TvSeries::class, false);
}

public function getFirstResult(): ?\Kiwilan\Tmdb\Models\TvSeries
Expand Down
9 changes: 9 additions & 0 deletions tests/Movie/MovieSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@
expect($results->getFirstResult())->toBeInstanceOf(Movie::class);
expect($results->getFirstResult()->getTitle())->toBe("Le Seigneur des anneaux : La Communauté de l'anneau");
});

it('can have null movie results', function () {
$results = new MovieResults(null);

expect($results->getPage())->toBe(1);
expect($results->getTotalPages())->toBe(1);
expect($results->getTotalResults())->toBe(0);
expect($results->getResults())->toBeArray();
});
10 changes: 10 additions & 0 deletions tests/TvSeries/TvSeriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Kiwilan\Tmdb\Models\TvSeries\Episode;
use Kiwilan\Tmdb\Models\TvSeries\Network;
use Kiwilan\Tmdb\Models\TvSeries\Season;
use Kiwilan\Tmdb\Results\TvSerieResults;
use Kiwilan\Tmdb\Tmdb;

it('can get tv series details', function () {
Expand Down Expand Up @@ -170,3 +171,12 @@
expect($second->getId())->toBeInt();
expect($second->getPosterPath())->toBeString();
});

it('can have null tv series results', function () {
$results = new TvSerieResults(null);

expect($results->getPage())->toBe(1);
expect($results->getTotalPages())->toBe(1);
expect($results->getTotalResults())->toBe(0);
expect($results->getResults())->toBeArray();
});

0 comments on commit c13feab

Please sign in to comment.