1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the KongAdminApi package. |
5
|
|
|
* |
6
|
|
|
* (c) Unikorp <https://github.com/unikorp> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Unikorp\KongAdminApi; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* abstract node |
16
|
|
|
* |
17
|
|
|
* @author VEBER Arnaud <https://github.com/VEBERArnaud> |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractNode implements NodeInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* urlencoded content type |
23
|
|
|
* @const array URLENCODED_CONTENT_TYPE |
24
|
|
|
*/ |
25
|
|
|
const URLENCODED_CONTENT_TYPE = [ |
26
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* json content type |
31
|
|
|
* @const array JSON_CONTENT_TYPE |
32
|
|
|
*/ |
33
|
|
|
const JSON_CONTENT_TYPE = [ |
34
|
|
|
'Content-Type' => 'application/json', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* client |
39
|
|
|
* @var \Unikorp\KongAdminApi\Client $client |
40
|
|
|
*/ |
41
|
|
|
private $client = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param \Unikorp\KongAdminApi\Client $client |
47
|
|
|
*/ |
48
|
9 |
|
public function __construct(Client $client) |
49
|
|
|
{ |
50
|
9 |
|
$this->client = $client; |
51
|
9 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* get |
55
|
|
|
* |
56
|
|
|
* @param string $endpoint |
57
|
|
|
* @param DocumentInterface $document |
58
|
|
|
* |
59
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
60
|
|
|
*/ |
61
|
20 |
|
protected function get($endpoint, DocumentInterface $document = null) |
62
|
|
|
{ |
63
|
20 |
|
return $this->client->getHttpClient()->get( |
64
|
20 |
|
sprintf('%1$s?%2$s', $endpoint, $document ? $document->toQueryString() : ''), |
65
|
20 |
|
self::URLENCODED_CONTENT_TYPE |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* post |
71
|
|
|
* |
72
|
|
|
* @param string $endpoint |
73
|
|
|
* @param DocumentInterface $document |
74
|
|
|
* |
75
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
76
|
|
|
*/ |
77
|
8 |
View Code Duplication |
protected function post($endpoint, DocumentInterface $document = null) |
78
|
|
|
{ |
79
|
8 |
|
return $this->client->getHttpClient()->post( |
80
|
8 |
|
$endpoint, |
81
|
8 |
|
self::JSON_CONTENT_TYPE, |
82
|
8 |
|
$document ? $document->toJson() : '[]' |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* put |
88
|
|
|
* |
89
|
|
|
* @param string $endpoint |
90
|
|
|
* @param DocumentInterface $document |
91
|
|
|
* |
92
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
93
|
|
|
*/ |
94
|
6 |
View Code Duplication |
protected function put($endpoint, DocumentInterface $document = null) |
95
|
|
|
{ |
96
|
6 |
|
$document->setCreatedAt(time()); |
97
|
|
|
|
98
|
6 |
|
return $this->client->getHttpClient()->put( |
99
|
6 |
|
$endpoint, |
100
|
6 |
|
self::JSON_CONTENT_TYPE, |
101
|
6 |
|
$document ? $document->toJson() : '[]' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* patch |
107
|
|
|
* |
108
|
|
|
* @param string $endpoint |
109
|
|
|
* @param DocumentInterface $document |
110
|
|
|
* |
111
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
112
|
|
|
*/ |
113
|
6 |
View Code Duplication |
protected function patch($endpoint, DocumentInterface $document = null) |
114
|
|
|
{ |
115
|
6 |
|
return $this->client->getHttpClient()->patch( |
116
|
6 |
|
$endpoint, |
117
|
6 |
|
self::JSON_CONTENT_TYPE, |
118
|
6 |
|
$document ? $document->toJson() : '[]' |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* delete |
124
|
|
|
* |
125
|
|
|
* @param string $endpoint |
126
|
|
|
* @param DocumentInterface $document |
127
|
|
|
* |
128
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
129
|
|
|
*/ |
130
|
8 |
View Code Duplication |
protected function delete($endpoint, DocumentInterface $document = null) |
131
|
|
|
{ |
132
|
8 |
|
return $this->client->getHttpClient()->delete( |
133
|
8 |
|
$endpoint, |
134
|
8 |
|
self::JSON_CONTENT_TYPE, |
135
|
8 |
|
$document ? $document->toJson() : '[]' |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|