Completed
Push — master ( a83709...932cd1 )
by Alex
04:41
created

DateTime::validate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 13
nc 3
nop 2
1
<?php
2
3
namespace POData\Providers\Metadata\Type;
4
5
use Carbon\Carbon;
6
7
/**
8
 * Class DateTime.
9
 */
10
class DateTime implements IType
11
{
12
    /**
13
     * Gets the type code
14
     * Note: implementation of IType::getTypeCode.
15
     *
16
     * @return TypeCode
17
     */
18
    public function getTypeCode()
19
    {
20
        return TypeCode::DATETIME;
21
    }
22
23
    /**
24
     * Checks this type is compatible with another type
25
     * Note: implementation of IType::isCompatibleWith.
26
     *
27
     * @param IType $type Type to check compatibility
28
     *
29
     * @return bool
30
     */
31
    public function isCompatibleWith(IType $type)
32
    {
33
        return TypeCode::DATETIME == $type->getTypeCode();
34
    }
35
36
    /**
37
     * Validate a value in Astoria uri is in a format for this type
38
     * Note: implementation of IType::validate.
39
     *
40
     * @param string $value     The value to validate
41
     * @param string &$outValue The stripped form of $value that can
42
     *                          be used in PHP expressions
43
     *
44
     * @return bool
45
     */
46
    public function validate($value, &$outValue)
47
    {
48
        //1. The datetime value present in the $filter option should have
49
        //   'datetime' prefix.
50
        //2. Month and day should be two digit
51
        if (!preg_match(
52
            "/^datetime\'(\d{4})-(\d{2})-(\d{2})((\s|T)([0-1][0-9]|2[0-4]):([0-5][0-9])(:([0-5][0-9])([Z])?)?)?\'$/",
53
            strval($value),
54
            $matches
55
        )) {
56
            return false;
57
        }
58
59
        //stripoff prefix, and quotes from both ends
60
        $value = trim($value, 'datetime\'');
61
62
        //Validate the date using PHP Carbon class
63
        try {
64
            new Carbon($value, new \DateTimeZone('UTC'));
65
        } catch (\Exception $e) {
66
            return false;
67
        }
68
69
        $outValue = "'" . $value . "'";
70
71
        return true;
72
    }
73
74
    /**
75
     * Gets full name of this type in EDM namespace
76
     * Note: implementation of IType::getFullTypeName.
77
     *
78
     * @return string
79
     */
80
    public function getFullTypeName()
81
    {
82
        return 'Edm.DateTime';
83
    }
84
85
    /**
86
     * Converts the given string value to datetime type.
87
     * Note: This function will not perform any conversion.
88
     *
89
     * @param string $stringValue Value to convert
90
     *
91
     * @return string
92
     */
93
    public function convert($stringValue)
94
    {
95
        return $stringValue;
96
    }
97
98
    /**
99
     * Convert the given value to a form that can be used in OData uri.
100
     * Note: The calling function should not pass null value, as this
101
     * function will not perform any check for nullability.
102
     *
103
     * @param mixed $value Value to convert
104
     *
105
     * @return string
106
     */
107
    public function convertToOData($value)
108
    {
109
        return 'datetime\'' . urlencode($value) . '\'';
110
    }
111
112
    /**
113
     * Gets year from datetime.
114
     *
115
     * @param string $dateTime datetime to get the year from
116
     *
117
     * @return string
118
     */
119
    public static function year($dateTime)
120
    {
121
        $date = new Carbon($dateTime);
122
        return $date->format("Y");
123
    }
124
125
    /**
126
     * Gets month from datetime.
127
     *
128
     * @param string $dateTime datetime to get the month from
129
     *
130
     * @return string
131
     */
132
    public static function month($dateTime)
133
    {
134
        $date = new Carbon($dateTime);
135
        return $date->format("m");
136
    }
137
138
    /**
139
     * Gets day from datetime.
140
     *
141
     * @param string $dateTime datetime to get the day from
142
     *
143
     * @return string
144
     */
145
    public static function day($dateTime)
146
    {
147
        $date = new Carbon($dateTime);
148
        return $date->format("d");
149
    }
150
151
    /**
152
     * Gets hour from datetime.
153
     *
154
     * @param string $dateTime datetime to get the hour from
155
     *
156
     * @return string
157
     */
158
    public static function hour($dateTime)
159
    {
160
        $date = new Carbon($dateTime);
161
        return $date->format("H");
162
    }
163
164
    /**
165
     * Gets minute from datetime.
166
     *
167
     * @param string $dateTime datetime to get the minute from
168
     *
169
     * @return string
170
     */
171
    public static function minute($dateTime)
172
    {
173
        $date = new Carbon($dateTime);
174
        return $date->format("i");
175
    }
176
177
    /**
178
     * Gets second from datetime.
179
     *
180
     * @param string $dateTime datetime to get the second from
181
     *
182
     * @return string
183
     */
184
    public static function second($dateTime)
185
    {
186
        $date = new Carbon($dateTime);
187
        return $date->format("s");
188
    }
189
190
    /**
191
     * Compare two dates. Note that this function will not perform any
192
     * validation on dates, one should use either validate or
193
     * validateWithoutPrefix to validate the date before calling this
194
     * function.
195
     *
196
     * @param string $dateTime1 First date
197
     * @param string $dateTime2 Second date
198
     *
199
     * @return int
200
     */
201
    public static function dateTimeCmp($dateTime1, $dateTime2)
202
    {
203
        return strtotime($dateTime1) - strtotime($dateTime2);
204
    }
205
206
    /**
207
     * Gets full name of the type implementing this interface in EDM namespace
208
     * Note: implementation of IType::getFullTypeName.
209
     *
210
     * @return string
211
     */
212
    public function getName()
213
    {
214
        return $this->getFullTypeName();
215
    }
216
}
217