|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\components; |
|
4
|
|
|
|
|
5
|
|
|
use yii\validators\EmailValidator; |
|
6
|
|
|
use yii\validators\UrlValidator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Formating Dates. |
|
10
|
|
|
* |
|
11
|
|
|
* It extends the Yii2 formatter component by a signifcant configuration option which allwos you |
|
12
|
|
|
* to *predefine* a format for each language if no sepcific format is provided. |
|
13
|
|
|
* |
|
14
|
|
|
* ```php |
|
15
|
|
|
* 'components' => [ |
|
16
|
|
|
* 'formatter' => [ |
|
17
|
|
|
* 'dateFormats' => [ |
|
18
|
|
|
* 'fr' => 'dd.MM.yyyy', |
|
19
|
|
|
* 'de' => 'php:A, d. F Y', |
|
20
|
|
|
* ], |
|
21
|
|
|
* ], |
|
22
|
|
|
* ], |
|
23
|
|
|
* ``` |
|
24
|
|
|
* |
|
25
|
|
|
* The follwing form norms are available: |
|
26
|
|
|
* |
|
27
|
|
|
* - `php:$format` php prefixed string where $format has following options: http://php.net/manual/en/function.date.php |
|
28
|
|
|
* - `$format` Or without php prefix use ICU options: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax |
|
29
|
|
|
* |
|
30
|
|
|
* @author Basil Suter <[email protected]> |
|
31
|
|
|
* @since 1.0.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
class Formatter extends \yii\i18n\Formatter |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var array An array with date formats to use as default values where key is the local language and value the format |
|
37
|
|
|
* to use for the given language. |
|
38
|
|
|
* |
|
39
|
|
|
* ```php |
|
40
|
|
|
* 'dateFormats' => [ |
|
41
|
|
|
* 'fr' => 'dd.MM.yyyy', |
|
42
|
|
|
* 'en' => 'MM/dd/yyyy', |
|
43
|
|
|
* ] |
|
44
|
|
|
* ``` |
|
45
|
|
|
* |
|
46
|
|
|
* See {{\luya\component\Formatter::$dateFormat}} for more informations about valid values. |
|
47
|
|
|
*/ |
|
48
|
|
|
public $dateFormats = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var array An array with datetime formats to use as default values where the key is the local language and value |
|
52
|
|
|
* the format to use for the given language. |
|
53
|
|
|
* |
|
54
|
|
|
* ```php |
|
55
|
|
|
* 'datetimeFormats' => [ |
|
56
|
|
|
* 'fr' => 'dd.MM.yyyy - HH:mm:ss', |
|
57
|
|
|
* 'en' => 'MM/dd/yyyy HH:mm:ss', |
|
58
|
|
|
* ] |
|
59
|
|
|
* ``` |
|
60
|
|
|
* |
|
61
|
|
|
* See {{\luya\component\Formatter::$datetimeFormat}} for more informations about valid values. |
|
62
|
|
|
*/ |
|
63
|
|
|
public $datetimeFormats = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var array An array with time formats to use as default values where the key is the local language and value |
|
67
|
|
|
* the format to use for the given language. |
|
68
|
|
|
* |
|
69
|
|
|
* ```php |
|
70
|
|
|
* 'timeFormats' => [ |
|
71
|
|
|
* 'fr' => 'HH:mm:ss', |
|
72
|
|
|
* 'de' => 'HH.mm.ss', |
|
73
|
|
|
* ], |
|
74
|
|
|
* ``` |
|
75
|
|
|
* |
|
76
|
|
|
* See {{\luya\component\Formatter::$timeFormat}} for more informations about valid values. |
|
77
|
|
|
*/ |
|
78
|
|
|
public $timeFormats = []; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritdoc |
|
82
|
|
|
*/ |
|
83
|
|
|
public function init() |
|
84
|
|
|
{ |
|
85
|
|
|
parent::init(); |
|
86
|
|
|
|
|
87
|
|
|
if (isset($this->dateFormats[$this->locale])) { |
|
88
|
|
|
$this->dateFormat = $this->dateFormats[$this->locale]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (isset($this->datetimeFormats[$this->locale])) { |
|
92
|
|
|
$this->datetimeFormat = $this->datetimeFormats[$this->locale]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (isset($this->timeFormats[$this->locale])) { |
|
96
|
|
|
$this->timeFormat = $this->timeFormats[$this->locale]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Auto format the value to a given format like url, email. |
|
102
|
|
|
* |
|
103
|
|
|
* The following rules will apply to auto format the value: |
|
104
|
|
|
* |
|
105
|
|
|
* + boolean: asBool |
|
106
|
|
|
* + email: asEmail |
|
107
|
|
|
* + url: asUrl |
|
108
|
|
|
* |
|
109
|
|
|
* @param mixed $value Returns the formated value otherwise the original input value. |
|
110
|
|
|
* @since 1.0.9 |
|
111
|
|
|
*/ |
|
112
|
|
|
public function autoFormat($value) |
|
113
|
|
|
{ |
|
114
|
|
|
// email validation |
|
115
|
|
|
if ((new EmailValidator())->validate($value)) { |
|
116
|
|
|
return $this->asEmail($value); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// url validator |
|
120
|
|
|
if ((new UrlValidator())->validate($value)) { |
|
121
|
|
|
return $this->asUrl($value); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// boolean type |
|
125
|
|
|
if (is_bool($value)) { |
|
126
|
|
|
return $this->asBoolean($value); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $value; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|