forked from CristalTeam/php-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApi.php
More file actions
executable file
·162 lines (139 loc) · 3.93 KB
/
Api.php
File metadata and controls
executable file
·162 lines (139 loc) · 3.93 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Cristal\ApiWrapper;
use Cristal\ApiWrapper\Concerns\HasCache;
use Cristal\ApiWrapper\Exceptions\ApiEntityNotFoundException;
use Cristal\ApiWrapper\Transports\TransportInterface;
/**
* Class Api.
*/
class Api
{
use HasCache;
/**
* @var TransportInterface
*/
protected $transport;
/**
* Api constructor.
*
* @param TransportInterface $transport
*/
public function __construct(TransportInterface $transport)
{
$this->transport = $transport;
}
/**
* Get current instance of TransportInterface.
*
* @return TransportInterface
*/
public function getTransport()
{
return $this->transport;
}
/**
* Magical use for getObjects(), getObject(), createObject(), updateObject(), deleteObject().
*
* @param $name
* @param $arguments
*
* @return array
*/
public function __call($name, $arguments)
{
if (method_exists($this, $name)) {
return $this->$name(...$arguments);
}
preg_match('/^(get|create|update|delete)([\w\-_\/]+?)$/', (string) $name, $matches);
$endpoint = lcfirst($matches[2]);
if ('get' === $matches[1]) {
if (array_key_exists(0, $arguments) && !is_array($arguments[0])) {
return $this->findOne($endpoint, ...$arguments);
}
return $this->findAll($endpoint, ...$arguments);
}
return $this->{$matches[1]}($endpoint, ...$arguments);
}
/**
* Call API for find all results of the entrypoint.
*
* @param string $endpoint
* @param array $filters
*
* @return array
*/
protected function findAll(string $endpoint, array $filters = []): array
{
$key = md5(__FUNCTION__.$endpoint.json_encode($filters));
if ($this->hasCache($key)) {
return $this->getCache($key);
}
return $this->setCache(
$key,
$this->getTransport()->request('/'.$endpoint, $filters) ?? []
);
}
/**
* Call API for find entity of entrypoint.
*
* @param string $endpoint
* @param int $id
* @param array $filters
*
* @return array
*/
protected function findOne(string $endpoint, $id, array $filters = [])
{
// Makes no sense to proceed API call if we passed null id.
// It would mean that we would like to do a findAll call instead and it could cause side effects.
if ($id === null) {
throw new ApiEntityNotFoundException([]);
}
$uri = '/'.$endpoint.'/'.$id;
$key = $uri.'?'.http_build_query($filters);
if ($this->hasCache($key)) {
return $this->getCache($key);
}
return $this->getTransport()->request($uri, $filters) ?? [];
}
/**
* Call API for update an entity.
*
* @param string $endpoint
* @param int $id
* @param $attributes
*
* @return mixed|array
*/
protected function update(string $endpoint, $id, $attributes)
{
$key = $endpoint.'/'.$id.'?';
return $this->setCache($key, $this->getTransport()->request('/'.$endpoint.'/'.$id, $attributes, 'put') ?? []);
}
/**
* Call API for create an entity.
*
* @param string $endpoint
* @param $attributes
*
* @return mixed|array
*/
protected function create(string $endpoint, $attributes)
{
return $this->getTransport()->request('/'.$endpoint, $attributes, 'post') ?? [];
}
/**
* Call API for delete an entity.
*
* @param string $endpoint
* @param int $id
*
* @return mixed|array
*/
protected function delete(string $endpoint, $id, array $data = [])
{
$key = $endpoint.'/'.$id.'?';
$this->deleteCache($key);
return $this->getTransport()->request('/'.$endpoint.'/'.$id, $data, 'delete') ?? [];
}
}