forked from ShootProof/recruiting-full-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetData.php
More file actions
161 lines (128 loc) · 3.38 KB
/
getData.php
File metadata and controls
161 lines (128 loc) · 3.38 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
<?php
class Thumbnail {
/** @var string $description */
public $description;
/** @var string $href */
public $href;
/**
* @param string $description
* @param string $href
*/
public function __construct(string $description, string $href) {
$this->description = $description;
$this->href = $href;
}
}
class Node {
/** @var int $id */
public $id;
/** @var string $name */
public $name;
/** @var Thumbnail $thumbnail */
public $thumbnail;
/** @var Node[]|null $children */
public $children;
/** @var bool $collapsed */
public $collapsed;
/** @var int|null $parentId */
private $parentId;
/**
* @param int $id
* @param string $name
* @param Thumbnail $thumbnail
* @param bool $collapsed
* @param int|null $parentId
*/
public function __construct(int $id, string $name, Thumbnail $thumbnail, $collapsed, $parentId = null) {
$this->id = $id;
$this->name = $name;
$this->thumbnail = $thumbnail;
$this->collapsed = $collapsed;
$this->parentId = $parentId;
}
/**
* @param Node $node
*/
public function addChild(Node $node): void {
$this->children[] = $node;
}
/**
* @return bool
*/
public function isChild(): bool {
return $this->parentId !== null;
}
/**
* @return int|null
*/
public function getParentId(): ?int {
return $this->parentId;
}
}
class Setting {
/** @var int $id */
public $id;
/** @var string $name */
public $name;
/** @var mixed $value */
public $value;
/**
* @param string $name
* @param mixed $value
*/
public function __construct(int $id, string $name, $value) {
$this->id = $id;
$this->name = $name;
$this->value = $value;
}
}
class GetDataResponse {
/** @var Node[] $nodes */
public $nodes;
/** @var Setting[] $settings */
public $settings;
/**
* @param Node[] $nodes
* @param Setting[] $settings
*/
public function __construct(array $nodes, array $settings) {
$this->nodes = $nodes;
$this->settings = $settings;
}
}
/**
* @param Node[] $nodes
* @param Node $node
*/
function assignToParent(array $nodes, Node $node): void {
foreach ($nodes as $parent) {
if ($parent->id === $node->getParentId()) {
$parent->children[] = $node;
break;
} else if ($parent->children) {
assignToParent($parent->children, $node);
}
}
}
/** @var array $testData */
$testData = json_decode(file_get_contents("testdata.json"));
/** @var Node[] $nodes */
$nodes = [];
/** @var Setting[] $settings */
$settings = [];
foreach ($testData->nodes as $node) {
$thumbnail = new Thumbnail($node->thumbnail->description, $node->thumbnail->href);
$collapsed = $node->collapsed ?? false;
$newNode = new Node($node->id, $node->name, $thumbnail, $collapsed, $node->parent);
if ($newNode->isChild()) {
assignToParent($nodes, $newNode);
continue;
}
$nodes[] = $newNode;
}
foreach ($testData->settings as $setting) {
$settings[] = new Setting($setting->id, $setting->name, $setting->value);
}
$getDataResponse = new GetDataResponse($nodes, $settings);
header('Content-Type: application/json');
echo json_encode($getDataResponse);