-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcollection.php
More file actions
39 lines (27 loc) · 755 Bytes
/
collection.php
File metadata and controls
39 lines (27 loc) · 755 Bytes
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
<?php
declare(strict_types=1);
use alsvanzelf\jsonapi\CollectionDocument;
use alsvanzelf\jsonapi\objects\ResourceObject;
require __DIR__.'/bootstrap_examples.php';
$users = ExampleDataset::findEntities('user');
/**
* send multiple entities, called a collection
*/
$collection = [];
foreach ($users as $user) {
$resource = ResourceObject::fromObject($user, type: 'user', id: $user->id);
if ($user->id === 42) {
$ship = new ResourceObject('ship', 5);
$ship->add('name', 'Heart of Gold');
$resource->addRelationship('ship', $ship);
}
$collection[] = $resource;
}
$document = CollectionDocument::fromResources(...$collection);
/**
* get the json
*/
$options = [
'prettyPrint' => true,
];
echo '<pre>'.$document->toJson($options);