forked from prismicio-community/php-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiData.php
More file actions
225 lines (202 loc) · 4.84 KB
/
ApiData.php
File metadata and controls
225 lines (202 loc) · 4.84 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
namespace Prismic;
use Prismic\Exception;
use stdClass;
class ApiData
{
/**
* An array of the usable refs for this API
* @var array
*/
private $refs;
/**
* An array of the available bookmarks
*
* @deprecated 5.4.0
*
* @var array
*/
private $bookmarks;
/**
* An array of the available types
* @var array
*/
private $types;
/**
* An array of the available tags
* @var array
*/
private $tags;
/**
* An array of the available forms
* @var array
*/
private $forms;
/**
* The URL of the endpoint to initiate the OAuth authentication
* @var string
*/
private $oauth_initiate;
/**
* The URL of the endpoint to authenticate through OAuth
* @var string
*/
private $oauth_token;
/**
* List of both drafts and running experiments from Prismic
* @var Experiments
*/
private $experiments;
/**
* List of configured languages
* @var array
*/
private $languages;
/**
* A constructor to build the object when you've retrieved all the data you need.
*
* @param array $refs
* @param array $bookmarks (deprecated since 5.4.0)
* @param array $types
* @param array $languages
* @param array $tags
* @param array $forms
* @param Experiments $experiments
* @param string $oauth_initiate
* @param string $oauth_token
*/
private function __construct(
array $refs,
array $bookmarks,
array $types,
array $languages,
array $tags,
array $forms,
Experiments $experiments,
string $oauth_initiate,
string $oauth_token
) {
$this->refs = $refs;
$this->bookmarks = $bookmarks;
$this->types = $types;
$this->languages = $languages;
$this->tags = $tags;
$this->forms = $forms;
$this->experiments = $experiments;
$this->oauth_initiate = $oauth_initiate;
$this->oauth_token = $oauth_token;
}
/**
* Return a new ApiData instance from the given JSON string
* @param string $json
* @return static
*/
public static function withJsonString(string $json) : self
{
$data = json_decode($json);
if (! $data) {
throw new Exception\RuntimeException(sprintf(
'Unable to decode JSON response: %s',
json_last_error_msg()
), json_last_error());
}
return static::withJsonObject($data);
}
/**
* Return a new ApiData instance from the given JSON decoded object
* @param stdClass $json
* @return self
*/
public static function withJsonObject(stdClass $json) : self
{
$experiments = isset($json->experiments)
? Experiments::parse($json->experiments)
: Experiments::parse(new stdClass);
return new self(
array_map(
function ($ref) {
return Ref::parse($ref);
},
$json->refs
),
isset($json->bookmarks) ? (array)$json->bookmarks : [],
(array)$json->types,
array_map(
function ($language) {
return Language::parse($language);
},
(array)$json->languages
),
$json->tags,
(array)$json->forms,
$experiments,
$json->oauth_initiate,
$json->oauth_token
);
}
/**
* Get the refs
* @return Ref[]
*/
public function getRefs() : array
{
return $this->refs;
}
/**
* Get the bookmarks
*
* @deprecated 5.4.0 This method will no longer work after the 12 February 2025,
* as bookmarks are removed from the API.
*/
public function getBookmarks() : array
{
return $this->bookmarks;
}
/**
* Get the types
*/
public function getTypes() : array
{
return $this->types;
}
/**
* Get the tags
*/
public function getTags() : array
{
return $this->tags;
}
/**
* Get the forms
*/
public function getForms() : array
{
return $this->forms;
}
/**
* Get the Experiments
*/
public function getExperiments() : Experiments
{
return $this->experiments;
}
/**
* Get the endpoint to initiate OAuth
*/
public function getOauthInitiate() : string
{
return $this->oauth_initiate;
}
/**
* Get the endpoint to run OAuth
*/
public function getOauthToken() : string
{
return $this->oauth_token;
}
public function getLanguages() : array
{
return $this->languages;
}
}