1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexWells\ApiDocsGenerator\Postman; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
class CollectionGenerator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array[] |
11
|
|
|
*/ |
12
|
|
|
private $routes; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* CollectionGenerator constructor. |
16
|
|
|
* |
17
|
|
|
* @param array[] $routes |
18
|
|
|
*/ |
19
|
|
|
public function __construct(array $routes) |
20
|
|
|
{ |
21
|
|
|
$this->routes = $routes; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Return generated collection in JSON format. |
26
|
|
|
* |
27
|
|
|
* All information is available here: https://schema.getpostman.com/json/collection/v2.0.0/docs/index.html |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getCollection() |
32
|
|
|
{ |
33
|
|
|
$collection = array_merge([ |
34
|
|
|
'info' => [ |
35
|
|
|
'name' => $this->getName(), |
36
|
|
|
'description' => $this->getDescription(), |
37
|
|
|
'version' => $this->getVersion(), |
38
|
|
|
'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json', |
39
|
|
|
] |
40
|
|
|
], $this->getFolder()); |
|
|
|
|
41
|
|
|
|
42
|
|
|
return json_encode($collection); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getFolder($name = null) { |
46
|
|
|
$folder = [ |
|
|
|
|
47
|
|
|
'name' => $name, |
48
|
|
|
'variable' => $this->getVariables($name), |
49
|
|
|
'event' => $this->getEvents($name), |
50
|
|
|
'auth' => $this->getAuth($name), |
|
|
|
|
51
|
|
|
'item' => $this->getItems($folder) |
|
|
|
|
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function getItems(array $input, array $formatted = []) { |
56
|
|
|
foreach ($input as $route) { |
57
|
|
|
$resource = $route['resource']; |
58
|
|
|
|
59
|
|
|
while (count($resource) > 1) { |
60
|
|
|
$key = array_shift($resource); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$folder = $this->getFolder($resource); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$array[array_shift($resource)] = $value; |
|
|
|
|
66
|
|
|
|
67
|
|
|
$formatted = array_merge($formatted, $array); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return []; |
71
|
|
|
/*$this->routeGroups->map(function ($routes, $groupName) { |
72
|
|
|
return [ |
73
|
|
|
'name' => $groupName, |
74
|
|
|
'description' => '', |
75
|
|
|
'item' => $routes->map(function ($route) { |
76
|
|
|
return [ |
77
|
|
|
'name' => $route['title'] != '' ? $route['title'] : url($route['uri']), |
78
|
|
|
'request' => [ |
79
|
|
|
'url' => url($route['uri']), |
80
|
|
|
'method' => $route['methods'][0], |
81
|
|
|
'body' => [ |
82
|
|
|
'mode' => 'formdata', |
83
|
|
|
'formdata' => collect($route['parameters']['query'])->map(function ($parameter) { |
84
|
|
|
return [ |
85
|
|
|
'key' => $parameter['name'], |
86
|
|
|
'value' => '', |
87
|
|
|
'type' => collect($parameter['rules'])->contains(function ($rule) { |
88
|
|
|
return in_array($rule, ['file', 'image']); |
89
|
|
|
// TODO: advanced mime checks |
90
|
|
|
}) ? 'file' : 'text', // this check is fine for now |
91
|
|
|
'enabled' => true, |
92
|
|
|
]; |
93
|
|
|
})->values()->toArray(), |
94
|
|
|
], |
95
|
|
|
'description' => $route['description'], |
96
|
|
|
'response' => [], |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
})->toArray(), |
100
|
|
|
]; |
101
|
|
|
})->values()->toArray();*/ |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get display name. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function getName() { |
110
|
|
|
return config('app.name'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get description. |
115
|
|
|
* |
116
|
|
|
* @return string|array |
117
|
|
|
*/ |
118
|
|
|
protected function getDescription() { |
119
|
|
|
return 'Automatically generated by laravel-api-docs-generator'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get version. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function getVersion() { |
128
|
|
|
return '1.0.0'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get set of variables. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
protected function getVariables(string $folderName = null) { |
|
|
|
|
137
|
|
|
return []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get set of events. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
protected function getEvents(string $folderName = null) { |
|
|
|
|
146
|
|
|
return []; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get auth. |
151
|
|
|
* |
152
|
|
|
* @return array|null |
153
|
|
|
*/ |
154
|
|
|
protected function getAuth(string $folderName = null) { |
|
|
|
|
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.