-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathStack.php
More file actions
executable file
·364 lines (341 loc) · 10.6 KB
/
Stack.php
File metadata and controls
executable file
·364 lines (341 loc) · 10.6 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
* Stack Class to initialize the provided parameter Stack
*
* PHP version 5
*
* @category PHP
* @package Contentstack
* @author Uttam K Ukkoji <uttamukkoji@gmail.com>
* @author Rohit Mishra <rhtmishra4545@gmail.com>
* @copyright 2012-2021 Contentstack. All Rights Reserved
* @license https://github.com/contentstack/contentstack-php/blob/master/LICENSE.txt MIT Licence
* @link https://pear.php.net/package/contentstack
* */
namespace Contentstack\Stack;
use Contentstack\Support\Utility;
use Contentstack\Stack\ContentType;
use Contentstack\Stack\Assets;
require_once __DIR__."/../Config/index.php";
/**
* Stack Class to initialize the provided parameter Stack
*
* @category PHP
* @package Contentstack
* @author Uttam K Ukkoji <uttamukkoji@gmail.com>
* @author Rohit Mishra <rhtmishra4545@gmail.com>
* @copyright 2012-2021 Contentstack. All Rights Reserved
* @license https://github.com/contentstack/contentstack-php/blob/master/LICENSE.txt MIT Licence
* @link https://pear.php.net/package/contentstack
* */
#[\AllowDynamicProperties]
class Stack
{
/* header - array where all the headers for the request will be stored */
var $header = array();
/* host - Host to be used to fetch the content */
private $host = HOST;
/* port - Port of the HOST */
private $port = PORT;
/* protocol - Protocol to be used to fetch the content */
private $protocol = PROTOCOL;
/* environment - Environment on which content published to be retrieved */
private $environment;
/**
* Constructor of the Stack
*
* @param string $api_key - API Key of Stack
* @param string $delivery_token - Delivery Token of Stack
* @param string $environment - Environment Name of Stack
* @param string $region - Region name of Contentstack. (default region 'us')
*
* */
public function __construct(
$api_key = '',
$delivery_token = '',
$environment = '',
$config = array('region'=> 'us', 'branch'=> '', 'live_preview' => array())
) {
$previewHost = 'api.contentstack.io';
if ($config && $config !== "undefined" && array_key_exists('region', $config) && $config['region'] !== "undefined" && $config['region'] !== "us" ) {
$this->host = $config['region'].'-cdn.contentstack.com';
$previewHost = $config['region'].'-api.contentstack.com';
}
$this->header = Utility::validateInput(
'stack', array('api_key' => $api_key,
'access_token' => $delivery_token,
'environment' => $environment,
'region' => $config['region'] ?? '',
'branch' => $config['branch'] ?? '')
);
$this->environment = $this->header['environment'];
unset($this->header['environment']);
$livePreview = array('enable' => false, 'host' => $previewHost);
$this->live_preview = $config['live_preview'] ? array_merge($livePreview, $config['live_preview']) : $livePreview;
$this->proxy = array_key_exists("proxy",$config) ? $config['proxy'] : array('proxy'=>array());
$this->timeout = array_key_exists("timeout",$config) ? $config['timeout'] : '3000';
$this->retryDelay = array_key_exists("retryDelay",$config) ? $config['retryDelay'] : '3000';
$this->retryLimit = array_key_exists("retryLimit",$config) ? $config['retryLimit'] : '5';
$this->errorRetry = array_key_exists("errorRetry",$config) ? $config['errorRetry'] : array('errorRetry'=>array(408, 429));
return $this;
}
/**
* To initialize the ContentType object from
* where the content will be fetched/retrieved.
*
* @param string $contentTypeId - valid content type
* uid relevant to configured stack
*
* @return ContentType
* */
public function ContentType($contentTypeId = '')
{
return new ContentType($contentTypeId, $this);
}
/**
* Assets Class to initalize your Assets
*
* @param string $assetUid - valid asset uid relevent to configured stack
*
* @return Assets
* */
public function Assets($assetUid = '')
{
return new Assets($assetUid, $this);
}
/**
* ImageTrasform function is define for image manipulation with different
*
* @param $url : Image url on which we want to manipulate.
* @param $parameters : It is an second parameter
* in which we want to place different
* manipulation key and value in array form
*
* @return string
* */
public function ImageTrasform($url, $parameters)
{
if (is_string($url) === true && strlen($url) > 0
&& is_array($parameters) === true
&& count($parameters) > 0
) {
$params = array();
foreach ($parameters as $key => $value) {
array_push($params, $key . '=' .$value);
}
$params = implode("&", $params);
$url = (strpos($url, '?') === false)
? $url .'?'.$params:
$url .'&'.$params;
return $url;
} else {
Utility::debug(
"Please provide valid url
and array of transformation parameters."
);
}
}
public function LivePreviewQuery($parameters) {
$this->live_preview['live_preview'] = $parameters['live_preview'] ?? 'init';
$this->live_preview['content_type_uid'] = $parameters['content_type_uid'] ?? null;
$this->live_preview['entry_uid'] = $parameters['entry_uid'] ?? null;
if(array_key_exists('content_type_uid',$parameters) && array_key_exists('entry_uid',$parameters)){
$this->ContentType($parameters['content_type_uid'])->Entry($parameters['entry_uid'])->fetch();
}
}
/**
* To get the last_activity information of the
* configured environment from all the content types
*
* @return Result
* */
public function getLastActivities()
{
$this->_query = array("only_last_activity" => "true");
return Utility::getLastActivites($this);
}
/**
* To set the host on stack object
*
* @param string $host - host name/ipaddress from where the content to be fetched
*
* @return Stack
* */
public function setHost($host = '')
{
Utility::validateInput('host', $host);
$this->host = $host;
return $this;
}
/**
* This function returns host.
*
* @return string
* */
public function getHost()
{
return $this->host;
}
/**
* This function sets protocol.
*
* @param string $protocol - protocol type
*
* @return Stack
* */
public function setProtocol($protocol = '')
{
Utility::validateInput('protocol', $protocol);
$this->protocol = $protocol;
return $this;
}
/**
* This function return protocol type.
*
* @return string
* */
public function getProtocol()
{
return $this->protocol;
}
/**
* This function sets Port.
*
* @param string $port - Port Number
*
* @return Stack
* */
public function setPort($port = '')
{
Utility::validateInput('port', $port);
$this->port = $port;
return $this;
}
/**
* This function return Port.
*
* @return string
* */
public function getPort()
{
return $this->port;
}
/**
* This function sets API Key.
*
* @param string $api_key - API Key
*
* @return Stack
* */
public function setAPIKEY($api_key = '')
{
Utility::validateInput('api_key', $api_key);
$this->header['api_key'] = $api_key;
return $this;
}
/**
* This function sets Delivery Token.
*
* @param string $delivery_token - Delivery Token
*
* @return Stack
* */
public function setDeliveryToken($delivery_token = '')
{
Utility::validateInput('access_token', $delivery_token);
$this->header['access_token'] = $delivery_token;
return $this;
}
/**
* This function sets environment name.
*
* @param string $environment - Name of Environment
*
* @return Stack
* */
public function setEnvironment($environment = '')
{
Utility::validateInput('environment', $environment);
$this->environment = $environment;
return $this;
}
/**
* This function returns API Key.
*
* @return string
* */
public function getAPIKEY()
{
return $this->header['api_key'];
}
/**
* This function returns Delivery Token.
*
* @return string
* */
public function DeliveryToken()
{
return $this->header['access_token'];
}
/**
* This function returns environment name.
*
* @return string
* */
public function getEnvironment()
{
return $this->environment;
}
/**
* This function sets Branch.
*
* @param string $branch - Name of branch
*
* @return Stack
* */
public function setBranch($branch = '')
{
Utility::validateInput('branch', $branch);
$this->header['branch'] = $branch;
return $this;
}
/**
* This function returns Branch.
*
* @return string
* */
public function Branch()
{
return $this->header['branch'];
}
/**
* This call returns comprehensive information of all
* the content types available in a particular stack in your account.
*
* @param object $params - query params for getting content-type.
*
* @return Stack
* */
public function getContentTypes($params)
{
if ($params && $params !== "undefined") {
$myArray = json_decode($params, true);
$this->_query = $myArray;
}
return Utility::contentstackRequest($this, $this, "getcontentTypes");
}
/**
* Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
*
* @param object $params - params is an object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries.
*
* @return Stack
* */
public function sync($params)
{
if ($params && $params !== "undefined") {
$this->_query = $params;
}
return Utility::contentstackRequest($this, $this, "sync");
}
}