-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbootstrap_examples.php
97 lines (82 loc) · 1.68 KB
/
bootstrap_examples.php
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
<?php
ini_set('display_errors', 1);
error_reporting(-1);
require '../vendor/autoload.php';
class ExampleDataset {
private static $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,
],
],
];
public static function getRecord($type, $id) {
if (!isset(self::$records[$type][$id])) {
throw new Exception('sorry, we have a limited dataset');
}
return self::$records[$type][$id];
}
public static function getEntity($type, $id) {
$record = self::getRecord($type, $id);
$user = new ExampleUser($id);
foreach ($record as $key => $value) {
$user->$key = $value;
}
return $user;
}
public static function findRecords($type) {
return self::$records[$type];
}
public static function findEntities($type) {
$records = self::findRecords($type);
$entities = [];
foreach ($records as $id => $record) {
$entities[$id] = self::getEntity($type, $id);
}
return $entities;
}
}
class ExampleUser {
public $id;
public $name;
public $heads;
public $unknown;
public function __construct($id) {
$this->id = $id;
}
function getCurrentLocation() {
return 'Earth';
}
}