1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WPApi; |
4
|
|
|
|
5
|
|
|
use Goutte\Client; |
6
|
|
|
use WPApi\Interfaces\ApiInterface; |
7
|
|
|
use AutoMapper\AutoMapper; |
8
|
|
|
use WPApi\Model\Collection; |
9
|
|
|
|
10
|
|
|
abstract class AbstractApiCall implements ApiInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Goutte Client. |
14
|
|
|
* |
15
|
|
|
* @var Client |
16
|
|
|
*/ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* auto mapper. |
21
|
|
|
* |
22
|
|
|
* @var AutoMapper |
23
|
|
|
*/ |
24
|
|
|
protected $automapper; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->client = new Client(); |
29
|
|
|
$this->automapper = new AutoMapper(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function author($name) |
36
|
|
|
{ |
37
|
|
|
return $this->makeApiCall('query', [ |
38
|
|
|
'author' => $name, |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function browse($type) |
46
|
|
|
{ |
47
|
|
|
return $this->makeApiCall('query', [ |
48
|
|
|
'browse' => $type, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function search($keywords) |
56
|
|
|
{ |
57
|
|
|
return $this->makeApiCall('query', [ |
58
|
|
|
'search' => $keywords, |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function slug($slug) |
66
|
|
|
{ |
67
|
|
|
return $this->makeApiCall('information', [ |
68
|
|
|
'slug' => $slug, |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function tag($tag) |
76
|
|
|
{ |
77
|
|
|
return $this->makeApiCall('query', [ |
78
|
|
|
'tag' => $tag, |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* make the actual API call. |
84
|
|
|
* |
85
|
|
|
* @param string $type information or query |
86
|
|
|
* @param array $arguments arguments to query |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
protected function makeApiCall($type, array $arguments) |
91
|
|
|
{ |
92
|
|
|
$body = [ |
93
|
|
|
'action' => $this->getAction($type), |
94
|
|
|
'request' => serialize((object) $arguments), |
95
|
|
|
]; |
96
|
|
|
$this->client->request('POST', $this->getUri(), $body); |
97
|
|
|
$content = $this->client->getResponse()->getContent(); |
98
|
|
|
|
99
|
|
|
return $this->mapResponse(unserialize($content)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function mapResponseToCollection(\stdClass $response) |
103
|
|
|
{ |
104
|
|
|
$type = $this->getType(); |
105
|
|
|
if (!property_exists($response, $type)) { |
106
|
|
|
throw new \Exception("Property {$type} does not exist."); |
107
|
|
|
} |
108
|
|
|
$collection = new Collection(); |
109
|
|
|
foreach ($response->$type as $key => $value) { |
110
|
|
|
$model = $this->mapResponseToModel($value); |
111
|
|
|
$collection->add($model); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $collection; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function mapResponseToModel(\stdClass $response) |
118
|
|
|
{ |
119
|
|
|
$model = $this->createModel(); |
120
|
|
|
$this->automapper->map($response, $model); |
121
|
|
|
|
122
|
|
|
return $model; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* map response to model. |
127
|
|
|
* |
128
|
|
|
* @param \stdClass $response |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
protected function mapResponse(\stdClass $response) |
133
|
|
|
{ |
134
|
|
|
try { |
135
|
|
|
return $this->mapResponseToCollection($response); |
136
|
|
|
} catch (\Exception $e) { |
137
|
|
|
return $this->mapResponseToModel($response); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* get API action. |
143
|
|
|
* |
144
|
|
|
* @param string $type |
145
|
|
|
* |
146
|
|
|
* @return string formatted action |
147
|
|
|
*/ |
148
|
|
|
protected function getAction($type) |
149
|
|
|
{ |
150
|
|
|
if ($type == 'information') { |
151
|
|
|
return rtrim($this->getType(), 's')."_{$type}"; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return "{$type}_{$this->getType()}"; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* get URI of API. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
protected function getUri() |
163
|
|
|
{ |
164
|
|
|
return "http://api.wordpress.org/{$this->getType()}/info/1.0/"; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* get the type of object to query. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
abstract protected function getType(); |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* create model. |
176
|
|
|
* |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
|
|
abstract protected function createModel(); |
180
|
|
|
} |
181
|
|
|
|