-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.php
141 lines (124 loc) · 4.89 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
use Symfony\Component\Yaml\Yaml;
require __DIR__.'/vendor/autoload.php';
$yaml = Yaml::parseFile($argv[1]);
// Defined the input paths that should be in the transformed output.
$filterPaths = [
// Patron
'/external/{agencyid}/patrons/{patronid}/v2',
'/external/{agencyid}/patrons/{patronid}/v5',
// Patron create
'/external/{agencyid}/patrons/v4',
'/external/{agencyid}/patrons/withGuardian/v1',
// Loans
'/external/{agencyid}/patrons/{patronid}/loans/v2',
'/external/{agencyid}/patrons/{patronid}/loans/renew/v2',
// Reservations
'/external/v1/{agencyid}/patrons/{patronid}/reservations/v2',
'/external/v1/{agencyid}/patrons/{patronid}/reservations',
// Fees
'/external/{agencyid}/patron/{patronid}/fees/v2',
// Branches etc.
'/external/v1/{agencyid}/branches',
'/external/{agencyid}/catalog/availability/v3',
'/external/{agencyid}/catalog/holdings/v3',
];
// Define basic output structure.
$output = [
'swagger' => $yaml['swagger'],
'info' => [
'title' => 'FBS Adapter',
'version' => $yaml['info']['version']
],
'basePath' => '/',
'paths' => [],
'securityDefinitions' => [
'oauth' => [
'type' => 'oauth2',
'description' => 'OAuth 2.0 password Grant',
'flow' => 'password',
'tokenUrl' => 'https://login.bib.dk/oauth/token',
],
],
'security' => [
[
'oauth' => [],
],
]
];
$tagsFilter = [];
$refFilter = [];
foreach ($yaml['paths'] as $path => $data) {
if (in_array($path, $filterPaths)) {
// Change '{agencyid}' path parameter to 'agencyid' string.
$path = str_replace('{agencyid}', 'agencyid', $path);
// Change '{patronid}' path parameter to 'patronid' string.
$path = str_replace('{patronid}', 'patronid', $path);
foreach ($data as $name => $op) {
// Find all used tags, so they can be set in output.
$tagsFilter[] = $op['tags'][0];
foreach ($op['parameters'] as $index => $parameter) {
// Remove '{agencyid}' parameters.
if ('agencyid' === $parameter['name']) {
unset($data[$name]['parameters'][$index]);
}
// Remove '{patronid}' parameters.
if ('patronid' === $parameter['name']) {
unset($data[$name]['parameters'][$index]);
}
// Find internal references for later filtering of definitions.
if (isset($parameter['schema']['$ref'])) {
$parts = explode('/', $parameter['schema']['$ref']);
$refFilter[] = end($parts);
}
}
// Reset index numbers for parameters as unset have change the array to associative when later converted to
// YAML.
$data[$name]['parameters'] = array_values($data[$name]['parameters']);
if (empty($data[$name]['parameters'])) {
unset($data[$name]['parameters']);
}
// Find all used references in responses, so they can be set in output.
foreach ($op['responses'] as $respons) {
if (isset($respons['schema'])) {
if (isset($respons['schema']['$ref'])) {
$parts = explode('/', $respons['schema']['$ref']);
$refFilter[] = end($parts);
} elseif (isset($respons['schema']['items']['$ref'])) {
$parts = explode('/', $respons['schema']['items']['$ref']);
$refFilter[] = end($parts);
}
}
}
}
// Added the cleaned up path description to the output.
$output['paths'][$path] = $data;
}
}
// Find definitions that reference other definitions.
array_walk($yaml['definitions'], function ($definition) use (&$refFilter) {
foreach ($definition['properties'] as $property) {
if (isset($property['$ref'])) {
$parts = explode('/', $property['$ref']);
$refFilter[] = end($parts);
}
if (isset($property['items']['$ref'])) {
$parts = explode('/', $property['items']['$ref']);
$refFilter[] = end($parts);
}
}
});
// Only output tags used by the paths in the output.
$tagsFilter = array_unique($tagsFilter);
$tags = array_filter($yaml['tags'], function ($tag) use ($tagsFilter) {
return in_array($tag['name'], $tagsFilter);
});
$output['tags'] = array_values($tags);
// Only output definitions used via references in the output.
$refFilter = array_unique($refFilter);
$output['definitions'] = array_filter($yaml['definitions'], function ($name) use ($refFilter) {
return in_array($name, $refFilter);
}, ARRAY_FILTER_USE_KEY);
// Write the array as YAML to file.
$yaml = Yaml::dump($output, 12);
file_put_contents($argv[2], $yaml);