Swagger.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Model\Rest;
  7. use Magento\Webapi\Model\Config as ModelConfig;
  8. /**
  9. * Webapi Swagger Specification Model
  10. *
  11. * @link https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md Swagger specification
  12. *
  13. * @method Swagger setHost(string $host)
  14. * @method Swagger setDefinitions(array $definitions)
  15. * @method Swagger setSchemes(array $schemes)
  16. */
  17. class Swagger extends \Magento\Framework\DataObject
  18. {
  19. /**
  20. * Swagger specification version
  21. */
  22. const SWAGGER_VERSION = '2.0';
  23. /**
  24. * Constructor
  25. */
  26. public function __construct()
  27. {
  28. $data = [
  29. 'swagger' => self::SWAGGER_VERSION,
  30. 'info' => [
  31. 'version' => '',
  32. 'title' => '',
  33. ],
  34. ];
  35. parent::__construct($data);
  36. }
  37. /**
  38. * Add a path
  39. *
  40. * @param string $path
  41. * @param string $httpOperation
  42. * @param string[] $pathInfo
  43. * @return $this
  44. */
  45. public function addPath($path, $httpOperation, $pathInfo)
  46. {
  47. $this->_data['paths'][$path][$httpOperation] = $pathInfo;
  48. return $this;
  49. }
  50. /**
  51. * Add a tag
  52. *
  53. * @param string $tagInfo
  54. * @return $this
  55. */
  56. public function addTag($tagInfo)
  57. {
  58. $this->_data['tags'][] = $tagInfo;
  59. return $this;
  60. }
  61. /**
  62. * Get JSON encoded REST schema
  63. *
  64. * @return string
  65. */
  66. public function toSchema()
  67. {
  68. return json_encode($this->toArray(), JSON_UNESCAPED_SLASHES);
  69. }
  70. /**
  71. * Add Info section data
  72. *
  73. * @param array $info
  74. * @return $this
  75. */
  76. public function setInfo($info)
  77. {
  78. if (! is_array($info)) {
  79. return $this;
  80. }
  81. if (isset($info['title'])) {
  82. $this->_data['info']['title'] = $info['title'];
  83. }
  84. if (isset($info['version'])) {
  85. $this->_data['info']['version'] = $info['version'];
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Set base path
  91. *
  92. * @param string $basePath
  93. * @return $this
  94. */
  95. public function setBasePath($basePath)
  96. {
  97. $this->_data['basePath'] = $basePath;
  98. return $this;
  99. }
  100. }