1 | <?php |
||
10 | class PostmanCollectionWriter |
||
11 | { |
||
12 | /** |
||
13 | * @var Collection |
||
14 | */ |
||
15 | private $routeGroups; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $baseUrl; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $protocol; |
||
26 | |||
27 | /** |
||
28 | * @var array|null |
||
29 | */ |
||
30 | private $auth; |
||
31 | |||
32 | /** |
||
33 | * CollectionWriter constructor. |
||
34 | * |
||
35 | * @param Collection $routeGroups |
||
36 | */ |
||
37 | public function __construct(Collection $routeGroups, $baseUrl) |
||
44 | |||
45 | public function getCollection() |
||
46 | { |
||
47 | $collection = [ |
||
48 | 'variables' => [], |
||
49 | 'info' => [ |
||
50 | 'name' => config('apidoc.postman.name') ?: config('app.name').' API', |
||
51 | '_postman_id' => Uuid::uuid4()->toString(), |
||
52 | 'description' => config('apidoc.postman.description') ?: '', |
||
53 | 'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json', |
||
54 | ], |
||
55 | 'item' => $this->routeGroups->map(function (Collection $routes, $groupName) { |
||
56 | return [ |
||
57 | 'name' => $groupName, |
||
58 | 'description' => $routes->first()['metadata']['groupDescription'], |
||
59 | 'item' => $routes->map(\Closure::fromCallable([$this, 'generateEndpointItem']))->toArray(), |
||
60 | ]; |
||
61 | })->values()->toArray(), |
||
62 | ]; |
||
63 | |||
64 | if (! empty($this->auth)) { |
||
65 | $collection['auth'] = $this->auth; |
||
66 | } |
||
67 | |||
68 | return json_encode($collection, JSON_PRETTY_PRINT); |
||
69 | } |
||
70 | |||
71 | protected function generateEndpointItem($route) |
||
72 | { |
||
73 | $mode = 'raw'; |
||
74 | |||
75 | $method = $route['methods'][0]; |
||
76 | |||
77 | return [ |
||
78 | 'name' => $route['metadata']['title'] != '' ? $route['metadata']['title'] : $route['uri'], |
||
79 | 'request' => [ |
||
80 | 'url' => $this->makeUrlData($route), |
||
81 | 'method' => $method, |
||
82 | 'header' => $this->resolveHeadersForRoute($route), |
||
83 | 'body' => [ |
||
84 | 'mode' => $mode, |
||
85 | $mode => json_encode($route['cleanBodyParameters'], JSON_PRETTY_PRINT), |
||
86 | ], |
||
87 | 'description' => $route['metadata']['description'] ?? null, |
||
88 | 'response' => [], |
||
89 | ], |
||
90 | ]; |
||
91 | } |
||
92 | |||
93 | protected function resolveHeadersForRoute($route) |
||
116 | |||
117 | protected function makeUrlData($route) |
||
159 | |||
160 | protected function getAuthHeader() |
||
182 | } |
||
183 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.