@@ -9,9 +9,9 @@ discard block |
||
9 | 9 | namespace HexMakina\Lezer; |
10 | 10 | |
11 | 11 | use \HexMakina\LocalFS\FileSystem; |
12 | -use \HexMakina\Format\Tempo\{Dato,DatoTempo,Tempo}; |
|
12 | +use \HexMakina\Format\Tempo\{Dato, DatoTempo, Tempo}; |
|
13 | 13 | |
14 | -class Lezer extends \i18n{ |
|
14 | +class Lezer extends \i18n { |
|
15 | 15 | |
16 | 16 | private $detected_language_files = []; |
17 | 17 | private $detected_language_env = []; |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | $this->detect_language_env(); |
29 | 29 | $the_one_language = current(array_intersect($this->detect_language_files(), $this->detect_language_env())); |
30 | 30 | |
31 | - if($the_one_language) |
|
31 | + if ($the_one_language) |
|
32 | 32 | $this->setForcedLang($the_one_language); |
33 | 33 | |
34 | 34 | return $the_one_language; |
@@ -37,12 +37,12 @@ discard block |
||
37 | 37 | public function detect_language_files() |
38 | 38 | { |
39 | 39 | $files = FileSystem::preg_scandir(dirname($this->filePath), '/.json$/'); |
40 | - if(empty($files)) |
|
40 | + if (empty($files)) |
|
41 | 41 | return []; |
42 | 42 | |
43 | - $files = implode('',$files); |
|
43 | + $files = implode('', $files); |
|
44 | 44 | $res = preg_match_all('/([a-z]{3})\.json/', $files, $m); |
45 | - if($res) // false or 0 is none found |
|
45 | + if ($res) // false or 0 is none found |
|
46 | 46 | $this->detected_language_files = $m[1]; |
47 | 47 | return $this->detected_language_files; |
48 | 48 | } |
@@ -101,16 +101,16 @@ discard block |
||
101 | 101 | |
102 | 102 | public static function model_type_to_label($form_model) |
103 | 103 | { |
104 | - return L(sprintf('MODEL_%s_INSTANCE',get_class($form_model)::model_type())); |
|
104 | + return L(sprintf('MODEL_%s_INSTANCE', get_class($form_model)::model_type())); |
|
105 | 105 | } |
106 | 106 | public static function field_name_to_label($form_model, $field_name) |
107 | 107 | { |
108 | - return L(sprintf('MODEL_%s_FIELD_%s',(get_class($form_model))::model_type(), $field_name)); |
|
108 | + return L(sprintf('MODEL_%s_FIELD_%s', (get_class($form_model))::model_type(), $field_name)); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | // options['decimals'] = int |
112 | 112 | // options['abbrev'] = mixed: key needs to be set |
113 | - public static function when($event, $options=[]) |
|
113 | + public static function when($event, $options = []) |
|
114 | 114 | { |
115 | 115 | try { |
116 | 116 | $amount_of_days = DatoTempo::days_diff(new \DateTime($event), new \DateTime()); |
@@ -119,11 +119,11 @@ discard block |
||
119 | 119 | return __FUNCTION__.': error'; |
120 | 120 | } |
121 | 121 | |
122 | - if($amount_of_days === -1) |
|
122 | + if ($amount_of_days === -1) |
|
123 | 123 | return L('DATETIME_RANGE_YESTERDAY'); |
124 | - elseif($amount_of_days === 0) |
|
124 | + elseif ($amount_of_days === 0) |
|
125 | 125 | return L('DATETIME_RANGE_TODAY'); |
126 | - elseif($amount_of_days === 1) |
|
126 | + elseif ($amount_of_days === 1) |
|
127 | 127 | return L('DATETIME_RANGE_TOMORROW'); |
128 | 128 | |
129 | 129 | |
@@ -139,49 +139,49 @@ discard block |
||
139 | 139 | |
140 | 140 | $date_diff = DatoTempo::days_diff_in_parts(abs($amount_of_days)); |
141 | 141 | $ordering = []; |
142 | - foreach($datetime_parts as $unit => $label) |
|
142 | + foreach ($datetime_parts as $unit => $label) |
|
143 | 143 | { |
144 | - if(!isset($date_diff[$unit])) |
|
144 | + if (!isset($date_diff[$unit])) |
|
145 | 145 | continue; |
146 | 146 | |
147 | 147 | $qty = (int)$date_diff[$unit]; |
148 | 148 | |
149 | - if($qty === 0) |
|
149 | + if ($qty === 0) |
|
150 | 150 | continue; |
151 | 151 | |
152 | - if(isset($options['abbrev'])) |
|
153 | - $label.= '_ABBREV'; |
|
154 | - elseif($qty > 1) |
|
155 | - $label.= '_PLURAL'; |
|
152 | + if (isset($options['abbrev'])) |
|
153 | + $label .= '_ABBREV'; |
|
154 | + elseif ($qty > 1) |
|
155 | + $label .= '_PLURAL'; |
|
156 | 156 | |
157 | 157 | $ordering[$unit] = $qty.' '.L($label).'.'; |
158 | 158 | } |
159 | 159 | $ret = (isset($amount_of_days) && $amount_of_days >= 0) ? L('DATETIME_RANGE_PREFIX_FUTURE') : L('DATETIME_RANGE_PREFIX_PAST'); |
160 | - $ret.= ' '.implode(' & ', array_slice($ordering, 0,2)); |
|
160 | + $ret .= ' '.implode(' & ', array_slice($ordering, 0, 2)); |
|
161 | 161 | |
162 | 162 | return $ret; |
163 | 163 | } |
164 | 164 | |
165 | - public static function time($time_string, $short=true) |
|
165 | + public static function time($time_string, $short = true) |
|
166 | 166 | { |
167 | 167 | if ($short === true) |
168 | 168 | $time_string = substr($time_string, 0, 5); |
169 | 169 | return $time_string; |
170 | 170 | } |
171 | 171 | |
172 | - public static function human_date($date_string, $short=true) |
|
172 | + public static function human_date($date_string, $short = true) |
|
173 | 173 | { |
174 | - if($date_string === '0000-00-00' || empty($date_string)) |
|
174 | + if ($date_string === '0000-00-00' || empty($date_string)) |
|
175 | 175 | return L('MODEL_common_VALUE_EMPTY'); |
176 | 176 | |
177 | - if(preg_match('/^[0-9]{4}$/', $date_string) === 1) |
|
177 | + if (preg_match('/^[0-9]{4}$/', $date_string) === 1) |
|
178 | 178 | return intval($date_string); |
179 | 179 | |
180 | - list($year, $month, $day) = explode('-',$date_string); |
|
180 | + list($year, $month, $day) = explode('-', $date_string); |
|
181 | 181 | |
182 | 182 | $ret = intval($day).' '.L("DATETIME_CALENDAR_MONTH_$month"); |
183 | 183 | |
184 | - if($short === true && Dato::format(null, 'Y') === $year) |
|
184 | + if ($short === true && Dato::format(null, 'Y') === $year) |
|
185 | 185 | return $ret; |
186 | 186 | else |
187 | 187 | return "$ret $year"; |
@@ -200,10 +200,10 @@ discard block |
||
200 | 200 | public static function human_seconds($seconds) |
201 | 201 | { |
202 | 202 | $hours = floor($seconds / 3600); |
203 | - $mins = floor(($seconds-$hours*3600) / 60); |
|
203 | + $mins = floor(($seconds-$hours * 3600) / 60); |
|
204 | 204 | $secs = floor($seconds % 60); |
205 | 205 | |
206 | - $hours_format ='%dh %dm %ds'; |
|
206 | + $hours_format = '%dh %dm %ds'; |
|
207 | 207 | return sprintf($hours_format, $hours, $mins, $secs); |
208 | 208 | } |
209 | 209 |
@@ -28,8 +28,9 @@ discard block |
||
28 | 28 | $this->detect_language_env(); |
29 | 29 | $the_one_language = current(array_intersect($this->detect_language_files(), $this->detect_language_env())); |
30 | 30 | |
31 | - if($the_one_language) |
|
32 | - $this->setForcedLang($the_one_language); |
|
31 | + if($the_one_language) { |
|
32 | + $this->setForcedLang($the_one_language); |
|
33 | + } |
|
33 | 34 | |
34 | 35 | return $the_one_language; |
35 | 36 | } |
@@ -37,13 +38,16 @@ discard block |
||
37 | 38 | public function detect_language_files() |
38 | 39 | { |
39 | 40 | $files = FileSystem::preg_scandir(dirname($this->filePath), '/.json$/'); |
40 | - if(empty($files)) |
|
41 | - return []; |
|
41 | + if(empty($files)) { |
|
42 | + return []; |
|
43 | + } |
|
42 | 44 | |
43 | 45 | $files = implode('',$files); |
44 | 46 | $res = preg_match_all('/([a-z]{3})\.json/', $files, $m); |
45 | - if($res) // false or 0 is none found |
|
47 | + if($res) { |
|
48 | + // false or 0 is none found |
|
46 | 49 | $this->detected_language_files = $m[1]; |
50 | + } |
|
47 | 51 | return $this->detected_language_files; |
48 | 52 | } |
49 | 53 | |
@@ -91,8 +95,9 @@ discard block |
||
91 | 95 | // remove illegal userLangs |
92 | 96 | foreach ($userLangs as $key => $value) { |
93 | 97 | // only allow a-z, A-Z and 0-9 and _ and - |
94 | - if (preg_match('/^[a-zA-Z0-9_-]*$/', $value) === 1) |
|
95 | - $this->detected_language_env[$key] = $value; |
|
98 | + if (preg_match('/^[a-zA-Z0-9_-]*$/', $value) === 1) { |
|
99 | + $this->detected_language_env[$key] = $value; |
|
100 | + } |
|
96 | 101 | } |
97 | 102 | |
98 | 103 | return $this->detected_language_env; |
@@ -119,12 +124,13 @@ discard block |
||
119 | 124 | return __FUNCTION__.': error'; |
120 | 125 | } |
121 | 126 | |
122 | - if($amount_of_days === -1) |
|
123 | - return L('DATETIME_RANGE_YESTERDAY'); |
|
124 | - elseif($amount_of_days === 0) |
|
125 | - return L('DATETIME_RANGE_TODAY'); |
|
126 | - elseif($amount_of_days === 1) |
|
127 | - return L('DATETIME_RANGE_TOMORROW'); |
|
127 | + if($amount_of_days === -1) { |
|
128 | + return L('DATETIME_RANGE_YESTERDAY'); |
|
129 | + } elseif($amount_of_days === 0) { |
|
130 | + return L('DATETIME_RANGE_TODAY'); |
|
131 | + } elseif($amount_of_days === 1) { |
|
132 | + return L('DATETIME_RANGE_TOMORROW'); |
|
133 | + } |
|
128 | 134 | |
129 | 135 | |
130 | 136 | $datetime_parts = [ |
@@ -141,18 +147,21 @@ discard block |
||
141 | 147 | $ordering = []; |
142 | 148 | foreach($datetime_parts as $unit => $label) |
143 | 149 | { |
144 | - if(!isset($date_diff[$unit])) |
|
145 | - continue; |
|
150 | + if(!isset($date_diff[$unit])) { |
|
151 | + continue; |
|
152 | + } |
|
146 | 153 | |
147 | 154 | $qty = (int)$date_diff[$unit]; |
148 | 155 | |
149 | - if($qty === 0) |
|
150 | - continue; |
|
156 | + if($qty === 0) { |
|
157 | + continue; |
|
158 | + } |
|
151 | 159 | |
152 | - if(isset($options['abbrev'])) |
|
153 | - $label.= '_ABBREV'; |
|
154 | - elseif($qty > 1) |
|
155 | - $label.= '_PLURAL'; |
|
160 | + if(isset($options['abbrev'])) { |
|
161 | + $label.= '_ABBREV'; |
|
162 | + } elseif($qty > 1) { |
|
163 | + $label.= '_PLURAL'; |
|
164 | + } |
|
156 | 165 | |
157 | 166 | $ordering[$unit] = $qty.' '.L($label).'.'; |
158 | 167 | } |
@@ -164,27 +173,31 @@ discard block |
||
164 | 173 | |
165 | 174 | public static function time($time_string, $short=true) |
166 | 175 | { |
167 | - if ($short === true) |
|
168 | - $time_string = substr($time_string, 0, 5); |
|
176 | + if ($short === true) { |
|
177 | + $time_string = substr($time_string, 0, 5); |
|
178 | + } |
|
169 | 179 | return $time_string; |
170 | 180 | } |
171 | 181 | |
172 | 182 | public static function human_date($date_string, $short=true) |
173 | 183 | { |
174 | - if($date_string === '0000-00-00' || empty($date_string)) |
|
175 | - return L('MODEL_common_VALUE_EMPTY'); |
|
184 | + if($date_string === '0000-00-00' || empty($date_string)) { |
|
185 | + return L('MODEL_common_VALUE_EMPTY'); |
|
186 | + } |
|
176 | 187 | |
177 | - if(preg_match('/^[0-9]{4}$/', $date_string) === 1) |
|
178 | - return intval($date_string); |
|
188 | + if(preg_match('/^[0-9]{4}$/', $date_string) === 1) { |
|
189 | + return intval($date_string); |
|
190 | + } |
|
179 | 191 | |
180 | 192 | list($year, $month, $day) = explode('-',$date_string); |
181 | 193 | |
182 | 194 | $ret = intval($day).' '.L("DATETIME_CALENDAR_MONTH_$month"); |
183 | 195 | |
184 | - if($short === true && Dato::format(null, 'Y') === $year) |
|
185 | - return $ret; |
|
186 | - else |
|
187 | - return "$ret $year"; |
|
196 | + if($short === true && Dato::format(null, 'Y') === $year) { |
|
197 | + return $ret; |
|
198 | + } else { |
|
199 | + return "$ret $year"; |
|
200 | + } |
|
188 | 201 | } |
189 | 202 | |
190 | 203 | public static function human_month($date_string) |