-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbootstrap_examples.php
More file actions
174 lines (147 loc) · 3.84 KB
/
bootstrap_examples.php
File metadata and controls
174 lines (147 loc) · 3.84 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
<?php
declare(strict_types=1);
use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\ResourceDocument;
use alsvanzelf\jsonapi\interfaces\ExtensionInterface;
use alsvanzelf\jsonapi\interfaces\HasAttributesInterface;
use alsvanzelf\jsonapi\interfaces\HasExtensionMembersInterface;
use alsvanzelf\jsonapi\interfaces\ProfileInterface;
use alsvanzelf\jsonapi\interfaces\ResourceInterface;
use alsvanzelf\jsonapi\objects\ResourceIdentifierObject;
ini_set('display_errors', 1);
error_reporting(-1);
require_once __DIR__.'/../vendor/autoload.php';
class ExampleDataset {
/** @var array<string, array<int, array<string, string|int>>> */
private static array $records = [
'articles' => [
1 => [
'title' => 'JSON:API paints my bikeshed!',
'authorId' => 9,
],
],
'comments' => [
5 => [
'body' => 'First!',
'authorId' => 2,
],
12 => [
'body' => 'I like XML better',
'authorId' => 9,
],
],
'people' => [
9 => [
'firstName' => 'Dan',
'lastName' => 'Gebhardt',
'twitter' => 'dgeb',
],
],
'user' => [
1 => [
'name' => 'Ford Prefect',
'heads' => 1,
],
2 => [
'name' => 'Arthur Dent',
'heads' => '1, but not always there',
],
42 => [
'name' => 'Zaphod Beeblebrox',
'heads' => 2,
],
],
];
/**
* @return array<string, string|int>
*/
public static function getRecord(string $type, int $id): array {
if (isset(self::$records[$type][$id]) === false) {
throw new \Exception('sorry, we have a limited dataset');
}
return self::$records[$type][$id];
}
public static function getEntity(string $type, int $id): ExampleUser {
$record = self::getRecord($type, $id);
$user = new ExampleUser($id);
foreach ($record as $key => $value) {
$user->$key = $value; // @phpstan-ignore property.dynamicName
}
return $user;
}
/**
* @return array<array<string, string|int>>
*/
public static function findRecords(string $type): array {
return self::$records[$type];
}
/**
* @return ExampleUser[]
*/
public static function findEntities(string $type): array {
$recordIds = array_keys(self::findRecords($type));
$entities = [];
foreach ($recordIds as $id) {
$entities[$id] = self::getEntity($type, $id);
}
return $entities;
}
}
class ExampleUser {
public ?string $name = null;
public null|int|string $heads = null;
public mixed $unknown = null;
public function __construct(
public int $id,
) {}
public function getCurrentLocation(): string {
return 'Earth';
}
}
class ExampleVersionExtension implements ExtensionInterface {
/**
* the required method
*/
public function getOfficialLink(): string {
return 'https://jsonapi.org/format/1.1/#extension-rules';
}
public function getNamespace(): string {
return 'version';
}
/**
* optionally helpers for the specific extension
*/
public function setVersion(ResourceInterface & HasExtensionMembersInterface $resource, string $version): void {
if ($resource instanceof ResourceDocument) {
$resource->getResource()->addExtensionMember($this, 'id', $version);
}
else {
$resource->addExtensionMember($this, 'id', $version);
}
}
}
class ExampleTimestampsProfile implements ProfileInterface {
/**
* the required method
*/
public function getOfficialLink(): string {
return 'https://jsonapi.org/recommendations/#authoring-profiles';
}
/**
* optionally helpers for the specific profile
*/
public function setTimestamps(
ResourceInterface & HasAttributesInterface $resource,
?\DateTimeInterface $created=null,
?\DateTimeInterface $updated=null,
): void {
$timestamps = [];
if ($created !== null) {
$timestamps['created'] = $created->format(\DateTime::ISO8601);
}
if ($updated !== null) {
$timestamps['updated'] = $updated->format(\DateTime::ISO8601);
}
$resource->addAttribute('timestamps', $timestamps);
}
}