1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\GoogleBooks; |
4
|
|
|
|
5
|
|
|
class Volume |
6
|
|
|
{ |
7
|
|
|
protected $client; |
8
|
|
|
protected $data; |
9
|
|
|
protected $full = false; |
10
|
|
|
|
11
|
|
|
protected static $coverSizes = [ |
12
|
|
|
"extraLarge", |
13
|
|
|
"large", |
14
|
|
|
"medium", |
15
|
|
|
"small", |
16
|
|
|
"thumbnail", |
17
|
|
|
"smallThumbnail", |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
public function __construct($client, $data, $full=false) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$this->client = $client; |
23
|
|
|
$this->data = $data; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns cover of size $preferredSize or smaller if a cover of size $preferredSize does not exist. |
28
|
|
|
* |
29
|
|
|
* @param string $preferredSize |
30
|
|
|
* @return mixed|null |
31
|
|
|
*/ |
32
|
|
|
public function getCover($preferredSize='extraLarge') |
33
|
|
|
{ |
34
|
|
|
$url = null; |
35
|
|
|
|
36
|
|
|
if (!isset($this->data->volumeInfo) || !isset($this->data->volumeInfo->imageLinks)) { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!$this->full && !in_array($preferredSize, ['thumbnail', 'smallThumbnail'])) { |
41
|
|
|
// Need to fetch the full record |
42
|
|
|
$this->data = $this->client->getItem('volumes/' . $this->id); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$idx = array_search($preferredSize, self::$coverSizes); |
46
|
|
|
if ($idx === false) { |
47
|
|
|
throw new \InvalidArgumentException('Invalid size: ' . $preferredSize); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
foreach (self::$coverSizes as $n => $size) { |
51
|
|
|
if ($n >= $idx && isset($this->data->volumeInfo->imageLinks->{$size})) { |
52
|
|
|
$url = $this->data->volumeInfo->imageLinks->{$size}; |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->removeCoverEdge($url); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function removeCoverEdge($url) { |
61
|
|
|
// Remove cover edge |
62
|
|
|
if (is_null($url)) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
return str_replace('&edge=curl', '&edge=none', $url); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function __get($key) |
69
|
|
|
{ |
70
|
|
|
if (isset($this->data->volumeInfo->{$key})) { |
71
|
|
|
return $this->data->volumeInfo->{$key}; |
72
|
|
|
} else if (isset($this->data->{$key})) { |
73
|
|
|
return $this->data->{$key}; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function __isset($key) |
78
|
|
|
{ |
79
|
|
|
return isset($this->data->volumeInfo->{$key}) || isset($this->data->{$key}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function __toString() |
83
|
|
|
{ |
84
|
|
|
return json_encode($this->data); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.