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
| <?php
use DOMDocument;
use DOMXPath;
// APIドキュメント読込
$html = file_get_contents('default.html');
$dom = new DOMDocument('1.0', 'UTF-8');
$html = mb_convert_encoding($html, "HTML-ENTITIES", 'auto');
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// 各APIを取得
$content = $xpath->query('//div[contains(@class, "action")]');
$apis = [];
foreach($content as $ivalue){
// HTML読込
$idom = new DOMDocument('1.0', 'UTF-8');
$html = mb_convert_encoding($ivalue->ownerDocument->saveXML($ivalue), "HTML-ENTITIES", 'auto');
@$idom->loadHTML($html);
$ixpath = new DOMXPath($idom);
// method
$method = $ixpath->query('//a[contains(@class, "method")]');
// uri
$uri = $ixpath->query('//span[contains(@class, "uri")]');
// description
$description = $ixpath->query('//h4/following-sibling::ul');
$descriptionStrs = [];
foreach($description as $value){
$descriptionStrs[] = $value->nodeValue;
}
// Request
$requestXpath = '//div[contains(@class, "title") and contains(*, "Request")]';
$requestElements = $ixpath->query($requestXpath);
$tempRequests = [];
foreach($requestElements as $key => $elements){
$i = $key + 1;
// Header
$requestHeaderElem = $ixpath->query($requestXpath . '[' . $i . ']/following::div/div/pre');
$requestHeader = null;
if(!is_null($requestHeaderElem[0] ?? null)){
// Headerを各行に分解
$requestHeader = $requestHeaderElem[0]->ownerDocument->saveXML($requestHeaderElem[0]);
$requestHeader = strip_tags(str_replace(['<br>', '<br/>'], PHP_EOL, $requestHeader));
}
// Body
$requestBodyElem = $ixpath->query('//div[contains(*, "Request")]/following::div/following::pre/code');
// Requestの後のResponse数をカウント
$responseHeaderElem = $ixpath->query($requestXpath . '[' . $i . ']/following::div[contains(@class, "title") and contains(*, "Response")]');
$tempRequests[] = [
'requestHeader' => $requestHeader,
'requestBody' => $requestBodyElem[0]->nodeValue ?? null,
'beforResponseCount' => count($requestHeaderElem),
];
}
// Response
$response = [];
// Status
$responseStatus = $ixpath->query('//div[contains(*, "Response")]/strong/code');
foreach($responseStatus as $key => $elements){
// Header
$responseHederElem = $ixpath->query('//div[contains(*, "Response")]/following::div[' . $key . '][contains(@class, "collapse-content")]/div[1]/pre[1]/code');
$responseHeder = null;
if(!is_null($responseHederElem[0] ?? null)){
// Headerを各行に分解
$responseHeder = $responseHederElem[0]->ownerDocument->saveXML($responseHederElem[0]);
$responseHeder = explode(PHP_EOL, strip_tags(str_replace(['<br>', '<br/>'], PHP_EOL, $responseHeder)));
}
// Body
$responseBodyElem = $ixpath->query('//div[contains(*, "Response")]/following::div[' . $key . '][contains(@class, "collapse-content")]/div[1]/pre[2]/code');
$response[] = [
'responseStatus' => $elements->nodeValue ?? null,
'responseHeder' => $responseHeder,
'responseBody' => $responseBodyElem[0]->nodeValue ?? null,
];
}
// Request に対しての Response を当て込む
$request = [];
for($i = (count($tempRequests) - 1); 0 <= $i; $i--){
$value = $tempRequests[$i];
$responseCount = $value['beforResponseCount'] - ($tempRequests[($i + 1)]['beforResponseCount'] ?? 0);
if($responseCount < 0){
$responseCount = 0;
}
$temp = [];
for($j = $responseCount; 0 < $j; $j--){
$key = $j - 1;
$temp[$response[$j]['responseStatus']] = $response[$j];
}
$request[] = [
'requestHeader' => $value['requestHeader'],
'requestBody' => $value['requestBody'],
'response' => $temp,
];
}
// GETのようにRequestがないものの対策
if(count($tempRequests) == 0){
$temp = [];
foreach($response as $value){
$temp[$value['responseStatus']] = $value;;
}
$request[] = [
'requestHeader' => null,
'requestBody' => null,
'response' => $temp,
];
}
// API毎に整理
$apis[] = [
'method' => $method[0]->nodeValue ?? null,
'uri' => $uri[0]->nodeValue ?? null,
'description' => implode('', $descriptionStrs),
'request' => $request,
];
}
var_dump($apis);
|