From 1b16d3726acb839e0eac04aa9ee064ce02a68b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Fri, 13 Sep 2024 10:45:23 +0200 Subject: [PATCH] Refactor code to use null instead of an empty array in getImage() method --- README.md | 2 +- src/Utils/TmdbImage.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fcb3493..d123165 100644 --- a/README.md +++ b/README.md @@ -417,7 +417,7 @@ $movie = Tmdb::client('API_KEY') ->movies() ->details(movie_id: 120); // ?\Kiwilan\Tmdb\Models\Movie -$poster_url = $movie->getPosterUrl(size: PosterSize::W500); // string|null (url to poster) +$poster_url = $movie->getPosterUrl(size: PosterSize::W500); // string|null (URL to poster) ``` These methods are available for `Poster`, `Backdrop`, `Logo`, `Profile` and `Still`. diff --git a/src/Utils/TmdbImage.php b/src/Utils/TmdbImage.php index d7286de..d81c072 100644 --- a/src/Utils/TmdbImage.php +++ b/src/Utils/TmdbImage.php @@ -32,11 +32,12 @@ public function getUrl(): string /** * Get the image content, as binary. */ - public function getImage(): string + public function getImage(): ?string { $url = $this->getUrl(); + $contents = file_get_contents($url); - return file_get_contents($url); + return $contents !== false ? $contents : null; } /** @@ -45,6 +46,9 @@ public function getImage(): string public function saveImage(string $path): bool { $contents = $this->getImage(); + if ($contents === null) { + return false; + } return file_put_contents($path, $contents) !== false; }