| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Api; |
| 4 |
|
| 5 |
/** |
| 6 |
* Represents a timestamp shape. |
| 7 |
*/ |
| 8 |
class TimestampShape extends Shape |
| 9 |
{ |
| 10 |
public function __construct(array $definition, ShapeMap $shapeMap) |
| 11 |
{ |
| 12 |
$definition['type'] = 'timestamp'; |
| 13 |
parent::__construct($definition, $shapeMap); |
| 14 |
} |
| 15 |
/** |
| 16 |
* Formats a timestamp value for a service. |
| 17 |
* |
| 18 |
* @param mixed $value Value to format |
| 19 |
* @param string $format Format used to serialize the value |
| 20 |
* |
| 21 |
* @return int|string |
| 22 |
* @throws \UnexpectedValueException if the format is unknown. |
| 23 |
* @throws \InvalidArgumentException if the value is an unsupported type. |
| 24 |
*/ |
| 25 |
public static function format($value, $format) |
| 26 |
{ |
| 27 |
if ($value instanceof \DateTimeInterface) { |
| 28 |
$value = $value->getTimestamp(); |
| 29 |
} elseif (\is_string($value)) { |
| 30 |
$value = \strtotime($value); |
| 31 |
} elseif (!\is_int($value)) { |
| 32 |
throw new \InvalidArgumentException('Unable to handle the provided' . ' timestamp type: ' . \gettype($value)); |
| 33 |
} |
| 34 |
switch ($format) { |
| 35 |
case 'iso8601': |
| 36 |
return \gmdate('Y-m-d\\TH:i:s\\Z', $value); |
| 37 |
case 'rfc822': |
| 38 |
return \gmdate('D, d M Y H:i:s \\G\\M\\T', $value); |
| 39 |
case 'unixTimestamp': |
| 40 |
return $value; |
| 41 |
default: |
| 42 |
throw new \UnexpectedValueException('Unknown timestamp format: ' . $format); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|