Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/TimestampShape.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api;
3
4 /**
5 * Represents a timestamp shape.
6 */
7 class TimestampShape extends Shape
8 {
9 public function __construct(array $definition, ShapeMap $shapeMap)
10 {
11 $definition['type'] = 'timestamp';
12 parent::__construct($definition, $shapeMap);
13 }
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) && !is_float($value)) {
32 throw new \InvalidArgumentException('Unable to handle the provided'
33 . ' timestamp type: ' . gettype($value));
34 }
35
36 switch ($format) {
37 case 'iso8601':
38 return gmdate('Y-m-d\TH:i:s\Z', (int) $value);
39 case 'rfc822':
40 return gmdate('D, d M Y H:i:s \G\M\T', (int) $value);
41 case 'unixTimestamp':
42 return $value;
43 default:
44 throw new \UnexpectedValueException('Unknown timestamp format: '
45 . $format);
46 }
47 }
48 }