-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.php
123 lines (94 loc) · 3.15 KB
/
convert.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
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
<?php
$path = $argv[1];
$section = $argv[2];
function getSections($path) {
$endpoints = file_get_contents($path);
$endpoints = json_decode($endpoints, true);
$sections = current($endpoints);
$foo = array();
foreach ($sections as $s) {
$foo[strtolower($s['name'])] = $s['methods'];
}
return $foo;
}
$sections = getSections($path);
$section = $sections[strtolower($section)];
$endpoints = $section;
//$endpoints = array_slice($section, 0, 4);
$service = array(
'operations' => array(
),
);
foreach ($endpoints as $endpoint) {
$name = str_replace(' ', '', $endpoint['MethodName']);
$uri = $endpoint['URI'];
if (strtoupper($endpoint['HTTPMethod']) === 'GET') {
$name = 'Get';
if (preg_match_all('~/([^:/]+)~', $uri, $matches)) {
$matches = $matches[1];
foreach ($matches as $match) {
if (false !== strpos($match, '-')) {
$name .= implode(array_map('ucwords', explode('-', $match)));
} else {
$name .= ucwords($match);
}
}
}
if (preg_match('~:[^/]*$~', $uri) && $name[strlen($name) - 1] === 's') {
$name = substr($name, 0, -1);
}
}
if (false !== strpos($uri, ':')) {
$uri = preg_replace('~:([^/]+)~', '{$1}', $uri);
}
$service['operations'][$name] = array(
'httpMethod' => strtoupper($endpoint['HTTPMethod']),
'uri' => $uri,
'summary' => $endpoint['Synopsis'],
);
$params = array();
if (preg_match_all('~\{([^\}]+)\}~', $uri, $matches)) {
$matches = $matches[1];
foreach ($matches as $match) {
$params[$match] = array(
'location' => 'uri',
'required' => true,
);
}
}
if (!empty($endpoint['parameters'])) {
foreach ($endpoint['parameters'] as $param) {
$type = $param['Type'];
$insert = array(
'type' => $type,
'required' => $param['Required'] === 'Y',
'description' => $param['Description'],
);
if (strlen($param['Default']) != 0) {
$insert['default'] = $param['Default'];
}
if ($type === 'enumerated') {
$insert['type'] = 'string';
$insert['enum'] = array_values($param['EnumeratedList']);
}
if (in_array($endpoint['HTTPMethod'], array('POST', 'PUT'))) {
$insert['location'] = 'json';
} else {
$insert['location'] = 'query';
}
if ($type === 'boolean') {
$insert['type'] = 'integer';
$insert['enum'] = array("0", "1");
}
if (isset($params[$param['Name']])) {
$params[$param['Name']] = array_replace($insert, $params[$param['Name']]);
} else {
$params[$param['Name']] = $insert;
}
}
}
if (!empty($params)) {
$service['operations'][$name]['parameters'] = $params;
}
}
echo json_encode($service) . PHP_EOL;