You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using `Art4\JsonApiClient\Helper\Parser::parseResponseString($jsonapiString)` is a shortcut for directly using the Manager:
61
+
**Note**: Using `Art4\JsonApiClient\Helper\Parser` is just a shortcut for directly using the [Manager](docs/manager.md).
62
62
63
-
```php
64
-
use Art4\JsonApiClient\Exception\InputException;
65
-
use Art4\JsonApiClient\Exception\ValidationException;
66
-
use Art4\JsonApiClient\Input\RequestStringInput;
67
-
use Art4\JsonApiClient\Input\ResponseStringInput;
68
-
use Art4\JsonApiClient\Manager\ErrorAbortManager;
69
-
use Art4\JsonApiClient\V1\Factory;
70
-
71
-
// The Response body from a JSON API server
72
-
$jsonapiString = '{"meta":{"info":"Testing the JsonApiClient library."}}';
73
-
74
-
$manager = new ErrorAbortManager(
75
-
new Factory()
76
-
);
77
-
78
-
try {
79
-
// Use this if you have a response after calling a JSON API server
80
-
$input = new ResponseStringInput($jsonapiString);
81
-
82
-
// Or use this if you have a request to your JSON API server
83
-
$input = new RequestStringInput($jsonapiString);
84
-
85
-
$document = $manager->parse($input);
86
-
} catch (InputException $e) {
87
-
// $jsonapiString is not valid JSON
88
-
} catch (ValidationException $e) {
89
-
// $jsonapiString is not valid JSON API
90
-
}
91
-
92
-
// do something with $document
93
-
```
94
-
95
-
`$document` implements the `Art4\JsonApiClient\Accessable` interface to access the parsed data. It has `has($key)`, `get($key)` and `getKeys()` methods.
63
+
`$document` implements the `Art4\JsonApiClient\Accessable` interface to access the parsed data. It has the methods `has($key)`, `get($key)` and `getKeys()`.
96
64
97
65
```php
98
66
// Note that has() and get() have support for dot-notated keys
// Use this if you have a response after calling a JSON API server
26
+
$input = new ResponseStringInput($jsonapiString);
27
+
28
+
$document = $manager->parse($input);
29
+
} catch (InputException $e) {
30
+
// $jsonapiString is not valid JSON
31
+
} catch (ValidationException $e) {
32
+
// $jsonapiString is not valid JSON API
33
+
}
23
34
```
24
35
25
36
This returns a [Document](objects-document.md) object which provided all contents.
26
37
27
-
> **Note:** If `$jsonapiString` contains not valid JSON a [InputException](exception-introduction.md#inputexception) will be thrown.
28
-
> **Note:** If `$jsonapiString` contains not valid JSON API a [ValidationException](exception-introduction.md#validationexception) will be thrown.
29
-
30
38
### Parse a JSON API string for creating a new resource
31
39
32
40
Assuming you have get a request for creating a new resource. In this case the `id` in the resource item can be missed and you have to tell the Manager about this case.
33
41
34
42
```php
43
+
use Art4\JsonApiClient\Exception\InputException;
44
+
use Art4\JsonApiClient\Exception\ValidationException;
If you want a full array without any objects, set the `recursive` configuration into the `ArraySerializer` to parse all objects recursively into arrays.
154
-
155
-
```php
156
-
use Art4\JsonApiClient\Serializer\ArraySerializer;
157
-
158
-
$serializer = new ArraySerializer(['recursive' => true]);
159
-
$array = $serializer->serialize($document);
160
-
161
-
var_dump($array);
162
-
```
163
-
164
-
This returns:
165
-
166
-
```php
167
-
array(1) {
168
-
["meta"] => array(1) {
169
-
["info"] => string(28) "Testing the JsonApiClient library."
170
-
}
171
-
}
172
-
```
134
+
You can get all data as an array using the [ArraySerializer](serializer.md#arrayserializer).
JsonApiClient provides Serializer to convert a parsed JSON API document into other formats. Till now JsonApiClient comes only with an `ArraySerializer`.
4
+
5
+
## ArraySerializer
6
+
7
+
You can get all data as an array using the `ArraySerializer`.
8
+
9
+
### Get the containing data as array
10
+
11
+
```php
12
+
use Art4\JsonApiClient\Serializer\ArraySerializer;
If you want a full array without any objects, set the `recursive` configuration into the `ArraySerializer` to parse all objects recursively into arrays.
29
+
30
+
```php
31
+
use Art4\JsonApiClient\Serializer\ArraySerializer;
32
+
33
+
$serializer = new ArraySerializer(['recursive' => true]);
34
+
$array = $serializer->serialize($document);
35
+
36
+
var_dump($array);
37
+
```
38
+
39
+
This returns:
40
+
41
+
```php
42
+
array(1) {
43
+
["meta"] => array(1) {
44
+
["info"] => string(28) "Testing the JsonApiClient library."
0 commit comments