-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAbstractResource.php
More file actions
121 lines (98 loc) · 3.41 KB
/
AbstractResource.php
File metadata and controls
121 lines (98 loc) · 3.41 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Author: Adrian Szuszkiewicz <me@imper.info>
* Github: https://github.com/imper86
* Date: 20.10.2019
* Time: 14:40
*/
namespace Imper86\PhpInpostApi\Resource;
use Imper86\PhpInpostApi\InpostApiInterface;
use InvalidArgumentException;
use Psr\Http\Client\ClientInterface as HttpClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use ReflectionClass;
abstract class AbstractResource implements ResourceInterface
{
/**
* @var InpostApiInterface
*/
protected $client;
/**
* @var RequestFactoryInterface
*/
protected $requestFactory;
/**
* @var UriFactoryInterface
*/
protected $uriFactory;
/**
* @var HttpClientInterface
*/
protected $httpClient;
/**
* @var StreamFactoryInterface
*/
protected $streamFactory;
/**
* @var ReflectionClass
*/
protected $reflection;
public function __construct(InpostApiInterface $client)
{
$this->client = $client;
$this->requestFactory = $client->getBuilder()->getRequestFactory();
$this->uriFactory = $client->getBuilder()->getUriFactory();
$this->streamFactory = $client->getBuilder()->getStreamFactory();
$this->httpClient = $client->getBuilder()->getHttpClient();
$this->reflection = new ReflectionClass($this);
}
public function __call($name, $arguments)
{
$className = $this->reflection->getName() . '\\' . ucfirst($name);
if (class_exists($className) && is_a($className, ResourceInterface::class, true)) {
return new $className($this->client);
}
throw new InvalidArgumentException(sprintf('%s resource not found', $name));
}
protected function apiGet(string $uri, ?array $query = null): ResponseInterface
{
$uri = $this->uriFactory->createUri($uri);
if ($query) {
$queryString = http_build_query($query);
$queryString = preg_replace('/%5B\d+%5D/', '[]', $queryString);
$uri = $uri->withQuery($queryString);
}
$request = $this->requestFactory->createRequest('GET', $uri);
return $this->httpClient->sendRequest($request);
}
protected function apiPost(string $uri, ?array $body = null): ResponseInterface
{
$request = $this->requestFactory->createRequest('POST', $uri);
if ($body) {
$stream = $this->streamFactory->createStream(json_encode($body));
$request = $request->withBody($stream);
}
return $this->httpClient->sendRequest($request);
}
protected function apiPut(string $uri, ?array $body = null): ResponseInterface
{
$request = $this->requestFactory->createRequest('PUT', $uri);
if ($body) {
$stream = $this->streamFactory->createStream(json_encode($body));
$request = $request->withBody($stream);
}
return $this->httpClient->sendRequest($request);
}
protected function apiDelete(string $uri, ?array $query = null): ResponseInterface
{
$uri = $this->uriFactory->createUri($uri);
if ($query) {
$uri = $uri->withQuery(http_build_query($query));
}
$request = $this->requestFactory->createRequest('DELETE', $uri);
return $this->httpClient->sendRequest($request);
}
}