| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Api\Parser; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Api\DateTimeResult; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\Api\Shape; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\Api\StructureShape; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\Result; |
| 9 |
use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 10 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface; |
| 11 |
/** |
| 12 |
* @internal |
| 13 |
*/ |
| 14 |
abstract class AbstractRestParser extends AbstractParser |
| 15 |
{ |
| 16 |
use PayloadParserTrait; |
| 17 |
/** |
| 18 |
* Parses a payload from a response. |
| 19 |
* |
| 20 |
* @param ResponseInterface $response Response to parse. |
| 21 |
* @param StructureShape $member Member to parse |
| 22 |
* @param array $result Result value |
| 23 |
* |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
protected abstract function payload(ResponseInterface $response, StructureShape $member, array &$result); |
| 27 |
public function __invoke(CommandInterface $command, ResponseInterface $response) |
| 28 |
{ |
| 29 |
$output = $this->api->getOperation($command->getName())->getOutput(); |
| 30 |
$result = []; |
| 31 |
if ($payload = $output['payload']) { |
| 32 |
$this->extractPayload($payload, $output, $response, $result); |
| 33 |
} |
| 34 |
foreach ($output->getMembers() as $name => $member) { |
| 35 |
switch ($member['location']) { |
| 36 |
case 'header': |
| 37 |
$this->extractHeader($name, $member, $response, $result); |
| 38 |
break; |
| 39 |
case 'headers': |
| 40 |
$this->extractHeaders($name, $member, $response, $result); |
| 41 |
break; |
| 42 |
case 'statusCode': |
| 43 |
$this->extractStatus($name, $response, $result); |
| 44 |
break; |
| 45 |
} |
| 46 |
} |
| 47 |
if (!$payload && $response->getBody()->getSize() > 0 && \count($output->getMembers()) > 0) { |
| 48 |
// if no payload was found, then parse the contents of the body |
| 49 |
$this->payload($response, $output, $result); |
| 50 |
} |
| 51 |
return new Result($result); |
| 52 |
} |
| 53 |
private function extractPayload($payload, StructureShape $output, ResponseInterface $response, array &$result) |
| 54 |
{ |
| 55 |
$member = $output->getMember($payload); |
| 56 |
if (!empty($member['eventstream'])) { |
| 57 |
$result[$payload] = new EventParsingIterator($response->getBody(), $member, $this); |
| 58 |
} else { |
| 59 |
if ($member instanceof StructureShape) { |
| 60 |
// Structure members parse top-level data into a specific key. |
| 61 |
$result[$payload] = []; |
| 62 |
$this->payload($response, $member, $result[$payload]); |
| 63 |
} else { |
| 64 |
// Streaming data is just the stream from the response body. |
| 65 |
$result[$payload] = $response->getBody(); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
/** |
| 70 |
* Extract a single header from the response into the result. |
| 71 |
*/ |
| 72 |
private function extractHeader($name, Shape $shape, ResponseInterface $response, &$result) |
| 73 |
{ |
| 74 |
$value = $response->getHeaderLine($shape['locationName'] ?: $name); |
| 75 |
switch ($shape->getType()) { |
| 76 |
case 'float': |
| 77 |
case 'double': |
| 78 |
$value = (float) $value; |
| 79 |
break; |
| 80 |
case 'long': |
| 81 |
$value = (int) $value; |
| 82 |
break; |
| 83 |
case 'boolean': |
| 84 |
$value = \filter_var($value, \FILTER_VALIDATE_BOOLEAN); |
| 85 |
break; |
| 86 |
case 'blob': |
| 87 |
$value = \base64_decode($value); |
| 88 |
break; |
| 89 |
case 'timestamp': |
| 90 |
try { |
| 91 |
$value = DateTimeResult::fromTimestamp($value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null); |
| 92 |
break; |
| 93 |
} catch (\Exception $e) { |
| 94 |
// If the value cannot be parsed, then do not add it to the |
| 95 |
// output structure. |
| 96 |
return; |
| 97 |
} |
| 98 |
case 'string': |
| 99 |
try { |
| 100 |
if ($shape['jsonvalue']) { |
| 101 |
$value = $this->parseJson(\base64_decode($value), $response); |
| 102 |
} |
| 103 |
// If value is not set, do not add to output structure. |
| 104 |
if (!isset($value)) { |
| 105 |
return; |
| 106 |
} |
| 107 |
break; |
| 108 |
} catch (\Exception $e) { |
| 109 |
//If the value cannot be parsed, then do not add it to the |
| 110 |
//output structure. |
| 111 |
return; |
| 112 |
} |
| 113 |
} |
| 114 |
$result[$name] = $value; |
| 115 |
} |
| 116 |
/** |
| 117 |
* Extract a map of headers with an optional prefix from the response. |
| 118 |
*/ |
| 119 |
private function extractHeaders($name, Shape $shape, ResponseInterface $response, &$result) |
| 120 |
{ |
| 121 |
// Check if the headers are prefixed by a location name |
| 122 |
$result[$name] = []; |
| 123 |
$prefix = $shape['locationName']; |
| 124 |
$prefixLen = $prefix !== null ? \strlen($prefix) : 0; |
| 125 |
foreach ($response->getHeaders() as $k => $values) { |
| 126 |
if (!$prefixLen) { |
| 127 |
$result[$name][$k] = \implode(', ', $values); |
| 128 |
} elseif (\stripos($k, $prefix) === 0) { |
| 129 |
$result[$name][\substr($k, $prefixLen)] = \implode(', ', $values); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
/** |
| 134 |
* Places the status code of the response into the result array. |
| 135 |
*/ |
| 136 |
private function extractStatus($name, ResponseInterface $response, array &$result) |
| 137 |
{ |
| 138 |
$result[$name] = (int) $response->getStatusCode(); |
| 139 |
} |
| 140 |
} |
| 141 |
|