CollectionWriter.php 2.3 KB

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