DocModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Aws\Api;
  3. /**
  4. * Encapsulates the documentation strings for a given service-version and
  5. * provides methods for extracting the desired parts related to a service,
  6. * operation, error, or shape (i.e., parameter).
  7. */
  8. class DocModel
  9. {
  10. /** @var array */
  11. private $docs;
  12. /**
  13. * @param array $docs
  14. *
  15. * @throws \RuntimeException
  16. */
  17. public function __construct(array $docs)
  18. {
  19. if (!extension_loaded('tidy')) {
  20. throw new \RuntimeException('The "tidy" PHP extension is required.');
  21. }
  22. $this->docs = $docs;
  23. }
  24. /**
  25. * Convert the doc model to an array.
  26. *
  27. * @return array
  28. */
  29. public function toArray()
  30. {
  31. return $this->docs;
  32. }
  33. /**
  34. * Retrieves documentation about the service.
  35. *
  36. * @return null|string
  37. */
  38. public function getServiceDocs()
  39. {
  40. return isset($this->docs['service']) ? $this->docs['service'] : null;
  41. }
  42. /**
  43. * Retrieves documentation about an operation.
  44. *
  45. * @param string $operation Name of the operation
  46. *
  47. * @return null|string
  48. */
  49. public function getOperationDocs($operation)
  50. {
  51. return isset($this->docs['operations'][$operation])
  52. ? $this->docs['operations'][$operation]
  53. : null;
  54. }
  55. /**
  56. * Retrieves documentation about an error.
  57. *
  58. * @param string $error Name of the error
  59. *
  60. * @return null|string
  61. */
  62. public function getErrorDocs($error)
  63. {
  64. return isset($this->docs['shapes'][$error]['base'])
  65. ? $this->docs['shapes'][$error]['base']
  66. : null;
  67. }
  68. /**
  69. * Retrieves documentation about a shape, specific to the context.
  70. *
  71. * @param string $shapeName Name of the shape.
  72. * @param string $parentName Name of the parent/context shape.
  73. * @param string $ref Name used by the context to reference the shape.
  74. *
  75. * @return null|string
  76. */
  77. public function getShapeDocs($shapeName, $parentName, $ref)
  78. {
  79. if (!isset($this->docs['shapes'][$shapeName])) {
  80. return '';
  81. }
  82. $result = '';
  83. $d = $this->docs['shapes'][$shapeName];
  84. if (isset($d['refs']["{$parentName}\$${ref}"])) {
  85. $result = $d['refs']["{$parentName}\$${ref}"];
  86. } elseif (isset($d['base'])) {
  87. $result = $d['base'];
  88. }
  89. if (isset($d['append'])) {
  90. $result .= $d['append'];
  91. }
  92. return $this->clean($result);
  93. }
  94. private function clean($content)
  95. {
  96. if (!$content) {
  97. return '';
  98. }
  99. $tidy = new \tidy();
  100. $tidy->parseString($content, [
  101. 'indent' => true,
  102. 'doctype' => 'omit',
  103. 'output-html' => true,
  104. 'show-body-only' => true,
  105. 'drop-empty-paras' => true,
  106. 'drop-font-tags' => true,
  107. 'drop-proprietary-attributes' => true,
  108. 'hide-comments' => true,
  109. 'logical-emphasis' => true
  110. ]);
  111. $tidy->cleanRepair();
  112. return (string) $content;
  113. }
  114. }