-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathEtsy.php
123 lines (109 loc) · 2.46 KB
/
Etsy.php
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
<?php
namespace Etsy;
use Etsy\OAuth\Client;
use Etsy\Exception\ApiException;
class Etsy {
/**
* @var string
*/
protected $api_key;
/**
* @var string
*/
protected $client_id;
/**
* @var Etsy\OAuth\Client
*/
public static $client;
/**
* @var integer|string
*/
protected $user;
public function __construct(
string $client_id,
?string $api_key = null,
array $config = []
) {
$this->client_id = $client_id;
$this->api_key = $api_key;
static::$client = new Client($client_id);
if($api_key) {
static::$client->setApiKey($api_key);
}
static::$client->setConfig($config);
}
/**
* Returns a resource object from the request result.
*
* @param object $response
* @param string $resource
* @return mixed
*/
public static function getResource(
$response,
string $resource
) {
if(!$response || ($response->error ?? false)) {
return null;
}
if(isset($response->results)) {
return static::createCollection($response, $resource);
}
return static::createResource($response, $resource);
}
/**
*
*/
public static function createCollection(
$response,
string $resource
) {
$collection = new Collection($resource, $response->uri);
if(isset($response->count)) {
$collection->count = $response->count;
}
if(!count($response->results) || !isset($response->results)) {
return $collection;
}
$collection->data = static::createCollectionResources(
$response->results,
$resource
);
return $collection;
}
/**
* Creates an array of a single Etsy resource.
*
* @param array $records
* @param string $resource
* @return mixed
*/
public static function createCollectionResources(array $records, string $resource) {
$resource = __NAMESPACE__ . "\\Resources\\{$resource}";
return array_map(function($record) use($resource) {
return new $resource($record);
}, $records);
}
/**
* Creates a new Etsy resource.
*
* @param json $record
* @param string $resource
* @return mixed
*/
public static function createResource(
$record,
string $resource
) {
$resource = __NAMESPACE__ . "\\Resources\\{$resource}";
return new $resource($record);
}
/**
* Check the permission scopes for the current Etsy user.
*
* @return array
*/
public function scopes(): array {
return static::$client->scopes($this->api_key);
}
}