From dabc55ba467f31d4274fc339682fe767dcaa49b7 Mon Sep 17 00:00:00 2001 From: Lasse Mammen Date: Fri, 21 Feb 2020 17:22:00 +0000 Subject: [PATCH] Centralise thumbnailResolver --- src/Entity/Author.php | 13 +------------ src/Entity/Book.php | 13 +------------ src/Entity/Entity.php | 20 ++++++++++++++++++++ src/Entity/Journey.php | 13 +------------ 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/Entity/Author.php b/src/Entity/Author.php index 603d0bf..1d35e08 100644 --- a/src/Entity/Author.php +++ b/src/Entity/Author.php @@ -94,18 +94,7 @@ public function getProfileImage() { */ 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]; - } - } + return $this->thumbnailResolver($this->safeGet('thumbnail', []), $size); } /** diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 3de7ff4..f74817d 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -293,18 +293,7 @@ public function getIsbn() */ 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]; - } - } + return $this->thumbnailResolver($this->safeGet('thumbnail', []), $size); } /** diff --git a/src/Entity/Entity.php b/src/Entity/Entity.php index bb44fca..a849aee 100644 --- a/src/Entity/Entity.php +++ b/src/Entity/Entity.php @@ -106,6 +106,26 @@ public static function isValidUUID(string $uuid) : bool return preg_match('/^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$/', $uuid) == true; } + protected function thumbnailResolver($thumbnails, $size) : string + { + if (false === is_array($thumbnails) || count($thumbnails) === 0) { + return ''; + } + + $thumbs = []; + foreach ($thumbnails 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]; + } + } + } + public function jsonSerialize() { return $this->getData(); diff --git a/src/Entity/Journey.php b/src/Entity/Journey.php index 3d75f63..c8678d5 100644 --- a/src/Entity/Journey.php +++ b/src/Entity/Journey.php @@ -109,18 +109,7 @@ public function getPublished() : string */ 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]; - } - } + return $this->thumbnailResolver($this->safeGet('thumbnail', []), $size); } /**