Open Journal Systems  3.3.0
SchemaFormatter.php
1 <?php
2 
4 
6 
11 {
13  protected static $utcTimeZone;
14 
23  public static function format($format, $value)
24  {
25  switch ($format) {
26  case 'date-time':
27  return self::formatDateTime($value);
28  case 'date-time-http':
29  return self::formatDateTimeHttp($value);
30  case 'date':
31  return self::formatDate($value);
32  case 'time':
33  return self::formatTime($value);
34  case 'timestamp':
35  return self::formatTimestamp($value);
36  case 'boolean-string':
37  return self::formatBooleanAsString($value);
38  default:
39  return $value;
40  }
41  }
42 
50  public static function formatDateTime($value)
51  {
52  return self::dateFormatter($value, 'Y-m-d\TH:i:s\Z');
53  }
54 
62  public static function formatDateTimeHttp($value)
63  {
64  return self::dateFormatter($value, 'D, d M Y H:i:s \G\M\T');
65  }
66 
74  public static function formatDate($value)
75  {
76  return self::dateFormatter($value, 'Y-m-d');
77  }
78 
86  public static function formatTime($value)
87  {
88  return self::dateFormatter($value, 'H:i:s');
89  }
90 
98  public static function formatBooleanAsString($value)
99  {
100  return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
101  }
102 
110  public static function formatTimestamp($value)
111  {
112  return (int) self::dateFormatter($value, 'U');
113  }
114 
120  protected static function getUtcTimeZone()
121  {
122  // @codeCoverageIgnoreStart
123  if (!self::$utcTimeZone) {
124  self::$utcTimeZone = new \DateTimeZone('UTC');
125  }
126  // @codeCoverageIgnoreEnd
127 
128  return self::$utcTimeZone;
129  }
130 
140  protected static function dateFormatter($dateTime, $format)
141  {
142  if (is_numeric($dateTime)) {
143  return gmdate($format, (int) $dateTime);
144  }
145 
146  if (is_string($dateTime)) {
147  $dateTime = new \DateTime($dateTime);
148  }
149 
150  if ($dateTime instanceof \DateTime) {
151  return $dateTime->setTimezone(self::getUtcTimeZone())->format($format);
152  }
153 
154  throw new InvalidArgumentException('Date/Time values must be either a string, integer, or DateTime object');
155  }
156 }
Guzzle\Service\Description\SchemaFormatter
Definition: SchemaFormatter.php:10
Guzzle\Service\Description\SchemaFormatter\getUtcTimeZone
static getUtcTimeZone()
Definition: SchemaFormatter.php:120
Guzzle\Service\Description\SchemaFormatter\formatTime
static formatTime($value)
Definition: SchemaFormatter.php:86
Guzzle\Service\Description
Definition: Operation.php:3
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Service\Description\SchemaFormatter\formatTimestamp
static formatTimestamp($value)
Definition: SchemaFormatter.php:110
Guzzle\Service\Description\SchemaFormatter\$utcTimeZone
static $utcTimeZone
Definition: SchemaFormatter.php:13
Guzzle\Service\Description\SchemaFormatter\formatDate
static formatDate($value)
Definition: SchemaFormatter.php:74
Guzzle\Service\Description\SchemaFormatter\formatBooleanAsString
static formatBooleanAsString($value)
Definition: SchemaFormatter.php:98
Guzzle\Service\Description\SchemaFormatter\formatDateTimeHttp
static formatDateTimeHttp($value)
Definition: SchemaFormatter.php:62
Guzzle\Service\Description\SchemaFormatter\format
static format($format, $value)
Definition: SchemaFormatter.php:23
Guzzle\Service\Description\SchemaFormatter\formatDateTime
static formatDateTime($value)
Definition: SchemaFormatter.php:50
Guzzle\Service\Description\SchemaFormatter\dateFormatter
static dateFormatter($dateTime, $format)
Definition: SchemaFormatter.php:140