forked from varsitynewsnetwork/wordpress-rest-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractWpEndpoint.php
More file actions
100 lines (82 loc) · 2.83 KB
/
Copy pathAbstractWpEndpoint.php
File metadata and controls
100 lines (82 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Vnn\WpApiClient\Endpoint;
use GuzzleHttp\Psr7\Request;
use RuntimeException;
use Vnn\WpApiClient\WpClient;
/**
* Class AbstractWpEndpoint
* @package Vnn\WpApiClient\Endpoint
*/
abstract class AbstractWpEndpoint
{
/**
* @var WpClient
*/
private $client;
/**
* Users constructor.
* @param WpClient $client
*/
public function __construct(WpClient $client)
{
$this->client = $client;
}
abstract protected function getEndpoint();
/**
* @param int $id
* @param array $params - parameters that can be passed to GET
* e.g. for tags: https://developer.wordpress.org/rest-api/reference/tags/#arguments
* @return array
*/
public function get($id = null, array $params = null)
{
$uri = $this->getEndpoint();
$uri .= (is_null($id)?'': '/' . $id);
$uri .= (is_null($params)?'': '?' . http_build_query($params));
$request = new Request('GET', $uri);
$response = $this->client->send($request);
if ($response->hasHeader('Content-Type')
&& substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') {
return json_decode($response->getBody()->getContents(), true);
}
throw new RuntimeException('Unexpected response');
}
/**
* @param int $id
* @param array $params - parameters that can be passed to GET
* e.g. for tags: https://developer.wordpress.org/rest-api/reference/tags/#arguments
* @return \Psr\Http\Message\ResponseInterface
*/
public function getResponse($id = null, array $params = null)
{
$uri = $this->getEndpoint();
$uri .= (is_null($id)?'': '/' . $id);
$uri .= (is_null($params)?'': '?' . http_build_query($params));
$request = new Request('GET', $uri);
$response = $this->client->send($request);
if ($response->hasHeader('Content-Type')
&& substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') {
return $response;
}
throw new RuntimeException('Unexpected response');
}
/**
* @param array $data
* @return array
*/
public function save(array $data)
{
$url = $this->getEndpoint();
if (isset($data['id'])) {
$url .= '/' . $data['id'];
unset($data['id']);
}
$request = new Request('POST', $url, ['Content-Type' => 'application/json'], json_encode($data));
$response = $this->client->send($request);
if ($response->hasHeader('Content-Type')
&& substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') {
return json_decode($response->getBody()->getContents(), true);
}
throw new RuntimeException('Unexpected response');
}
}