Completed
Push — master ( 138e24...047e0d )
by Marcel
06:22
created

CollectionWriter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Postman;
4
5
use Illuminate\Support\Collection;
6
use Ramsey\Uuid\Uuid;
7
8
class CollectionWriter
9
{
10
    /**
11
     * @var Collection
12
     */
13
    private $routeGroups;
14
15
    /**
16
     * CollectionWriter constructor.
17
     * @param Collection $routeGroups
18
     */
19
    public function __construct(Collection $routeGroups)
20
    {
21
        $this->routeGroups = $routeGroups;
22
    }
23
24
    public function getCollection()
25
    {
26
        $collection = [
27
            'variables' => [],
28
            'info' => [
29
                'name' => '',
30
                '_postman_id' => Uuid::uuid1()->toString(),
31
                'description' => '',
32
                'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json'
33
            ],
34
            'item' => $this->routeGroups->map(function ($routes, $groupName) {
35
                return [
36
                    'name' => $groupName,
37
                    'description' => '',
38
                    'item' => $routes->map(function ($route) {
39
                        return [
40
                            'name' => $route['title'] != '' ? $route['title'] : url($route['uri']),
41
                            'request' => [
42
                                'url' => url($route['uri']),
43
                                'method' => $route['methods'][0],
44
                                'body' => [
45
                                    'mode' => 'formdata',
46
                                    'formdata' => collect($route['parameters'])->map(function ($parameter, $key) {
47
                                        return [
48
                                            'key' => $key,
49
                                            'value' => isset($parameter['value']) ? $parameter['value'] : '',
50
                                            'type' => 'text',
51
                                            'enabled' => true
52
                                        ];
53
                                    })->values()->toArray(),
54
                                ],
55
                                'description' => $route['description'],
56
                                'response' => []
57
                            ]
58
                        ];
59
                    })->toArray()
60
                ];
61
            })->values()->toArray()
62
        ];
63
64
        return json_encode($collection);
65
    }
66
67
}