1 | <?php |
||
19 | class DateSelectField extends TimeZoneAwareField implements ParserInterface |
||
20 | { |
||
21 | const KEY_YEAR = 'year'; |
||
22 | const KEY_MONTH = 'month'; |
||
23 | const KEY_DAY = 'day'; |
||
24 | |||
25 | /** |
||
26 | * @var int first year available for selection in year drop-down |
||
27 | */ |
||
28 | public $year_min; |
||
29 | |||
30 | /** |
||
31 | * @var int last year available for selection in year drop-down |
||
32 | */ |
||
33 | public $year_max; |
||
34 | |||
35 | /** |
||
36 | * @var string[] list of month names |
||
37 | */ |
||
38 | public $months; |
||
39 | |||
40 | /** |
||
41 | * @var string label for the year drop-down |
||
42 | */ |
||
43 | public $label_year; |
||
44 | |||
45 | /** |
||
46 | * @var string label for the year drop-down |
||
47 | */ |
||
48 | public $label_month; |
||
49 | |||
50 | /** |
||
51 | * @var string label for the year drop-down |
||
52 | */ |
||
53 | public $label_day; |
||
54 | |||
55 | /** |
||
56 | * @var string[] field order, e.g. KEY_* constants in the desired input order |
||
57 | */ |
||
58 | public $order = [ |
||
59 | self::KEY_DAY, |
||
60 | self::KEY_MONTH, |
||
61 | self::KEY_YEAR, |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * @var string CSS class-name for the year drop-down |
||
66 | */ |
||
67 | public $year_class = self::KEY_YEAR; |
||
68 | |||
69 | /** |
||
70 | * @var string CSS class-name for the year drop-down |
||
71 | */ |
||
72 | public $month_class = self::KEY_MONTH; |
||
73 | |||
74 | /** |
||
75 | * @var string CSS class-name for the year drop-down |
||
76 | */ |
||
77 | public $day_class = self::KEY_DAY; |
||
78 | |||
79 | /** |
||
80 | * @param string $name field name |
||
81 | * @param DateTimeZone|string|null $timezone input time-zone (or NULL to use the current default timezone) |
||
82 | */ |
||
83 | 2 | public function __construct($name, $timezone = null) |
|
99 | |||
100 | /** |
||
101 | * Set the range of years available in the year drop-down. |
||
102 | * |
||
103 | * Values are relative to current year - e.g. 0 is the current year, -1 is last |
||
104 | * year, 10 is 10 years from now, etc. |
||
105 | * |
||
106 | * @param int $min |
||
107 | * @param int $max |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | 2 | public function setYearRange($min, $max) |
|
121 | |||
122 | /** |
||
123 | * Attempt to parse the given form input and convert to integer timestamp. |
||
124 | * |
||
125 | * @param string $input |
||
126 | * |
||
127 | * @return int|null integer timestamp on success; NULL on failure |
||
128 | */ |
||
129 | 1 | public function parseInput($input) |
|
150 | |||
151 | /** |
||
152 | * @param InputModel $model |
||
153 | * |
||
154 | * @return int|null timestamp |
||
155 | * |
||
156 | * @throws UnexpectedValueException if the input is invalid (assumes valid input) |
||
157 | */ |
||
158 | 1 | public function getValue(InputModel $model) |
|
174 | |||
175 | /** |
||
176 | * @param InputModel $model |
||
177 | * @param int|null $value timestamp |
||
178 | * |
||
179 | * @return void |
||
180 | * |
||
181 | * @throws InvalidArgumentException if the given value is unacceptable. |
||
182 | */ |
||
183 | 1 | public function setValue(InputModel $model, $value) |
|
184 | { |
||
185 | 1 | if (is_int($value)) { |
|
186 | 1 | $date = new Datetime(); |
|
187 | 1 | $date->setTimestamp($value); |
|
188 | 1 | $date->setTimezone($this->timezone); |
|
189 | |||
190 | 1 | $model->setInput( |
|
191 | 1 | $this, [ |
|
192 | 1 | self::KEY_YEAR => $date->format('Y'), |
|
193 | 1 | self::KEY_MONTH => $date->format('n'), |
|
194 | 1 | self::KEY_DAY => $date->format('j'), |
|
195 | ] |
||
196 | ); |
||
197 | 1 | } elseif ($value === null) { |
|
198 | 1 | $model->setInput($this, null); |
|
199 | } else { |
||
200 | throw new InvalidArgumentException("string expected"); |
||
201 | } |
||
202 | 1 | } |
|
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | * |
||
207 | * @throws UnexpectedValueException |
||
208 | */ |
||
209 | 1 | public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | 1 | public function createValidators() |
|
262 | } |
||
263 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.