SchemaFormatter::formatDateTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace GuzzleHttp\Command\Guzzle;
3
4
/**
5
 * JSON Schema formatter class
6
 */
7
class SchemaFormatter
8
{
9
    /**
10
     * Format a value by a registered format name
11
     *
12
     * @param string $format Registered format used to format the value
13
     * @param mixed  $value  Value being formatted
14
     *
15
     * @return mixed
16
     */
17 18
    public function format($format, $value)
18
    {
19
        switch ($format) {
20 18
            case 'date-time':
21 5
                return $this->formatDateTime($value);
22 13
            case 'date-time-http':
23 1
                return $this->formatDateTimeHttp($value);
24 12
            case 'date':
25 1
                return $this->formatDate($value);
26 11
            case 'time':
27 2
                return $this->formatTime($value);
28 9
            case 'timestamp':
29 4
                return $this->formatTimestamp($value);
30 5
            case 'boolean-string':
31 4
                return $this->formatBooleanAsString($value);
32 1
            default:
33 1
                return $value;
34 1
        }
35
    }
36
37
    /**
38
     * Perform the actual DateTime formatting
39
     *
40
     * @param int|string|\DateTime $dateTime Date time value
41
     * @param string               $format   Format of the result
42
     *
43
     * @return string
44
     * @throws \InvalidArgumentException
45
     */
46 13
    protected function dateFormatter($dateTime, $format)
47
    {
48 13
        if (is_numeric($dateTime)) {
49 5
            return gmdate($format, (int) $dateTime);
50
        }
51
52 8
        if (is_string($dateTime)) {
53 6
            $dateTime = new \DateTime($dateTime);
54 6
        }
55
56 8
        if ($dateTime instanceof \DateTimeInterface) {
57 7
            static $utc;
58 7
            if (!$utc) {
59
                $utc = new \DateTimeZone('UTC');
60
            }
61 7
            return $dateTime->setTimezone($utc)->format($format);
62
        }
63
64 1
        throw new \InvalidArgumentException('Date/Time values must be either '
65 1
            . 'be a string, integer, or DateTime object');
66
    }
67
68
    /**
69
     * Create a ISO 8601 (YYYY-MM-DDThh:mm:ssZ) formatted date time value in
70
     * UTC time.
71
     *
72
     * @param string|integer|\DateTime $value Date time value
73
     *
74
     * @return string
75
     */
76 5
    private function formatDateTime($value)
77
    {
78 5
        return $this->dateFormatter($value, 'Y-m-d\TH:i:s\Z');
79
    }
80
81
    /**
82
     * Create an HTTP date (RFC 1123 / RFC 822) formatted UTC date-time string
83
     *
84
     * @param string|integer|\DateTime $value Date time value
85
     *
86
     * @return string
87
     */
88 1
    private function formatDateTimeHttp($value)
89
    {
90 1
        return $this->dateFormatter($value, 'D, d M Y H:i:s \G\M\T');
91
    }
92
93
    /**
94
     * Create a YYYY-MM-DD formatted string
95
     *
96
     * @param string|integer|\DateTime $value Date time value
97
     *
98
     * @return string
99
     */
100 1
    private function formatDate($value)
101
    {
102 1
        return $this->dateFormatter($value, 'Y-m-d');
103
    }
104
105
    /**
106
     * Create a hh:mm:ss formatted string
107
     *
108
     * @param string|integer|\DateTime $value Date time value
109
     *
110
     * @return string
111
     */
112 2
    private function formatTime($value)
113
    {
114 2
        return $this->dateFormatter($value, 'H:i:s');
115
    }
116
117
    /**
118
     * Formats a boolean value as a string
119
     *
120
     * @param string|integer|bool $value Value to convert to a boolean
121
     *                                   'true' / 'false' value
122
     *
123
     * @return string
124
     */
125 4
    private function formatBooleanAsString($value)
126
    {
127 4
        return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
128
    }
129
130
    /**
131
     * Return a UNIX timestamp in the UTC timezone
132
     *
133
     * @param string|integer|\DateTime $value Time value
134
     *
135
     * @return int
136
     */
137 4
    private function formatTimestamp($value)
138
    {
139 4
        return (int) $this->dateFormatter($value, 'U');
140
    }
141
}
142