From a53bbf52dba98caef54e2e122c1d8fbea0bd46a8 Mon Sep 17 00:00:00 2001 From: Lasse Mammen Date: Fri, 21 Feb 2020 17:12:05 +0000 Subject: [PATCH] Add getThumbnail on Author too --- src/Entity/Author.php | 27 +++++++++++++++++++++++++-- src/Entity/Book.php | 2 +- src/Entity/Journey.php | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Entity/Author.php b/src/Entity/Author.php index b28d0bd..603d0bf 100644 --- a/src/Entity/Author.php +++ b/src/Entity/Author.php @@ -79,10 +79,33 @@ protected function isValid(array $array) : bool /** * @return string - * + * @deprecated use getThumbnail */ public function getProfileImage() { - return $this->safeGet('profileImage', ''); + return $this->getThumbnail(380); + } + + /** + * Returns closes thumbnail size to input, default 210px. + * + * @param int $size appromimate size + * + * @return string url for thumbnail + */ + public function getThumbnail(int $size = 210) + { + $thumbs = []; + foreach ($this->safeGet('thumbnail', []) as $thumb) { + $thumbs[$thumb['width']] = $thumb['_link']; + } + + $sizes = array_keys($thumbs); + while (true) { + $thumbSize = array_shift($sizes); + if ((int) $size <= (int) $thumbSize || count($sizes) === 0) { + return $thumbs[$thumbSize]; + } + } } /** diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 69ecba4..3de7ff4 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -294,7 +294,7 @@ public function getIsbn() public function getThumbnail(int $size = 210) { $thumbs = []; - foreach ($this->safeGet('thumbnail') as $thumb) { + foreach ($this->safeGet('thumbnail', []) as $thumb) { $thumbs[$thumb['width']] = $thumb['_link']; } diff --git a/src/Entity/Journey.php b/src/Entity/Journey.php index 0d5400e..3d75f63 100644 --- a/src/Entity/Journey.php +++ b/src/Entity/Journey.php @@ -110,7 +110,7 @@ public function getPublished() : string public function getThumbnail(int $size = 210) { $thumbs = []; - foreach ($this->safeGet('thumbnail') as $thumb) { + foreach ($this->safeGet('thumbnail', []) as $thumb) { $thumbs[$thumb['width']] = $thumb['_link']; }