1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clarify; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use Clarify\Exceptions\InvalidIntegerArgumentException; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the base class that all of the individual media-related classes extend. At the moment, it simply initializes |
11
|
|
|
* the connection by setting the user agent and the base URI for the API calls. |
12
|
|
|
* |
13
|
|
|
* Class Client |
14
|
|
|
* @package Clarify |
15
|
|
|
* |
16
|
|
|
* @property mixed stores This is the stores subresource of the client. |
17
|
|
|
* @property mixed products This is the products subresource of the client. |
18
|
|
|
*/ |
19
|
|
|
class Client |
20
|
|
|
{ |
21
|
|
|
const USER_AGENT = 'clarify-php/2.0.0'; |
22
|
|
|
|
23
|
|
|
protected $baseURI = 'https://api.clarify.io/v1/'; |
24
|
|
|
protected $apiKey = ''; |
25
|
|
|
protected $client = null; |
26
|
|
|
protected $request = null; |
27
|
|
|
public $response = null; |
28
|
|
|
public $statusCode = null; |
29
|
|
|
public $detail = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $key |
33
|
|
|
* @param null $httpClient |
34
|
|
|
*/ |
35
|
|
|
public function __construct($key, $httpClient = null) |
36
|
|
|
{ |
37
|
|
|
$this->apiKey = $key; |
38
|
|
|
$this->httpClient = (is_null($httpClient)) ? new GuzzleClient( |
|
|
|
|
39
|
|
|
['base_uri' => $this->baseURI, 'headers' => ['User-Agent' => $this::USER_AGENT . '/' . PHP_VERSION ]] |
40
|
|
|
) : $httpClient; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $uri |
45
|
|
|
* @param array $options |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function post($uri, array $options = array()) |
49
|
|
|
{ |
50
|
|
|
$this->process('PUT', $uri, ['form_params' => $options]); |
51
|
|
|
$this->detail = json_decode($this->response->getBody(), true); |
52
|
|
|
|
53
|
|
|
return $this->isSuccessful(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $uri |
58
|
|
|
* @param array $options |
59
|
|
|
* @return bool |
60
|
|
|
* @throws Exceptions\InvalidIntegerArgumentException |
61
|
|
|
*/ |
62
|
|
|
public function put($uri, array $options) |
63
|
|
|
{ |
64
|
|
|
$version = isset($options['version']) ? $options['version'] : '1'; |
65
|
|
|
if (!is_numeric($version)) { |
66
|
|
|
throw new InvalidIntegerArgumentException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
unset($options['id']); |
70
|
|
|
$this->process('PUT', $uri, ['form_params' => $options]); |
71
|
|
|
$this->detail = json_decode($this->response->getBody(), true); |
72
|
|
|
|
73
|
|
|
return $this->isSuccessful(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $uri |
78
|
|
|
* @param array $parameters |
79
|
|
|
* @return array|bool|float|int|string |
80
|
|
|
*/ |
81
|
|
|
public function get($uri, array $parameters = array()) |
82
|
|
|
{ |
83
|
|
|
$options = array(); |
84
|
|
|
$options['query'] = $parameters; |
85
|
|
|
|
86
|
|
|
$this->process('GET', $uri, $options); |
87
|
|
|
$this->detail = json_decode($this->response->getBody(), true); |
88
|
|
|
|
89
|
|
|
return $this->detail; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* This deletes a resource using a full URI. |
94
|
|
|
* |
95
|
|
|
* @param $uri |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function delete($uri) |
99
|
|
|
{ |
100
|
|
|
return $this->process('DELETE', $uri); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $method |
105
|
|
|
* @param $uri |
106
|
|
|
* @param array $options |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
protected function process($method, $uri, $options = array()) |
110
|
|
|
{ |
111
|
|
|
$options['http_errors'] = 'false'; |
112
|
|
|
$options['headers'] = ['Authorization' => 'Bearer ' . $this->apiKey ]; |
113
|
|
|
|
114
|
|
|
$this->response = $this->httpClient->request($method, $uri, $options); |
|
|
|
|
115
|
|
|
$this->statusCode = $this->response->getStatusCode(); |
116
|
|
|
|
117
|
|
|
return $this->isSuccessful(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function isSuccessful() |
121
|
|
|
{ |
122
|
|
|
$successful = false; |
123
|
|
|
|
124
|
|
|
if (2 == substr($this->statusCode, 0, 1)) { |
125
|
|
|
$successful = true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $successful; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.