1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\GoogleBooks; |
4
|
|
|
|
5
|
|
|
class Volume extends Model |
6
|
|
|
{ |
7
|
|
|
protected static $coverSizes = [ |
8
|
|
|
'extraLarge', |
9
|
|
|
'large', |
10
|
|
|
'medium', |
11
|
|
|
'small', |
12
|
|
|
'thumbnail', |
13
|
|
|
'smallThumbnail', |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Provide a shortcut to the data in 'volumeInfo', so we can do e.g. |
18
|
|
|
* `$volume->title` instead of `$volume->volumeInfo->title`. |
19
|
|
|
* |
20
|
|
|
* @param string $key |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
|
|
public function __get($key) |
24
|
|
|
{ |
25
|
|
|
return $this->get("volumeInfo.$key") ?: $this->get($key); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the URL to the largest available cover, but no larger than $preferredSize. |
30
|
|
|
* |
31
|
|
|
* @param string $preferredSize |
32
|
|
|
* @return string|null |
33
|
|
|
*/ |
34
|
|
|
public function getCover($preferredSize = 'extraLarge') |
35
|
|
|
{ |
36
|
|
|
$url = null; |
37
|
|
|
|
38
|
|
|
if (!$this->has('volumeInfo.imageLinks')) { |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($this->isSearchResult() && !in_array($preferredSize, ['thumbnail', 'smallThumbnail'])) { |
43
|
|
|
// The brief record we get from search results only contains the sizes 'smallThumbnail' |
44
|
|
|
// and 'thumbnail'. To get larger sizes, we need the full record. |
45
|
|
|
$this->expandToFullRecord(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$idx = array_search($preferredSize, self::$coverSizes); |
49
|
|
|
if ($idx === false) { |
50
|
|
|
throw new \InvalidArgumentException('Invalid size: ' . $preferredSize); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach (self::$coverSizes as $n => $size) { |
54
|
|
|
if ($n >= $idx && $this->has("volumeInfo.imageLinks.{$size}")) { |
55
|
|
|
$url = $this->get("volumeInfo.imageLinks.{$size}"); |
56
|
|
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->removeCoverEdge($url); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Modify the cover URL to remove the cover edge. |
65
|
|
|
* |
66
|
|
|
* @param string $url |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected function removeCoverEdge($url) |
70
|
|
|
{ |
71
|
|
|
if (is_null($url)) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
return str_replace('&edge=curl', '&edge=none', $url); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|