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