|
1
|
|
|
<?php |
|
2
|
|
|
namespace Solvire\API\Serializers\DataFields; |
|
3
|
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* A DateTime object - Carbon |
|
8
|
|
|
* |
|
9
|
|
|
* It would be nicer to us if you pass in an object so we don't have to guess. |
|
10
|
|
|
* BTW Carbon Rocks |
|
11
|
|
|
* |
|
12
|
|
|
* @see http://carbon.nesbot.com/docs/ |
|
13
|
|
|
* |
|
14
|
|
|
* |
|
15
|
|
|
* @author solvire <[email protected]> |
|
16
|
|
|
* @package DataFields |
|
17
|
|
|
* @namespace Solvire\API\Serializers\DataFields |
|
18
|
|
|
*/ |
|
19
|
|
|
class DateTimeField extends DataField |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* default formatting of ISO8601 |
|
24
|
|
|
* |
|
25
|
|
|
* NOTE: make sure to set the formatting that will be coming in. |
|
26
|
|
|
* It can be set in the constructor or after the fact |
|
27
|
|
|
* |
|
28
|
|
|
* @var string [Y-m-d\TH:i:sO] |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $format = Carbon::ISO8601; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var unknown |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $cast = 'datetime'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $options |
|
41
|
|
|
*/ |
|
42
|
10 |
|
public function __construct($options=[]) |
|
43
|
|
|
{ |
|
44
|
|
|
// load up the format at creation time |
|
45
|
10 |
|
if(isset($options['format'])) |
|
46
|
10 |
|
$this->format = $options['format']; |
|
47
|
10 |
|
parent::__construct($options); |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* |
|
52
|
|
|
* |
|
53
|
|
|
* This can take any of the three formats listed. |
|
54
|
|
|
* |
|
55
|
|
|
* @param $data mixed |
|
56
|
|
|
* [string, Carbon, DateTime] |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this (non-PHPdoc) |
|
59
|
|
|
* @throws RuntimeException |
|
60
|
|
|
* @see \Solvire\API\Serializers\DataFields\DataField::setData() |
|
61
|
|
|
*/ |
|
62
|
5 |
|
public function setData($data) |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
5 |
|
if (is_string($data)) { |
|
66
|
1 |
|
$date = Carbon::createFromFormat($this->format, $data); |
|
67
|
5 |
|
} elseif ($data instanceof Carbon) { |
|
68
|
3 |
|
$date = $data; |
|
69
|
5 |
|
} elseif ($data instanceof \DateTime) { |
|
70
|
3 |
|
$date = Carbon::instance($data); |
|
71
|
3 |
|
} |
|
72
|
5 |
|
} catch (\Exception $e) { |
|
73
|
1 |
|
throw new \RuntimeException('DateTimeField data must be a string representation of a date/time object in format: ' . $this->format, 400, $e); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
5 |
|
$this->data = $date; |
|
|
|
|
|
|
77
|
5 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $format |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function setFormat($format) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->format = $format; |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* This is a char so it will always be just a string |
|
91
|
|
|
*/ |
|
92
|
5 |
|
public function getData() |
|
93
|
|
|
{ |
|
94
|
5 |
|
return $this->data; |
|
95
|
|
|
} |
|
96
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: