-
Notifications
You must be signed in to change notification settings - Fork 5
/
DateRangeConfig.php
52 lines (44 loc) · 1.52 KB
/
DateRangeConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace omnilight\datetime;
use omnilight\widgets\DateRangePicker;
use yii\base\InvalidParamException;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\helpers\FormatConverter;
/**
* Class DateRangeConfig
*/
class DateRangeConfig
{
use DateTimeAttributeFinder, DateTimeRangeBehaviorFinder;
/**
* @param Model $model
* @param string $attribute
* @param array $options
* @param string $datePickerClass
* @return array
*/
public static function get(Model $model, $attribute, $options = [], $datePickerClass = DateRangePicker::class)
{
$behavior = self::findBehavior($model, $attribute);
$formatAttribute = null;
try {
$formatAttribute = self::findAttribute($model, $behavior->startAttribute);
$formatAttribute = self::findAttribute($model, $behavior->endAttribute);
} catch (InvalidParamException $e) {
}
$defaults = [];
switch ($datePickerClass) {
case DateRangePicker::class:
$defaults = [
'separator' => $behavior->separator,
];
if ($formatAttribute) {
$format = DateTimeBehavior::normalizeIcuFormat($formatAttribute->targetFormat, $formatAttribute->behavior->formatter);
$defaults['dateFormat'] = 'php:'.FormatConverter::convertDateIcuToPhp($format[1], $format[0]);
}
break;
}
return ArrayHelper::merge($defaults, $options);
}
}