1
1
<?php
2
2
declare (strict_types=1 );
3
+
3
4
/**
4
5
* BEdita, API-first content management framework
5
6
* Copyright 2023 Atlas Srl, ChannelWeb Srl, Chialab Srl
11
12
12
13
namespace BEdita \SDK ;
13
14
15
+ use Exception ;
16
+
14
17
/**
15
18
* BEdita API Client class
16
19
*/
@@ -31,8 +34,9 @@ public function authenticate(string $username, string $password): ?array
31
34
unset($ headers ['Authorization ' ]);
32
35
$ this ->setDefaultHeaders ($ headers );
33
36
}
37
+ $ body = (string )json_encode (compact ('username ' , 'password ' ) + ['grant_type ' => 'password ' ]);
34
38
35
- return $ this ->post ('/auth ' , json_encode ( compact ( ' username ' , ' password ' ) + [ ' grant_type ' => ' password ' ]) , ['Content-Type ' => 'application/json ' ]);
39
+ return $ this ->post ('/auth ' , $ body , ['Content-Type ' => 'application/json ' ]);
36
40
}
37
41
38
42
/**
@@ -51,73 +55,73 @@ public function getObjects(string $type = 'objects', ?array $query = null, ?arra
51
55
/**
52
56
* GET a single object of a given type
53
57
*
54
- * @param int| string $id Object id
58
+ * @param string|int $id Object id
55
59
* @param string $type Object type name
56
60
* @param array|null $query Optional query string
57
61
* @param array|null $headers Custom request headers
58
62
* @return array|null Response in array format
59
63
*/
60
- public function getObject ($ id , string $ type = 'objects ' , ?array $ query = null , ?array $ headers = null ): ?array
64
+ public function getObject (string | int $ id , string $ type = 'objects ' , ?array $ query = null , ?array $ headers = null ): ?array
61
65
{
62
66
return $ this ->get (sprintf ('/%s/%s ' , $ type , $ id ), $ query , $ headers );
63
67
}
64
68
65
69
/**
66
70
* Get a list of related resources or objects
67
71
*
68
- * @param int| string $id Resource id or object uname/id
72
+ * @param string|int $id Resource id or object uname/id
69
73
* @param string $type Type name
70
74
* @param string $relation Relation name
71
75
* @param array|null $query Optional query string
72
76
* @param array|null $headers Custom request headers
73
77
* @return array|null Response in array format
74
78
*/
75
- public function getRelated ($ id , string $ type , string $ relation , ?array $ query = null , ?array $ headers = null ): ?array
79
+ public function getRelated (string | int $ id , string $ type , string $ relation , ?array $ query = null , ?array $ headers = null ): ?array
76
80
{
77
81
return $ this ->get (sprintf ('/%s/%s/%s ' , $ type , $ id , $ relation ), $ query , $ headers );
78
82
}
79
83
80
84
/**
81
85
* Add a list of related resources or objects
82
86
*
83
- * @param int| string $id Resource id or object uname/id
87
+ * @param string|int $id Resource id or object uname/id
84
88
* @param string $type Type name
85
89
* @param string $relation Relation name
86
90
* @param array $data Related resources or objects to add, MUST contain id and type
87
91
* @param array|null $headers Custom request headers
88
92
* @return array|null Response in array format
89
93
*/
90
- public function addRelated ($ id , string $ type , string $ relation , array $ data , ?array $ headers = null ): ?array
94
+ public function addRelated (string | int $ id , string $ type , string $ relation , array $ data , ?array $ headers = null ): ?array
91
95
{
92
96
return $ this ->post (sprintf ('/%s/%s/relationships/%s ' , $ type , $ id , $ relation ), json_encode (compact ('data ' )), $ headers );
93
97
}
94
98
95
99
/**
96
100
* Remove a list of related resources or objects
97
101
*
98
- * @param int| string $id Resource id or object uname/id
102
+ * @param string|int $id Resource id or object uname/id
99
103
* @param string $type Type name
100
104
* @param string $relation Relation name
101
105
* @param array $data Related resources or objects to remove from relation
102
106
* @param array|null $headers Custom request headers
103
107
* @return array|null Response in array format
104
108
*/
105
- public function removeRelated ($ id , string $ type , string $ relation , array $ data , ?array $ headers = null ): ?array
109
+ public function removeRelated (string | int $ id , string $ type , string $ relation , array $ data , ?array $ headers = null ): ?array
106
110
{
107
111
return $ this ->delete (sprintf ('/%s/%s/relationships/%s ' , $ type , $ id , $ relation ), json_encode (compact ('data ' )), $ headers );
108
112
}
109
113
110
114
/**
111
115
* Replace a list of related resources or objects: previuosly related are removed and replaced with these.
112
116
*
113
- * @param int| string $id Object id
117
+ * @param string|int $id Object id
114
118
* @param string $type Object type name
115
119
* @param string $relation Relation name
116
120
* @param array $data Related resources or objects to insert
117
121
* @param array|null $headers Custom request headers
118
122
* @return array|null Response in array format
119
123
*/
120
- public function replaceRelated ($ id , string $ type , string $ relation , array $ data , ?array $ headers = null ): ?array
124
+ public function replaceRelated (string | int $ id , string $ type , string $ relation , array $ data , ?array $ headers = null ): ?array
121
125
{
122
126
return $ this ->patch (sprintf ('/%s/%s/relationships/%s ' , $ type , $ id , $ relation ), json_encode (compact ('data ' )), $ headers );
123
127
}
@@ -170,11 +174,11 @@ public function saveObject(string $type, array $data, ?array $headers = null): ?
170
174
/**
171
175
* Delete an object (DELETE) => move to trashcan.
172
176
*
173
- * @param int| string $id Object id
177
+ * @param string|int $id Object id
174
178
* @param string $type Object type name
175
179
* @return array|null Response in array format
176
180
*/
177
- public function deleteObject ($ id , string $ type ): ?array
181
+ public function deleteObject (string | int $ id , string $ type ): ?array
178
182
{
179
183
return $ this ->delete (sprintf ('/%s/%s ' , $ type , $ id ));
180
184
}
@@ -191,7 +195,7 @@ public function deleteObjects(array $ids, string $type = 'objects'): ?array
191
195
$ response = null ;
192
196
try {
193
197
$ response = $ this ->delete (sprintf ('/%s?ids=%s ' , $ type , implode (', ' , $ ids )));
194
- } catch (\ Exception $ e ) {
198
+ } catch (Exception $ e ) {
195
199
// fallback to delete one by one, to be retrocompatible
196
200
foreach ($ ids as $ id ) {
197
201
$ response = !empty ($ response ) ? $ response : $ this ->deleteObject ($ id , $ type );
@@ -204,10 +208,10 @@ public function deleteObjects(array $ids, string $type = 'objects'): ?array
204
208
/**
205
209
* Remove an object => permanently remove object from trashcan.
206
210
*
207
- * @param int| string $id Object id
211
+ * @param string|int $id Object id
208
212
* @return array|null Response in array format
209
213
*/
210
- public function remove ($ id ): ?array
214
+ public function remove (string | int $ id ): ?array
211
215
{
212
216
return $ this ->delete (sprintf ('/trash/%s ' , $ id ));
213
217
}
@@ -223,7 +227,7 @@ public function removeObjects(array $ids): ?array
223
227
$ response = null ;
224
228
try {
225
229
$ response = $ this ->delete (sprintf ('/trash?ids=%s ' , implode (', ' , $ ids )));
226
- } catch (\ Exception $ e ) {
230
+ } catch (Exception $ e ) {
227
231
// fallback to delete one by one, to be retrocompatible
228
232
foreach ($ ids as $ id ) {
229
233
$ response = !empty ($ response ) ? $ response : $ this ->remove ($ id );
@@ -270,7 +274,7 @@ public function upload(string $filename, string $filepath, ?array $headers = nul
270
274
* @return array|null Response in array format
271
275
* @throws \BEdita\SDK\BEditaClientException
272
276
*/
273
- public function createMediaFromStream ($ streamId , string $ type , array $ body ): ?array
277
+ public function createMediaFromStream (string $ streamId , string $ type , array $ body ): ?array
274
278
{
275
279
$ id = $ this ->createMedia ($ type , $ body );
276
280
$ this ->addStreamToMedia ($ streamId , $ id , $ type );
@@ -335,7 +339,7 @@ public function addStreamToMedia(string $streamId, string $id, string $type): vo
335
339
* @param array $query The query params for thumbs call.
336
340
* @return array|null Response in array format
337
341
*/
338
- public function thumbs ($ id = null , $ query = []): ?array
342
+ public function thumbs (? int $ id = null , array $ query = []): ?array
339
343
{
340
344
if (empty ($ id ) && empty ($ query ['ids ' ])) {
341
345
throw new BEditaClientException ('Invalid empty id|ids for thumbs ' );
@@ -377,11 +381,11 @@ public function relationData(string $name): ?array
377
381
/**
378
382
* Restore object from trash
379
383
*
380
- * @param int| string $id Object id
384
+ * @param string|int $id Object id
381
385
* @param string $type Object type name
382
386
* @return array|null Response in array format
383
387
*/
384
- public function restoreObject ($ id , string $ type ): ?array
388
+ public function restoreObject (string | int $ id , string $ type ): ?array
385
389
{
386
390
return $ this ->patch (
387
391
sprintf ('/trash/%s ' , $ id ),
0 commit comments