TimestampShape.php 1.4 KB

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