| 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\Psr\Http\Message\ResponseInterface; |
| 8 |
trait MetadataParserTrait |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Extract a single header from the response into the result. |
| 12 |
*/ |
| 13 |
protected function extractHeader($name, Shape $shape, ResponseInterface $response, &$result) |
| 14 |
{ |
| 15 |
$value = $response->getHeaderLine($shape['locationName'] ?: $name); |
| 16 |
// Empty values should not be deserialized |
| 17 |
if ($value === null || $value === '') { |
| 18 |
return; |
| 19 |
} |
| 20 |
switch ($shape->getType()) { |
| 21 |
case 'float': |
| 22 |
case 'double': |
| 23 |
$value = (float) $value; |
| 24 |
break; |
| 25 |
case 'long': |
| 26 |
case 'integer': |
| 27 |
$value = (int) $value; |
| 28 |
break; |
| 29 |
case 'boolean': |
| 30 |
$value = \filter_var($value, \FILTER_VALIDATE_BOOLEAN); |
| 31 |
break; |
| 32 |
case 'blob': |
| 33 |
$value = \base64_decode($value); |
| 34 |
break; |
| 35 |
case 'timestamp': |
| 36 |
try { |
| 37 |
$value = DateTimeResult::fromTimestamp($value, !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null); |
| 38 |
break; |
| 39 |
} catch (\Exception $e) { |
| 40 |
// If the value cannot be parsed, then do not add it to the |
| 41 |
// output structure. |
| 42 |
return; |
| 43 |
} |
| 44 |
case 'string': |
| 45 |
if ($shape['jsonvalue']) { |
| 46 |
$value = $this->parseJson(\base64_decode($value), $response); |
| 47 |
} |
| 48 |
break; |
| 49 |
} |
| 50 |
$result[$name] = $value; |
| 51 |
} |
| 52 |
/** |
| 53 |
* Extract a map of headers with an optional prefix from the response. |
| 54 |
*/ |
| 55 |
protected function extractHeaders($name, Shape $shape, ResponseInterface $response, &$result) |
| 56 |
{ |
| 57 |
// Check if the headers are prefixed by a location name |
| 58 |
$result[$name] = []; |
| 59 |
$prefix = $shape['locationName']; |
| 60 |
$prefixLen = \strlen($prefix); |
| 61 |
foreach ($response->getHeaders() as $k => $values) { |
| 62 |
if (!$prefixLen) { |
| 63 |
$result[$name][$k] = \implode(', ', $values); |
| 64 |
} elseif (\stripos($k, $prefix) === 0) { |
| 65 |
$result[$name][\substr($k, $prefixLen)] = \implode(', ', $values); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
/** |
| 70 |
* Places the status code of the response into the result array. |
| 71 |
*/ |
| 72 |
protected function extractStatus($name, ResponseInterface $response, array &$result) |
| 73 |
{ |
| 74 |
$result[$name] = (int) $response->getStatusCode(); |
| 75 |
} |
| 76 |
} |
| 77 |
|