Skip to content

Commit

Permalink
Merge pull request #52 from bookboon/hotfix/thumbnailresolver
Browse files Browse the repository at this point in the history
Centralise thumbnailResolver
  • Loading branch information
lkm committed Feb 21, 2020
2 parents 68315d0 + dabc55b commit 528c66f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
13 changes: 1 addition & 12 deletions src/Entity/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
13 changes: 1 addition & 12 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 1 addition & 12 deletions src/Entity/Journey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 528c66f

Please sign in to comment.