1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\GoogleBooks; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Scriptotek\GoogleBooks\GoogleBooks; |
7
|
|
|
|
8
|
|
|
class LibraryBuilder |
9
|
|
|
{ |
10
|
|
|
protected $data; |
11
|
|
|
protected $client; |
12
|
|
|
protected $chunk; |
13
|
|
|
protected $query; |
14
|
|
|
protected $limit; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Accepts a GoogleBooks dependency and sets it as a client property |
18
|
|
|
* |
19
|
|
|
* @param GoogleBooks $GoogleBooks |
20
|
|
|
*/ |
21
|
|
|
public function __construct(GoogleBooks $GoogleBooks) |
22
|
|
|
{ |
23
|
|
|
$this->client = $GoogleBooks; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Limits the results. May be an integer between 1 and 40 |
28
|
|
|
* |
29
|
|
|
* @param int $limit |
30
|
|
|
* @return self |
31
|
|
|
*/ |
32
|
|
|
public function limit($limit) |
33
|
|
|
{ |
34
|
|
|
$this->limit = $limit; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets the query property |
40
|
|
|
* |
41
|
|
|
* @param string $query |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
|
|
public function search($query) |
45
|
|
|
{ |
46
|
|
|
$this->query = $query; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Fetches results from the Google API |
52
|
|
|
* |
53
|
|
|
* @param string $id |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private function fetch($id = false) |
57
|
|
|
{ |
58
|
|
|
if ($this->limit <= 40) { |
59
|
|
|
if ($id !== false) { |
60
|
|
|
return $this->client->getItem("volumes/$id"); |
61
|
|
|
} |
62
|
|
|
return $this->client->raw('volumes', $this->getParams()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->fetchAll(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Performs multiple searches in order to overcome Googles max of 40 results per request |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
private function fetchAll() |
74
|
|
|
{ |
75
|
|
|
return $this->data = $this->client->listItems('volumes', ['q' => $this->query]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Finds and returns one result. Should accept a book ID (found on API responses) |
80
|
|
|
* |
81
|
|
|
* @param string $id |
82
|
|
|
* @return Scriptotek\GoogleBooks\Volume |
83
|
|
|
*/ |
84
|
|
|
public function find($id) |
85
|
|
|
{ |
86
|
|
|
return $this->fetch($id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieves the first result |
91
|
|
|
* |
92
|
|
|
* @return Scriptotek\GoogleBooks\Volume |
93
|
|
|
*/ |
94
|
|
|
public function first() |
95
|
|
|
{ |
96
|
|
|
if ($this->fetch()->totalItems) { |
97
|
|
|
return $this->fetch()->items[0]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets all the results from the query. Possibly chunks the results |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
* @see chunk() |
108
|
|
|
*/ |
109
|
|
|
public function all() |
110
|
|
|
{ |
111
|
|
|
if ($this->chunk) { |
112
|
|
|
return $this->data = array_chunk($this->fetch()->items, $this->chunk); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->data = $this->fetch()->items; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the chunk property. Used as an alias for array chunking. Primarily for views |
120
|
|
|
* |
121
|
|
|
* @param int $chunk |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function chunk($chunk) |
125
|
|
|
{ |
126
|
|
|
$this->chunk = $chunk; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Gets the paramters to be passed to the API request. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
private function getParams() |
136
|
|
|
{ |
137
|
|
|
$params = []; |
138
|
|
|
$params['q'] = $this->query; |
139
|
|
|
$params['maxResults'] = $this->limit ?: 40; |
140
|
|
|
|
141
|
|
|
return $params; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|