-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_rest_meta.php
More file actions
executable file
·167 lines (134 loc) · 3.92 KB
/
php_rest_meta.php
File metadata and controls
executable file
·167 lines (134 loc) · 3.92 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
<?php
// Metadata for the argument
class MetaArg {
public $name;
public $isMandatory;
public $desc;
public function __construct() {
$args = func_get_args();
$this->name = $args[0];
$this->isMandatory = $args[1];
$this->desc = $args[2];
}
}
// Metadata for a general call
class MetaCall {
public $path;
public $pathArgs = array();
public $desc;
}
// Metadata for GET call
class MetaGetCall extends MetaCall {
public $args = array();
public function mandatoryArg($name, $desc) {
$this->args[] = new MetaArg($name, true, $desc);
return $this;
}
public function optionalArg($name, $desc) {
$this->args[] = new MetaArg($name, false, $desc);
return $this;
}
public function toHtml() {
echo '<table>', "\n";
// Path
echo ' <tr><td class="specTitle" colspan="2">GET ' , $this->path , '</td></tr>', "\n";
echo ' <tr><td class="specDesc" colspan="2">', $this->desc , '</td></tr>', "\n";
// Path args
if (count($this->pathArgs) > 0) {
echo ' <tr><td class="specPathArgTitle" colspan="2">Path Arguments</td></tr>', "\n";
foreach ($this->pathArgs as $arg) {
echo ' <tr><td class="specPathArgName">' , $arg->name , '</td>',
'<td class="specPathArgDesc">' , $arg->desc , '</td></tr>', "\n";
}
}
// Args
if (count($this->args) > 0) {
echo ' <tr><td class="specUrlArgTitle" colspan="2">URL Arguments</td></tr>', "\n";
foreach ($this->args as $arg) {
echo ' <tr><td class="specUrlArgName">' , $arg->name, '</td>',
'<td class="specUrlArgDesc">', $arg->isMandatory ? '[mandatory]' : '[optional]',
': ', $arg->desc , '</td></tr>', "\n";
}
}
echo '</table>', "\n";
}
}
// Metadata for POST call
class MetaPostCall extends MetaCall {
public $body;
public function toHtml() {
echo '<table>', "\n";
// Path
echo ' <tr><td class="specTitle" colspan="2">POST ' , $this->path , '</td></tr>', "\n";
echo ' <tr><td class="specDesc" colspan="2">', $this->desc , '</td></tr>', "\n";
// Path args
if (count($this->pathArgs) > 0) {
echo ' <tr><td class="specPathArgTitle" colspan="2">Path Arguments</td></tr>', "\n";
foreach ($this->pathArgs as $arg) {
echo ' <tr><td class="specPathArgName">' , $arg->name , '</td>',
'<td class="specPathArgDesc">' , $arg->desc , '</td></tr>', "\n";
}
}
echo ' <tr><td class="specBodyTitle" colspan="2">Body</td></tr>', "\n";
echo ' <tr><td colspan="2"><pre class="specBodyText">' , $this->body , '</pre></td></tr>', "\n";
echo '</table>', "\n";
}
}
// Helper method holding metata
class Meta {
public static function postProcessPost($path, $args) {
// Filer and validate
$result = array();
$pathArgs = array();
foreach($args as $arg) {
if ($arg instanceof MetaArg) {
$pathArgs[] = $arg;
} else if ($arg instanceof MetaPostCall) {
$result[] = $arg;
} else {
throw new InternalErrorException("Unsupported type " . $arg);
}
}
// Backfill
foreach($result as $arg) {
$arg->path = $path;
$arg->pathArgs = $pathArgs;
}
return $result;
}
public static function postProcessGet($path, $args) {
// Filer and validate
$result = array();
$pathArgs = array();
foreach($args as $arg) {
if ($arg instanceof MetaArg) {
$pathArgs[] = $arg;
} else if ($arg instanceof MetaGetCall) {
$result[] = $arg;
} else {
throw new InternalErrorException("Unsupported type " . $arg);
}
}
// Backfill
foreach($result as $arg) {
$arg->path = $path;
$arg->pathArgs = $pathArgs;
}
return $result;
}
public static function pathArg($name, $desc) {
return new MetaArg($name, true, $desc);
}
public static function getCall($desc) {
$d = new MetaGetCall();
$d->desc = $desc;
return $d;
}
public static function postCall($body, $desc) {
$d = new MetaPostCall();
$d->body = $body;
$d->desc = $desc;
return $d;
}
}
?>