Skip to content

Commit 30de4fc

Browse files
committed
Update attribute representation to latest spec
1 parent 2de2699 commit 30de4fc

File tree

4 files changed

+130
-21
lines changed

4 files changed

+130
-21
lines changed

README.md

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ will output
5656
"data": {
5757
"type": "people",
5858
"id": "123",
59-
"first_name": "John",
60-
"last_name": "Dow",
59+
"attributes": {
60+
"first_name": "John",
61+
"last_name": "Dow"
62+
},
6163
"links": {
6264
"self": "http:\/\/example.com\/people\/123"
6365
}
@@ -110,7 +112,9 @@ will output
110112
"data": {
111113
"type": "sites",
112114
"id": "1",
113-
"name": "JSON API Samples",
115+
"attributes": {
116+
"name": "JSON API Samples"
117+
},
114118
"links": {
115119
"self": "http:\/\/example.com\/sites\/1",
116120
"posts": {
@@ -125,13 +129,17 @@ will output
125129
{
126130
"type": "people",
127131
"id": "123",
128-
"first_name": "John",
129-
"last_name": "Dow"
132+
"attributes": {
133+
"first_name": "John",
134+
"last_name": "Dow"
135+
}
130136
},
131137
{
132138
"type": "comments",
133139
"id": "456",
134-
"body": "Included objects work as easy as basic ones",
140+
"attributes": {
141+
"body": "Included objects work as easy as basic ones"
142+
},
135143
"links": {
136144
"self": "http:\/\/example.com\/comments\/456",
137145
"author": {
@@ -145,7 +153,9 @@ will output
145153
{
146154
"type": "comments",
147155
"id": "789",
148-
"body": "Let's try!",
156+
"attributes": {
157+
"body": "Let's try!"
158+
},
149159
"links": {
150160
"self": "http:\/\/example.com\/comments\/789",
151161
"author": {
@@ -159,8 +169,10 @@ will output
159169
{
160170
"type": "posts",
161171
"id": "321",
162-
"title": "Included objects",
163-
"body": "Yes, it is supported",
172+
"attributes": {
173+
"title": "Included objects",
174+
"body": "Yes, it is supported"
175+
},
164176
"links": {
165177
"author": {
166178
"linkage": {
@@ -216,22 +228,20 @@ Note that output does not contain objects from neither ```posts``` nor ```posts.
216228
"data": {
217229
"type": "sites",
218230
"id": "1",
219-
"name": "JSON API Samples",
231+
"attributes": {
232+
"name": "JSON API Samples"
233+
},
220234
"links": {
221-
"self": "http:\/\/example.com\/sites\/1",
222-
"posts": {
223-
"linkage": {
224-
"type": "posts",
225-
"id": "321"
226-
}
227-
}
235+
"self": "http:\/\/example.com\/sites\/1"
228236
}
229237
},
230238
"included": [
231239
{
232240
"type": "people",
233241
"id": "123",
234-
"first_name": "John"
242+
"attributes": {
243+
"first_name": "John"
244+
}
235245
}
236246
]
237247
}
@@ -342,6 +352,68 @@ class YourSchema extends SchemaProvider
342352
}
343353
```
344354

355+
### Show top level links and meta information
356+
357+
```php
358+
$author = Author::instance('123', 'John', 'Dow');
359+
$meta = [
360+
"copyright" => "Copyright 2015 Example Corp.",
361+
"authors" => [
362+
"Yehuda Katz",
363+
"Steve Klabnik",
364+
"Dan Gebhardt"
365+
]
366+
];
367+
$links = new DocumentLinks(
368+
null,
369+
'http://example.com/people?first',
370+
'http://example.com/people?last',
371+
'http://example.com/people?prev',
372+
'http://example.com/people?next'
373+
);
374+
375+
$encoder = Encoder::instance([
376+
Author::class => AuthorSchema::class,
377+
Comment::class => CommentSchema::class,
378+
Post::class => PostSchema::class,
379+
Site::class => SiteSchema::class
380+
], new JsonEncodeOptions(JSON_PRETTY_PRINT));
381+
382+
echo $encoder->encode($author, $links, $meta) . PHP_EOL;
383+
```
384+
385+
will output
386+
387+
```json
388+
{
389+
"meta": {
390+
"copyright": "Copyright 2015 Example Corp.",
391+
"authors": [
392+
"Yehuda Katz",
393+
"Steve Klabnik",
394+
"Dan Gebhardt"
395+
]
396+
},
397+
"links": {
398+
"first": "http:\/\/example.com\/people?first",
399+
"last": "http:\/\/example.com\/people?last",
400+
"prev": "http:\/\/example.com\/people?prev",
401+
"next": "http:\/\/example.com\/people?next"
402+
},
403+
"data": {
404+
"type": "people",
405+
"id": "123",
406+
"attributes": {
407+
"first_name": "John",
408+
"last_name": "Dow"
409+
},
410+
"links": {
411+
"self": "http:\/\/example.com\/people\/123"
412+
}
413+
}
414+
}
415+
```
416+
345417
## Questions?
346418

347419
Do not hesitate to contact us on [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/neomerx/json-api) or post an [issue](https://github.com/neomerx/json-api/issues).

sample/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ will output
1717
"data": {
1818
"type": "people",
1919
"id": "123",
20-
"first_name": "John",
21-
"last_name": "Dow",
20+
"attributes": {
21+
"first_name": "John",
22+
"last_name": "Dow"
23+
},
2224
"links": {
2325
"self": "http:\/\/example.com\/people\/123"
2426
}

sample/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
},
1717
"minimum-stability": "dev",
1818
"require": {
19-
"neomerx/json-api": "0.1"
19+
"neomerx/json-api": "0.1.1"
2020
}
2121
}

sample/sample.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,40 @@ private function showSparseAndFieldSetsExample()
102102
echo $encoder->encode($site, null, null, $options) . PHP_EOL;
103103
}
104104

105+
/**
106+
* Shows sparse and field set filters.
107+
*/
108+
private function showTopLevelMetaAndLinksExample()
109+
{
110+
echo 'Neomerx JSON API sample application (top level links and meta information)' . PHP_EOL;
111+
112+
$author = Author::instance('123', 'John', 'Dow');
113+
$meta = [
114+
"copyright" => "Copyright 2015 Example Corp.",
115+
"authors" => [
116+
"Yehuda Katz",
117+
"Steve Klabnik",
118+
"Dan Gebhardt"
119+
]
120+
];
121+
$links = new DocumentLinks(
122+
null,
123+
'http://example.com/people?first',
124+
'http://example.com/people?last',
125+
'http://example.com/people?prev',
126+
'http://example.com/people?next'
127+
);
128+
129+
$encoder = Encoder::instance([
130+
Author::class => AuthorSchema::class,
131+
Comment::class => CommentSchema::class,
132+
Post::class => PostSchema::class,
133+
Site::class => SiteSchema::class
134+
], new JsonEncodeOptions(JSON_PRETTY_PRINT));
135+
136+
echo $encoder->encode($author, $links, $meta) . PHP_EOL;
137+
}
138+
105139
/**
106140
* Run performance test.
107141
*
@@ -151,6 +185,7 @@ public function main()
151185
$this->showBasicExample();
152186
$this->showIncludedObjectsExample();
153187
$this->showSparseAndFieldSetsExample();
188+
$this->showTopLevelMetaAndLinksExample();
154189
} else {
155190
$num = $args['t'];
156191
$inv = empty($num) === true || is_numeric($num) === false || ctype_digit($num) == false || (int)$num <= 0;

0 commit comments

Comments
 (0)