1: <?php
2:
3: namespace Guzzle\Service\Description;
4:
5: use Guzzle\Common\Exception\InvalidArgumentException;
6:
7: 8: 9:
10: class SchemaFormatter
11: {
12: 13: 14:
15: protected static $utcTimeZone;
16:
17: 18: 19: 20: 21: 22: 23: 24:
25: public static function format($format, $value)
26: {
27: switch ($format) {
28: case 'date-time':
29: return self::formatDateTime($value);
30: case 'date-time-http':
31: return self::formatDateTimeHttp($value);
32: case 'date':
33: return self::formatDate($value);
34: case 'time':
35: return self::formatTime($value);
36: case 'timestamp':
37: return self::formatTimestamp($value);
38: case 'boolean-string':
39: return self::formatBooleanAsString($value);
40: default:
41: return $value;
42: }
43: }
44:
45: 46: 47: 48: 49: 50: 51:
52: public static function formatDateTime($value)
53: {
54: return self::dateFormatter($value, 'Y-m-d\TH:i:s\Z');
55: }
56:
57: 58: 59: 60: 61: 62: 63:
64: public static function formatDateTimeHttp($value)
65: {
66: return self::dateFormatter($value, 'D, d M Y H:i:s \G\M\T');
67: }
68:
69: 70: 71: 72: 73: 74: 75:
76: public static function formatDate($value)
77: {
78: return self::dateFormatter($value, 'Y-m-d');
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public static function formatTime($value)
89: {
90: return self::dateFormatter($value, 'H:i:s');
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: public static function formatBooleanAsString($value)
101: {
102: return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
103: }
104:
105: 106: 107: 108: 109: 110: 111:
112: public static function formatTimestamp($value)
113: {
114: return self::dateFormatter($value, 'U');
115: }
116:
117: 118: 119: 120: 121:
122: protected static function getUtcTimeZone()
123: {
124:
125: if (!self::$utcTimeZone) {
126: self::$utcTimeZone = new \DateTimeZone('UTC');
127: }
128:
129:
130: return self::$utcTimeZone;
131: }
132:
133: 134: 135: 136: 137: 138: 139: 140: 141:
142: protected static function dateFormatter($dateTime, $format)
143: {
144: if (is_numeric($dateTime)) {
145: return gmdate($format, (int) $dateTime);
146: }
147:
148: if (is_string($dateTime)) {
149: $dateTime = new \DateTime($dateTime);
150: }
151:
152: if ($dateTime instanceof \DateTime) {
153: return $dateTime->setTimezone(self::getUtcTimeZone())->format($format);
154: }
155:
156: throw new InvalidArgumentException('Date/Time values must be either a string, integer, or DateTime object');
157: }
158: }
159: