1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace frictionlessdata\tableschema\Fields; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class TimeField |
9
|
|
|
* casts to array [hour, minute, second]. |
10
|
|
|
*/ |
11
|
|
|
class TimeField extends BaseField |
12
|
|
|
{ |
13
|
|
|
protected function validateCastValue($val) |
14
|
|
|
{ |
15
|
|
|
switch ($this->format()) { |
16
|
|
|
case 'default': |
17
|
|
|
$time = explode(':', $val); |
18
|
|
|
if (count($time) != 3) { |
19
|
|
|
throw $this->getValidationException(null, $val); |
20
|
|
|
} else { |
21
|
|
|
list($hour, $minute, $second) = $time; |
22
|
|
|
|
23
|
|
|
return $this->getNativeTime($hour, $minute, $second); |
24
|
|
|
} |
25
|
|
|
break; |
|
|
|
|
26
|
|
|
case 'any': |
27
|
|
|
try { |
28
|
|
|
$dt = Carbon::parse($val); |
29
|
|
|
} catch (\Exception $e) { |
30
|
|
|
throw $this->getValidationException($e->getMessage(), $val); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this->getNativeTime($dt->hour, $dt->minute, $dt->second); |
34
|
|
|
default: |
35
|
|
|
$date = strptime($val, $this->format()); |
36
|
|
|
if ($date === false || $date['unparsed'] != '') { |
37
|
|
|
throw $this->getValidationException(null, $val); |
38
|
|
|
} else { |
39
|
|
|
return $this->getNativeTime($date['tm_hour'], $date['tm_min'], $date['tm_sec']); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function type() |
45
|
|
|
{ |
46
|
|
|
return 'time'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getNativeTime($hour, $minute, $second) |
50
|
|
|
{ |
51
|
|
|
$parts = [$hour, $minute, $second]; |
52
|
|
|
foreach ($parts as &$part) { |
53
|
|
|
$part = (int) $part; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $parts; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.