Complex classes like Formatter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Formatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Formatter extends Component |
||
49 | { |
||
50 | /** |
||
51 | * @since 2.0.13 |
||
52 | */ |
||
53 | const UNIT_SYSTEM_METRIC = 'metric'; |
||
54 | /** |
||
55 | * @since 2.0.13 |
||
56 | */ |
||
57 | const UNIT_SYSTEM_IMPERIAL = 'imperial'; |
||
58 | /** |
||
59 | * @since 2.0.13 |
||
60 | */ |
||
61 | const FORMAT_WIDTH_LONG = 'long'; |
||
62 | /** |
||
63 | * @since 2.0.13 |
||
64 | */ |
||
65 | const FORMAT_WIDTH_SHORT = 'short'; |
||
66 | /** |
||
67 | * @since 2.0.13 |
||
68 | */ |
||
69 | const UNIT_LENGTH = 'length'; |
||
70 | /** |
||
71 | * @since 2.0.13 |
||
72 | */ |
||
73 | const UNIT_WEIGHT = 'mass'; |
||
74 | |||
75 | /** |
||
76 | * @var string the text to be displayed when formatting a `null` value. |
||
77 | * Defaults to `'<span class="not-set">(not set)</span>'`, where `(not set)` |
||
78 | * will be translated according to [[locale]]. |
||
79 | */ |
||
80 | public $nullDisplay; |
||
81 | /** |
||
82 | * @var array the text to be displayed when formatting a boolean value. The first element corresponds |
||
83 | * to the text displayed for `false`, the second element for `true`. |
||
84 | * Defaults to `['No', 'Yes']`, where `Yes` and `No` |
||
85 | * will be translated according to [[locale]]. |
||
86 | */ |
||
87 | public $booleanFormat; |
||
88 | /** |
||
89 | * @var string the locale ID that is used to localize the date and number formatting. |
||
90 | * For number and date formatting this is only effective when the |
||
91 | * [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed. |
||
92 | * If not set, [[\yii\base\Application::language]] will be used. |
||
93 | */ |
||
94 | public $locale; |
||
95 | /** |
||
96 | * @var string the time zone to use for formatting time and date values. |
||
97 | * |
||
98 | * This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php) |
||
99 | * e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. |
||
100 | * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available time zones. |
||
101 | * If this property is not set, [[\yii\base\Application::timeZone]] will be used. |
||
102 | * |
||
103 | * Note that the default time zone for input data is assumed to be UTC by default if no time zone is included in the input date value. |
||
104 | * If you store your data in a different time zone in the database, you have to adjust [[defaultTimeZone]] accordingly. |
||
105 | */ |
||
106 | public $timeZone; |
||
107 | /** |
||
108 | * @var string the time zone that is assumed for input values if they do not include a time zone explicitly. |
||
109 | * |
||
110 | * The value must be a valid time zone identifier, e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. |
||
111 | * Please refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available time zones. |
||
112 | * |
||
113 | * It defaults to `UTC` so you only have to adjust this value if you store datetime values in another time zone in your database. |
||
114 | * |
||
115 | * Note that a UNIX timestamp is always in UTC by its definition. That means that specifying a default time zone different from |
||
116 | * UTC has no effect on date values given as UNIX timestamp. |
||
117 | * |
||
118 | * @since 2.0.1 |
||
119 | */ |
||
120 | public $defaultTimeZone = 'UTC'; |
||
121 | /** |
||
122 | * @var string the default format string to be used to format a [[asDate()|date]]. |
||
123 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
124 | * |
||
125 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax). |
||
126 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
127 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
128 | * |
||
129 | * For example: |
||
130 | * |
||
131 | * ```php |
||
132 | * 'MM/dd/yyyy' // date in ICU format |
||
133 | * 'php:m/d/Y' // the same date in PHP format |
||
134 | * ``` |
||
135 | */ |
||
136 | public $dateFormat = 'medium'; |
||
137 | /** |
||
138 | * @var string the default format string to be used to format a [[asTime()|time]]. |
||
139 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
140 | * |
||
141 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax). |
||
142 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
143 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
144 | * |
||
145 | * For example: |
||
146 | * |
||
147 | * ```php |
||
148 | * 'HH:mm:ss' // time in ICU format |
||
149 | * 'php:H:i:s' // the same time in PHP format |
||
150 | * ``` |
||
151 | */ |
||
152 | public $timeFormat = 'medium'; |
||
153 | /** |
||
154 | * @var string the default format string to be used to format a [[asDatetime()|date and time]]. |
||
155 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
156 | * |
||
157 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax). |
||
158 | * |
||
159 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
160 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
161 | * |
||
162 | * For example: |
||
163 | * |
||
164 | * ```php |
||
165 | * 'MM/dd/yyyy HH:mm:ss' // date and time in ICU format |
||
166 | * 'php:m/d/Y H:i:s' // the same date and time in PHP format |
||
167 | * ``` |
||
168 | */ |
||
169 | public $datetimeFormat = 'medium'; |
||
170 | /** |
||
171 | * @var \IntlCalendar|int|null the calendar to be used for date formatting. The value of this property will be directly |
||
172 | * passed to the [constructor of the `IntlDateFormatter` class](http://php.net/manual/en/intldateformatter.create.php). |
||
173 | * |
||
174 | * Defaults to `null`, which means the Gregorian calendar will be used. You may also explicitly pass the constant |
||
175 | * `\IntlDateFormatter::GREGORIAN` for Gregorian calendar. |
||
176 | * |
||
177 | * To use an alternative calendar like for example the [Jalali calendar](https://en.wikipedia.org/wiki/Jalali_calendar), |
||
178 | * set this property to `\IntlDateFormatter::TRADITIONAL`. |
||
179 | * The calendar must then be specified in the [[locale]], for example for the persian calendar the configuration for the formatter would be: |
||
180 | * |
||
181 | * ```php |
||
182 | * 'formatter' => [ |
||
183 | * 'locale' => 'fa_IR@calendar=persian', |
||
184 | * 'calendar' => \IntlDateFormatter::TRADITIONAL, |
||
185 | * ], |
||
186 | * ``` |
||
187 | * |
||
188 | * Available calendar names can be found in the [ICU manual](http://userguide.icu-project.org/datetime/calendar). |
||
189 | * |
||
190 | * Since PHP 5.5 you may also use an instance of the [[\IntlCalendar]] class. |
||
191 | * Check the [PHP manual](http://php.net/manual/en/intldateformatter.create.php) for more details. |
||
192 | * |
||
193 | * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, setting this property will have no effect. |
||
194 | * |
||
195 | * @see http://php.net/manual/en/intldateformatter.create.php |
||
196 | * @see http://php.net/manual/en/class.intldateformatter.php#intl.intldateformatter-constants.calendartypes |
||
197 | * @see http://php.net/manual/en/class.intlcalendar.php |
||
198 | * @since 2.0.7 |
||
199 | */ |
||
200 | public $calendar; |
||
201 | /** |
||
202 | * @var string the character displayed as the decimal point when formatting a number. |
||
203 | * If not set, the decimal separator corresponding to [[locale]] will be used. |
||
204 | * If [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value is '.'. |
||
205 | */ |
||
206 | public $decimalSeparator; |
||
207 | /** |
||
208 | * @var string the character displayed as the thousands separator (also called grouping separator) character when formatting a number. |
||
209 | * If not set, the thousand separator corresponding to [[locale]] will be used. |
||
210 | * If [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value is ','. |
||
211 | */ |
||
212 | public $thousandSeparator; |
||
213 | /** |
||
214 | * @var array a list of name value pairs that are passed to the |
||
215 | * intl [NumberFormatter::setAttribute()](http://php.net/manual/en/numberformatter.setattribute.php) method of all |
||
216 | * the number formatter objects created by [[createNumberFormatter()]]. |
||
217 | * This property takes only effect if the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed. |
||
218 | * |
||
219 | * Please refer to the [PHP manual](http://php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatattribute) |
||
220 | * for the possible options. |
||
221 | * |
||
222 | * For example to adjust the maximum and minimum value of fraction digits you can configure this property like the following: |
||
223 | * |
||
224 | * ```php |
||
225 | * [ |
||
226 | * NumberFormatter::MIN_FRACTION_DIGITS => 0, |
||
227 | * NumberFormatter::MAX_FRACTION_DIGITS => 2, |
||
228 | * ] |
||
229 | * ``` |
||
230 | */ |
||
231 | public $numberFormatterOptions = []; |
||
232 | /** |
||
233 | * @var array a list of name value pairs that are passed to the |
||
234 | * intl [NumberFormatter::setTextAttribute()](http://php.net/manual/en/numberformatter.settextattribute.php) method of all |
||
235 | * the number formatter objects created by [[createNumberFormatter()]]. |
||
236 | * This property takes only effect if the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed. |
||
237 | * |
||
238 | * Please refer to the [PHP manual](http://php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformattextattribute) |
||
239 | * for the possible options. |
||
240 | * |
||
241 | * For example to change the minus sign for negative numbers you can configure this property like the following: |
||
242 | * |
||
243 | * ```php |
||
244 | * [ |
||
245 | * NumberFormatter::NEGATIVE_PREFIX => 'MINUS', |
||
246 | * ] |
||
247 | * ``` |
||
248 | */ |
||
249 | public $numberFormatterTextOptions = []; |
||
250 | /** |
||
251 | * @var array a list of name value pairs that are passed to the |
||
252 | * intl [NumberFormatter::setSymbol()](http://php.net/manual/en/numberformatter.setsymbol.php) method of all |
||
253 | * the number formatter objects created by [[createNumberFormatter()]]. |
||
254 | * This property takes only effect if the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed. |
||
255 | * |
||
256 | * Please refer to the [PHP manual](http://php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatsymbol) |
||
257 | * for the possible options. |
||
258 | * |
||
259 | * For example to choose a custom currency symbol, e.g. [U+20BD](http://unicode-table.com/en/20BD/) instead of `руб.` for Russian Ruble: |
||
260 | * |
||
261 | * ```php |
||
262 | * [ |
||
263 | * NumberFormatter::CURRENCY_SYMBOL => '₽', |
||
264 | * ] |
||
265 | * ``` |
||
266 | * |
||
267 | * @since 2.0.4 |
||
268 | */ |
||
269 | public $numberFormatterSymbols = []; |
||
270 | /** |
||
271 | * @var string the 3-letter ISO 4217 currency code indicating the default currency to use for [[asCurrency]]. |
||
272 | * If not set, the currency code corresponding to [[locale]] will be used. |
||
273 | * Note that in this case the [[locale]] has to be specified with a country code, e.g. `en-US` otherwise it |
||
274 | * is not possible to determine the default currency. |
||
275 | */ |
||
276 | public $currencyCode; |
||
277 | /** |
||
278 | * @var int the base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte), used by [[asSize]] and [[asShortSize]]. |
||
279 | * Defaults to 1024. |
||
280 | */ |
||
281 | public $sizeFormatBase = 1024; |
||
282 | /** |
||
283 | * @var string default system of measure units. Defaults to [[UNIT_SYSTEM_METRIC]]. |
||
284 | * Possible values: |
||
285 | * - [[UNIT_SYSTEM_METRIC]] |
||
286 | * - [[UNIT_SYSTEM_IMPERIAL]] |
||
287 | * |
||
288 | * @see asLength |
||
289 | * @see asWeight |
||
290 | * @since 2.0.13 |
||
291 | */ |
||
292 | public $systemOfUnits = self::UNIT_SYSTEM_METRIC; |
||
293 | /** |
||
294 | * @var array configuration of weight and length measurement units. |
||
295 | * This array contains the most usable measurement units, but you can change it |
||
296 | * in case you have some special requirements. |
||
297 | * |
||
298 | * For example, you can add smaller measure unit: |
||
299 | * |
||
300 | * ```php |
||
301 | * $this->measureUnits[self::UNIT_LENGTH][self::UNIT_SYSTEM_METRIC] = [ |
||
302 | * 'nanometer' => 0.000001 |
||
303 | * ] |
||
304 | * ``` |
||
305 | * @see asLength |
||
306 | * @see asWeight |
||
307 | * @since 2.0.13 |
||
308 | */ |
||
309 | public $measureUnits = [ |
||
310 | self::UNIT_LENGTH => [ |
||
311 | self::UNIT_SYSTEM_IMPERIAL => [ |
||
312 | 'inch' => 1, |
||
313 | 'foot' => 12, |
||
314 | 'yard' => 36, |
||
315 | 'chain' => 792, |
||
316 | 'furlong' => 7920, |
||
317 | 'mile' => 63360, |
||
318 | ], |
||
319 | self::UNIT_SYSTEM_METRIC => [ |
||
320 | 'millimeter' => 1, |
||
321 | 'centimeter' => 10, |
||
322 | 'meter' => 1000, |
||
323 | 'kilometer' => 1000000, |
||
324 | ], |
||
325 | ], |
||
326 | self::UNIT_WEIGHT => [ |
||
327 | self::UNIT_SYSTEM_IMPERIAL => [ |
||
328 | 'grain' => 1, |
||
329 | 'drachm' => 27.34375, |
||
330 | 'ounce' => 437.5, |
||
331 | 'pound' => 7000, |
||
332 | 'stone' => 98000, |
||
333 | 'quarter' => 196000, |
||
334 | 'hundredweight' => 784000, |
||
335 | 'ton' => 15680000, |
||
336 | ], |
||
337 | self::UNIT_SYSTEM_METRIC => [ |
||
338 | 'gram' => 1, |
||
339 | 'kilogram' => 1000, |
||
340 | 'ton' => 1000000, |
||
341 | ], |
||
342 | ], |
||
343 | ]; |
||
344 | /** |
||
345 | * @var array The base units that are used as multipliers for smallest possible unit from [[measureUnits]]. |
||
346 | * @since 2.0.13 |
||
347 | */ |
||
348 | public $baseUnits = [ |
||
349 | self::UNIT_LENGTH => [ |
||
350 | self::UNIT_SYSTEM_IMPERIAL => 12, // 1 feet = 12 inches |
||
351 | self::UNIT_SYSTEM_METRIC => 1000, // 1 meter = 1000 millimeters |
||
352 | ], |
||
353 | self::UNIT_WEIGHT => [ |
||
354 | self::UNIT_SYSTEM_IMPERIAL => 7000, // 1 pound = 7000 grains |
||
355 | self::UNIT_SYSTEM_METRIC => 1000, // 1 kilogram = 1000 grams |
||
356 | ], |
||
357 | ]; |
||
358 | |||
359 | /** |
||
360 | * @var bool whether the [PHP intl extension](http://php.net/manual/en/book.intl.php) is loaded. |
||
361 | */ |
||
362 | private $_intlLoaded = false; |
||
363 | /** |
||
364 | * @var \ResourceBundle cached ResourceBundle object used to read unit translations |
||
365 | */ |
||
366 | private $_resourceBundle; |
||
367 | /** |
||
368 | * @var array cached unit translation patterns |
||
369 | */ |
||
370 | private $_unitMessages = []; |
||
371 | |||
372 | |||
373 | /** |
||
374 | * @inheritdoc |
||
375 | */ |
||
376 | 257 | public function init() |
|
377 | { |
||
378 | 257 | if ($this->timeZone === null) { |
|
379 | 257 | $this->timeZone = Yii::$app->timeZone; |
|
380 | } |
||
381 | 257 | if ($this->locale === null) { |
|
382 | 38 | $this->locale = Yii::$app->language; |
|
383 | } |
||
384 | 257 | if ($this->booleanFormat === null) { |
|
385 | 257 | $this->booleanFormat = [Yii::t('yii', 'No', [], $this->locale), Yii::t('yii', 'Yes', [], $this->locale)]; |
|
386 | } |
||
387 | 257 | if ($this->nullDisplay === null) { |
|
388 | 257 | $this->nullDisplay = '<span class="not-set">' . Yii::t('yii', '(not set)', [], $this->locale) . '</span>'; |
|
389 | } |
||
390 | 257 | $this->_intlLoaded = extension_loaded('intl'); |
|
391 | 257 | if (!$this->_intlLoaded) { |
|
392 | 117 | if ($this->decimalSeparator === null) { |
|
393 | 117 | $this->decimalSeparator = '.'; |
|
394 | } |
||
395 | 117 | if ($this->thousandSeparator === null) { |
|
396 | 117 | $this->thousandSeparator = ','; |
|
397 | } |
||
398 | } |
||
399 | 257 | } |
|
400 | |||
401 | /** |
||
402 | * Formats the value based on the given format type. |
||
403 | * This method will call one of the "as" methods available in this class to do the formatting. |
||
404 | * For type "xyz", the method "asXyz" will be used. For example, if the format is "html", |
||
405 | * then [[asHtml()]] will be used. Format names are case insensitive. |
||
406 | * @param mixed $value the value to be formatted. |
||
407 | * @param string|array|Closure $format the format of the value, e.g., "html", "text" or an anonymous function |
||
408 | * returning the formatted value. |
||
409 | * |
||
410 | * To specify additional parameters of the formatting method, you may use an array. |
||
411 | * The first element of the array specifies the format name, while the rest of the elements will be used as the |
||
412 | * parameters to the formatting method. For example, a format of `['date', 'Y-m-d']` will cause the invocation |
||
413 | * of `asDate($value, 'Y-m-d')`. |
||
414 | * |
||
415 | * The anonymous function signature should be: `function($value, $formatter)`, |
||
416 | * where `$value` is the value that should be formatted and `$formatter` is an instance of the Formatter class, |
||
417 | * which can be used to call other formatting functions. |
||
418 | * The possibility to use an anonymous function is available since version 2.0.13. |
||
419 | * @return string the formatting result. |
||
420 | * @throws InvalidArgumentException if the format type is not supported by this class. |
||
421 | */ |
||
422 | 11 | public function format($value, $format) |
|
423 | { |
||
424 | 11 | if ($format instanceof Closure) { |
|
425 | return call_user_func($format, $value, $this); |
||
426 | 11 | } elseif (is_array($format)) { |
|
427 | 7 | if (!isset($format[0])) { |
|
428 | throw new InvalidArgumentException('The $format array must contain at least one element.'); |
||
429 | } |
||
430 | 7 | $f = $format[0]; |
|
431 | 7 | $format[0] = $value; |
|
432 | 7 | $params = $format; |
|
433 | 7 | $format = $f; |
|
434 | } else { |
||
435 | 6 | $params = [$value]; |
|
436 | } |
||
437 | 11 | $method = 'as' . $format; |
|
438 | 11 | if ($this->hasMethod($method)) { |
|
439 | 11 | return call_user_func_array([$this, $method], $params); |
|
440 | } |
||
441 | |||
442 | 2 | throw new InvalidArgumentException("Unknown format type: $format"); |
|
443 | } |
||
444 | |||
445 | |||
446 | // simple formats |
||
447 | |||
448 | |||
449 | /** |
||
450 | * Formats the value as is without any formatting. |
||
451 | * This method simply returns back the parameter without any format. |
||
452 | * The only exception is a `null` value which will be formatted using [[nullDisplay]]. |
||
453 | * @param mixed $value the value to be formatted. |
||
454 | * @return string the formatted result. |
||
455 | */ |
||
456 | 1 | public function asRaw($value) |
|
457 | { |
||
458 | 1 | if ($value === null) { |
|
459 | 1 | return $this->nullDisplay; |
|
460 | } |
||
461 | 1 | return $value; |
|
462 | } |
||
463 | |||
464 | /** |
||
465 | * Formats the value as an HTML-encoded plain text. |
||
466 | * @param string $value the value to be formatted. |
||
467 | * @return string the formatted result. |
||
468 | */ |
||
469 | 5 | public function asText($value) |
|
470 | { |
||
471 | 5 | if ($value === null) { |
|
472 | 2 | return $this->nullDisplay; |
|
473 | } |
||
474 | 5 | return Html::encode($value); |
|
475 | } |
||
476 | |||
477 | /** |
||
478 | * Formats the value as an HTML-encoded plain text with newlines converted into breaks. |
||
479 | * @param string $value the value to be formatted. |
||
480 | * @return string the formatted result. |
||
481 | */ |
||
482 | 1 | public function asNtext($value) |
|
483 | { |
||
484 | 1 | if ($value === null) { |
|
485 | 1 | return $this->nullDisplay; |
|
486 | } |
||
487 | 1 | return nl2br(Html::encode($value)); |
|
488 | } |
||
489 | |||
490 | /** |
||
491 | * Formats the value as HTML-encoded text paragraphs. |
||
492 | * Each text paragraph is enclosed within a `<p>` tag. |
||
493 | * One or multiple consecutive empty lines divide two paragraphs. |
||
494 | * @param string $value the value to be formatted. |
||
495 | * @return string the formatted result. |
||
496 | */ |
||
497 | 1 | public function asParagraphs($value) |
|
498 | { |
||
499 | 1 | if ($value === null) { |
|
500 | 1 | return $this->nullDisplay; |
|
501 | } |
||
502 | 1 | return str_replace('<p></p>', '', '<p>' . preg_replace('/\R{2,}/u', "</p>\n<p>", Html::encode($value)) . '</p>'); |
|
503 | } |
||
504 | |||
505 | /** |
||
506 | * Formats the value as HTML text. |
||
507 | * The value will be purified using [[HtmlPurifier]] to avoid XSS attacks. |
||
508 | * Use [[asRaw()]] if you do not want any purification of the value. |
||
509 | * @param string $value the value to be formatted. |
||
510 | * @param array|null $config the configuration for the HTMLPurifier class. |
||
511 | * @return string the formatted result. |
||
512 | */ |
||
513 | public function asHtml($value, $config = null) |
||
514 | { |
||
515 | if ($value === null) { |
||
516 | return $this->nullDisplay; |
||
517 | } |
||
518 | return HtmlPurifier::process($value, $config); |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Formats the value as a mailto link. |
||
523 | * @param string $value the value to be formatted. |
||
524 | * @param array $options the tag options in terms of name-value pairs. See [[Html::mailto()]]. |
||
525 | * @return string the formatted result. |
||
526 | */ |
||
527 | 1 | public function asEmail($value, $options = []) |
|
528 | { |
||
529 | 1 | if ($value === null) { |
|
530 | 1 | return $this->nullDisplay; |
|
531 | } |
||
532 | 1 | return Html::mailto(Html::encode($value), $value, $options); |
|
533 | } |
||
534 | |||
535 | /** |
||
536 | * Formats the value as an image tag. |
||
537 | * @param mixed $value the value to be formatted. |
||
538 | * @param array $options the tag options in terms of name-value pairs. See [[Html::img()]]. |
||
539 | * @return string the formatted result. |
||
540 | */ |
||
541 | 1 | public function asImage($value, $options = []) |
|
542 | { |
||
543 | 1 | if ($value === null) { |
|
544 | 1 | return $this->nullDisplay; |
|
545 | } |
||
546 | 1 | return Html::img($value, $options); |
|
547 | } |
||
548 | |||
549 | /** |
||
550 | * Formats the value as a hyperlink. |
||
551 | * @param mixed $value the value to be formatted. |
||
552 | * @param array $options the tag options in terms of name-value pairs. See [[Html::a()]]. |
||
553 | * @return string the formatted result. |
||
554 | */ |
||
555 | 1 | public function asUrl($value, $options = []) |
|
556 | { |
||
557 | 1 | if ($value === null) { |
|
558 | 1 | return $this->nullDisplay; |
|
559 | } |
||
560 | 1 | $url = $value; |
|
561 | 1 | if (strpos($url, '://') === false) { |
|
562 | 1 | $url = 'http://' . $url; |
|
563 | } |
||
564 | |||
565 | 1 | return Html::a(Html::encode($value), $url, $options); |
|
566 | } |
||
567 | |||
568 | /** |
||
569 | * Formats the value as a boolean. |
||
570 | * @param mixed $value the value to be formatted. |
||
571 | * @return string the formatted result. |
||
572 | * @see booleanFormat |
||
573 | */ |
||
574 | 1 | public function asBoolean($value) |
|
575 | { |
||
576 | 1 | if ($value === null) { |
|
577 | 1 | return $this->nullDisplay; |
|
578 | } |
||
579 | |||
580 | 1 | return $value ? $this->booleanFormat[1] : $this->booleanFormat[0]; |
|
581 | } |
||
582 | |||
583 | |||
584 | // date and time formats |
||
585 | |||
586 | |||
587 | /** |
||
588 | * Formats the value as a date. |
||
589 | * @param int|string|DateTime $value the value to be formatted. The following |
||
590 | * types of value are supported: |
||
591 | * |
||
592 | * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. |
||
593 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
594 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
595 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone |
||
596 | * for the DateTime object to specify the source time zone. |
||
597 | * |
||
598 | * The formatter will convert date values according to [[timeZone]] before formatting it. |
||
599 | * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. |
||
600 | * Also no conversion will be performed on values that have no time information, e.g. `"2017-06-05"`. |
||
601 | * |
||
602 | * @param string $format the format used to convert the value into a date string. |
||
603 | * If null, [[dateFormat]] will be used. |
||
604 | * |
||
605 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
606 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). |
||
607 | * |
||
608 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
609 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
610 | * |
||
611 | * @return string the formatted result. |
||
612 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
613 | * @throws InvalidConfigException if the date format is invalid. |
||
614 | * @see dateFormat |
||
615 | */ |
||
616 | 168 | public function asDate($value, $format = null) |
|
617 | { |
||
618 | 168 | if ($format === null) { |
|
619 | 146 | $format = $this->dateFormat; |
|
620 | } |
||
621 | |||
622 | 168 | return $this->formatDateTimeValue($value, $format, 'date'); |
|
623 | } |
||
624 | |||
625 | /** |
||
626 | * Formats the value as a time. |
||
627 | * @param int|string|DateTime $value the value to be formatted. The following |
||
628 | * types of value are supported: |
||
629 | * |
||
630 | * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. |
||
631 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
632 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
633 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone |
||
634 | * for the DateTime object to specify the source time zone. |
||
635 | * |
||
636 | * The formatter will convert date values according to [[timeZone]] before formatting it. |
||
637 | * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. |
||
638 | * |
||
639 | * @param string $format the format used to convert the value into a date string. |
||
640 | * If null, [[timeFormat]] will be used. |
||
641 | * |
||
642 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
643 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). |
||
644 | * |
||
645 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
646 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
647 | * |
||
648 | * @return string the formatted result. |
||
649 | * @throws InvalidParamException if the input value can not be evaluated as a date value. |
||
650 | * @throws InvalidConfigException if the date format is invalid. |
||
651 | * @see timeFormat |
||
652 | */ |
||
653 | 148 | public function asTime($value, $format = null) |
|
654 | { |
||
655 | 148 | if ($format === null) { |
|
656 | 144 | $format = $this->timeFormat; |
|
657 | } |
||
658 | |||
659 | 148 | return $this->formatDateTimeValue($value, $format, 'time'); |
|
660 | } |
||
661 | |||
662 | /** |
||
663 | * Formats the value as a datetime. |
||
664 | * @param int|string|DateTime $value the value to be formatted. The following |
||
665 | * types of value are supported: |
||
666 | * |
||
667 | * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. |
||
668 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
669 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
670 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone |
||
671 | * for the DateTime object to specify the source time zone. |
||
672 | * |
||
673 | * The formatter will convert date values according to [[timeZone]] before formatting it. |
||
674 | * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. |
||
675 | * |
||
676 | * @param string $format the format used to convert the value into a date string. |
||
677 | * If null, [[datetimeFormat]] will be used. |
||
678 | * |
||
679 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
680 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). |
||
681 | * |
||
682 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
683 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
684 | * |
||
685 | * @return string the formatted result. |
||
686 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
687 | * @throws InvalidConfigException if the date format is invalid. |
||
688 | * @see datetimeFormat |
||
689 | */ |
||
690 | 152 | public function asDatetime($value, $format = null) |
|
691 | { |
||
692 | 152 | if ($format === null) { |
|
693 | 144 | $format = $this->datetimeFormat; |
|
694 | } |
||
695 | |||
696 | 152 | return $this->formatDateTimeValue($value, $format, 'datetime'); |
|
697 | } |
||
698 | |||
699 | /** |
||
700 | * @var array map of short format names to IntlDateFormatter constant values. |
||
701 | */ |
||
702 | private $_dateFormats = [ |
||
703 | 'short' => 3, // IntlDateFormatter::SHORT, |
||
704 | 'medium' => 2, // IntlDateFormatter::MEDIUM, |
||
705 | 'long' => 1, // IntlDateFormatter::LONG, |
||
706 | 'full' => 0, // IntlDateFormatter::FULL, |
||
707 | ]; |
||
708 | |||
709 | /** |
||
710 | * @param int|string|DateTime $value the value to be formatted. The following |
||
711 | * types of value are supported: |
||
712 | * |
||
713 | * - an integer representing a UNIX timestamp |
||
714 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
715 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
716 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
717 | * |
||
718 | * @param string $format the format used to convert the value into a date string. |
||
719 | * @param string $type 'date', 'time', or 'datetime'. |
||
720 | * @throws InvalidConfigException if the date format is invalid. |
||
721 | * @return string the formatted result. |
||
722 | */ |
||
723 | 176 | private function formatDateTimeValue($value, $format, $type) |
|
782 | |||
783 | /** |
||
784 | * Normalizes the given datetime value as a DateTime object that can be taken by various date/time formatting methods. |
||
785 | * |
||
786 | * @param int|string|DateTime $value the datetime value to be normalized. The following |
||
787 | * types of value are supported: |
||
788 | * |
||
789 | * - an integer representing a UNIX timestamp |
||
790 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
791 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
792 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
793 | * |
||
794 | * @param bool $checkDateTimeInfo whether to also check if the date/time value has some time and date information attached. |
||
795 | * Defaults to `false`. If `true`, the method will then return an array with the first element being the normalized |
||
796 | * timestamp, the second a boolean indicating whether the timestamp has time information and third a boolean indicating |
||
797 | * whether the timestamp has date information. |
||
798 | * This parameter is available since version 2.0.1. |
||
799 | * @return DateTime|array the normalized datetime value. |
||
800 | * Since version 2.0.1 this may also return an array if `$checkDateTimeInfo` is true. |
||
801 | * The first element of the array is the normalized timestamp and the second is a boolean indicating whether |
||
802 | * the timestamp has time information or it is just a date value. |
||
803 | * Since version 2.0.12 the array has third boolean element indicating whether the timestamp has date information |
||
804 | * or it is just a time value. |
||
805 | * @throws InvalidParamException if the input value can not be evaluated as a date value. |
||
806 | */ |
||
807 | 181 | protected function normalizeDatetimeValue($value, $checkDateTimeInfo = false) |
|
843 | |||
844 | /** |
||
845 | * Formats a date, time or datetime in a float number as UNIX timestamp (seconds since 01-01-1970). |
||
846 | * @param int|string|DateTime $value the value to be formatted. The following |
||
847 | * types of value are supported: |
||
848 | * |
||
849 | * - an integer representing a UNIX timestamp |
||
850 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
851 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
852 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
853 | * |
||
854 | * @return string the formatted result. |
||
855 | */ |
||
856 | 145 | public function asTimestamp($value) |
|
864 | |||
865 | /** |
||
866 | * Formats the value as the time interval between a date and now in human readable form. |
||
867 | * |
||
868 | * This method can be used in three different ways: |
||
869 | * |
||
870 | * 1. Using a timestamp that is relative to `now`. |
||
871 | * 2. Using a timestamp that is relative to the `$referenceTime`. |
||
872 | * 3. Using a `DateInterval` object. |
||
873 | * |
||
874 | * @param int|string|DateTime|DateInterval $value the value to be formatted. The following |
||
875 | * types of value are supported: |
||
876 | * |
||
877 | * - an integer representing a UNIX timestamp |
||
878 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
879 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
880 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
881 | * - a PHP DateInterval object (a positive time interval will refer to the past, a negative one to the future) |
||
882 | * |
||
883 | * @param int|string|DateTime $referenceTime if specified the value is used as a reference time instead of `now` |
||
884 | * when `$value` is not a `DateInterval` object. |
||
885 | * @return string the formatted result. |
||
886 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
887 | */ |
||
888 | 92 | public function asRelativeTime($value, $referenceTime = null) |
|
968 | |||
969 | /** |
||
970 | * Represents the value as duration in human readable format. |
||
971 | * |
||
972 | * @param DateInterval|string|int $value the value to be formatted. Acceptable formats: |
||
973 | * - [DateInterval object](http://php.net/manual/ru/class.dateinterval.php) |
||
974 | * - integer - number of seconds. For example: value `131` represents `2 minutes, 11 seconds` |
||
975 | * - ISO8601 duration format. For example, all of these values represent `1 day, 2 hours, 30 minutes` duration: |
||
976 | * `2015-01-01T13:00:00Z/2015-01-02T13:30:00Z` - between two datetime values |
||
977 | * `2015-01-01T13:00:00Z/P1D2H30M` - time interval after datetime value |
||
978 | * `P1D2H30M/2015-01-02T13:30:00Z` - time interval before datetime value |
||
979 | * `P1D2H30M` - simply a date interval |
||
980 | * `P-1D2H30M` - a negative date interval (`-1 day, 2 hours, 30 minutes`) |
||
981 | * |
||
982 | * @param string $implodeString will be used to concatenate duration parts. Defaults to `, `. |
||
983 | * @param string $negativeSign will be prefixed to the formatted duration, when it is negative. Defaults to `-`. |
||
984 | * @return string the formatted duration. |
||
985 | * @since 2.0.7 |
||
986 | */ |
||
987 | 2 | public function asDuration($value, $implodeString = ', ', $negativeSign = '-') |
|
1034 | |||
1035 | |||
1036 | // number formats |
||
1037 | |||
1038 | |||
1039 | /** |
||
1040 | * Formats the value as an integer number by removing any decimal digits without rounding. |
||
1041 | * |
||
1042 | * @param mixed $value the value to be formatted. |
||
1043 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1044 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1045 | * @return string the formatted result. |
||
1046 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1047 | */ |
||
1048 | 7 | public function asInteger($value, $options = [], $textOptions = []) |
|
1067 | |||
1068 | /** |
||
1069 | * Formats the value as a decimal number. |
||
1070 | * |
||
1071 | * Property [[decimalSeparator]] will be used to represent the decimal point. The |
||
1072 | * value is rounded automatically to the defined decimal digits. |
||
1073 | * |
||
1074 | * @param mixed $value the value to be formatted. |
||
1075 | * @param int $decimals the number of digits after the decimal point. |
||
1076 | * If not given, the number of digits depends in the input value and is determined based on |
||
1077 | * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured |
||
1078 | * using [[$numberFormatterOptions]]. |
||
1079 | * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value is `2`. |
||
1080 | * If you want consistent behavior between environments where intl is available and not, you should explicitly |
||
1081 | * specify a value here. |
||
1082 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1083 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1084 | * @return string the formatted result. |
||
1085 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1086 | * @see decimalSeparator |
||
1087 | * @see thousandSeparator |
||
1088 | */ |
||
1089 | 14 | public function asDecimal($value, $decimals = null, $options = [], $textOptions = []) |
|
1112 | |||
1113 | |||
1114 | /** |
||
1115 | * Formats the value as a percent number with "%" sign. |
||
1116 | * |
||
1117 | * @param mixed $value the value to be formatted. It must be a factor e.g. `0.75` will result in `75%`. |
||
1118 | * @param int $decimals the number of digits after the decimal point. |
||
1119 | * If not given, the number of digits depends in the input value and is determined based on |
||
1120 | * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured |
||
1121 | * using [[$numberFormatterOptions]]. |
||
1122 | * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value is `0`. |
||
1123 | * If you want consistent behavior between environments where intl is available and not, you should explicitly |
||
1124 | * specify a value here. |
||
1125 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1126 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1127 | * @return string the formatted result. |
||
1128 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1129 | */ |
||
1130 | 2 | public function asPercent($value, $decimals = null, $options = [], $textOptions = []) |
|
1154 | |||
1155 | /** |
||
1156 | * Formats the value as a scientific number. |
||
1157 | * |
||
1158 | * @param mixed $value the value to be formatted. |
||
1159 | * @param int $decimals the number of digits after the decimal point. |
||
1160 | * If not given, the number of digits depends in the input value and is determined based on |
||
1161 | * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured |
||
1162 | * using [[$numberFormatterOptions]]. |
||
1163 | * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value depends on your PHP configuration. |
||
1164 | * If you want consistent behavior between environments where intl is available and not, you should explicitly |
||
1165 | * specify a value here. |
||
1166 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1167 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1168 | * @return string the formatted result. |
||
1169 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1170 | */ |
||
1171 | 2 | public function asScientific($value, $decimals = null, $options = [], $textOptions = []) |
|
1193 | |||
1194 | /** |
||
1195 | * Formats the value as a currency number. |
||
1196 | * |
||
1197 | * This function does not require the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed |
||
1198 | * to work, but it is highly recommended to install it to get good formatting results. |
||
1199 | * |
||
1200 | * @param mixed $value the value to be formatted. |
||
1201 | * @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use. |
||
1202 | * If null, [[currencyCode]] will be used. |
||
1203 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1204 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1205 | * @return string the formatted result. |
||
1206 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1207 | * @throws InvalidConfigException if no currency is given and [[currencyCode]] is not defined. |
||
1208 | */ |
||
1209 | 4 | public function asCurrency($value, $currency = null, $options = [], $textOptions = []) |
|
1245 | |||
1246 | /** |
||
1247 | * Formats the value as a number spellout. |
||
1248 | * |
||
1249 | * This function requires the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed. |
||
1250 | * |
||
1251 | * @param mixed $value the value to be formatted |
||
1252 | * @return string the formatted result. |
||
1253 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1254 | * @throws InvalidConfigException when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available. |
||
1255 | */ |
||
1256 | 1 | public function asSpellout($value) |
|
1273 | |||
1274 | /** |
||
1275 | * Formats the value as a ordinal value of a number. |
||
1276 | * |
||
1277 | * This function requires the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed. |
||
1278 | * |
||
1279 | * @param mixed $value the value to be formatted |
||
1280 | * @return string the formatted result. |
||
1281 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1282 | * @throws InvalidConfigException when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available. |
||
1283 | */ |
||
1284 | 2 | public function asOrdinal($value) |
|
1301 | |||
1302 | /** |
||
1303 | * Formats the value in bytes as a size in human readable form for example `12 KB`. |
||
1304 | * |
||
1305 | * This is the short form of [[asSize]]. |
||
1306 | * |
||
1307 | * If [[sizeFormatBase]] is 1024, [binary prefixes](http://en.wikipedia.org/wiki/Binary_prefix) (e.g. kibibyte/KiB, mebibyte/MiB, ...) |
||
1308 | * are used in the formatting result. |
||
1309 | * |
||
1310 | * @param string|int|float $value value in bytes to be formatted. |
||
1311 | * @param int $decimals the number of digits after the decimal point. |
||
1312 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1313 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1314 | * @return string the formatted result. |
||
1315 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1316 | * @see sizeFormatBase |
||
1317 | * @see asSize |
||
1318 | */ |
||
1319 | 5 | public function asShortSize($value, $decimals = null, $options = [], $textOptions = []) |
|
1359 | |||
1360 | /** |
||
1361 | * Formats the value in bytes as a size in human readable form, for example `12 kilobytes`. |
||
1362 | * |
||
1363 | * If [[sizeFormatBase]] is 1024, [binary prefixes](http://en.wikipedia.org/wiki/Binary_prefix) (e.g. kibibyte/KiB, mebibyte/MiB, ...) |
||
1364 | * are used in the formatting result. |
||
1365 | * |
||
1366 | * @param string|int|float $value value in bytes to be formatted. |
||
1367 | * @param int $decimals the number of digits after the decimal point. |
||
1368 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1369 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1370 | * @return string the formatted result. |
||
1371 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1372 | * @see sizeFormatBase |
||
1373 | * @see asShortSize |
||
1374 | */ |
||
1375 | 6 | public function asSize($value, $decimals = null, $options = [], $textOptions = []) |
|
1415 | |||
1416 | /** |
||
1417 | * Formats the value as a length in human readable form for example `12 meters`. |
||
1418 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1419 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1420 | * |
||
1421 | * @param float|int $value value to be formatted. |
||
1422 | * @param int $decimals the number of digits after the decimal point. |
||
1423 | * @param array $numberOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1424 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1425 | * @return string the formatted result. |
||
1426 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1427 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1428 | * @see asLength |
||
1429 | * @since 2.0.13 |
||
1430 | * @author John Was <[email protected]> |
||
1431 | */ |
||
1432 | public function asLength($value, $decimals = null, $numberOptions = [], $textOptions = []) |
||
1436 | |||
1437 | /** |
||
1438 | * Formats the value as a length in human readable form for example `12 m`. |
||
1439 | * This is the short form of [[asLength]]. |
||
1440 | * |
||
1441 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1442 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1443 | * |
||
1444 | * @param float|int $value value to be formatted. |
||
1445 | * @param int $decimals the number of digits after the decimal point. |
||
1446 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1447 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1448 | * @return string the formatted result. |
||
1449 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1450 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1451 | * @see asLength |
||
1452 | * @since 2.0.13 |
||
1453 | * @author John Was <[email protected]> |
||
1454 | */ |
||
1455 | 1 | public function asShortLength($value, $decimals = null, $options = [], $textOptions = []) |
|
1459 | |||
1460 | /** |
||
1461 | * Formats the value as a weight in human readable form for example `12 kilograms`. |
||
1462 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1463 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1464 | * |
||
1465 | * @param float|int $value value to be formatted. |
||
1466 | * @param int $decimals the number of digits after the decimal point. |
||
1467 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1468 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1469 | * @return string the formatted result. |
||
1470 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1471 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1472 | * @since 2.0.13 |
||
1473 | * @author John Was <[email protected]> |
||
1474 | */ |
||
1475 | 1 | public function asWeight($value, $decimals = null, $options = [], $textOptions = []) |
|
1479 | |||
1480 | /** |
||
1481 | * Formats the value as a weight in human readable form for example `12 kg`. |
||
1482 | * This is the short form of [[asWeight]]. |
||
1483 | * |
||
1484 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1485 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1486 | * |
||
1487 | * @param float|int $value value to be formatted. |
||
1488 | * @param int $decimals the number of digits after the decimal point. |
||
1489 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1490 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1491 | * @return string the formatted result. |
||
1492 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1493 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1494 | * @since 2.0.13 |
||
1495 | * @author John Was <[email protected]> |
||
1496 | */ |
||
1497 | public function asShortWeight($value, $decimals = null, $options = [], $textOptions = []) |
||
1501 | |||
1502 | /** |
||
1503 | * @param string $unitType one of [[UNIT_WEIGHT]], [[UNIT_LENGTH]] |
||
1504 | * @param string $unitFormat one of [[FORMAT_WIDTH_SHORT]], [[FORMAT_WIDTH_LONG]] |
||
1505 | * @param float|int $value to be formatted |
||
1506 | * @param float $baseUnit unit of value as the multiplier of the smallest unit. When `null`, property [[baseUnits]] |
||
1507 | * will be used to determine base unit using $unitType and $unitSystem. |
||
1508 | * @param string $unitSystem either [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. When `null`, property [[systemOfUnits]] will be used. |
||
1509 | * @param int $decimals the number of digits after the decimal point. |
||
1510 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1511 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1512 | * @return string |
||
1513 | * @throws InvalidConfigException when INTL is not installed or does not contain required information |
||
1514 | */ |
||
1515 | 2 | private function formatUnit($unitType, $unitFormat, $value, $baseUnit, $unitSystem, $decimals, $options, $textOptions) |
|
1538 | |||
1539 | /** |
||
1540 | * @param string $unitType one of [[UNIT_WEIGHT]], [[UNIT_LENGTH]] |
||
1541 | * @param string $unitFormat one of [[FORMAT_WIDTH_SHORT]], [[FORMAT_WIDTH_LONG]] |
||
1542 | * @param string $system either [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. When `null`, property [[systemOfUnits]] will be used. |
||
1543 | * @param int $position internal position of size unit |
||
1544 | * @return string |
||
1545 | * @throws InvalidConfigException when INTL is not installed or does not contain required information |
||
1546 | */ |
||
1547 | 2 | private function getUnitMessage($unitType, $unitFormat, $system, $position) |
|
1581 | |||
1582 | /** |
||
1583 | * Given the value in bytes formats number part of the human readable form. |
||
1584 | * |
||
1585 | * @param string|int|float $value value in bytes to be formatted. |
||
1586 | * @param int $decimals the number of digits after the decimal point |
||
1587 | * @param int $maxPosition maximum internal position of size unit, ignored if $formatBase is an array |
||
1588 | * @param array|int $formatBase the base at which each next unit is calculated, either 1000 or 1024, or an array |
||
1589 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1590 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1591 | * @return array [parameters for Yii::t containing formatted number, internal position of size unit] |
||
1592 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1593 | */ |
||
1594 | 11 | private function formatNumber($value, $decimals, $maxPosition, $formatBase, $options, $textOptions) |
|
1649 | |||
1650 | /** |
||
1651 | * Normalizes a numeric input value. |
||
1652 | * |
||
1653 | * - everything [empty](http://php.net/manual/en/function.empty.php) will result in `0` |
||
1654 | * - a [numeric](http://php.net/manual/en/function.is-numeric.php) string will be casted to float |
||
1655 | * - everything else will be returned if it is [numeric](http://php.net/manual/en/function.is-numeric.php), |
||
1656 | * otherwise an exception is thrown. |
||
1657 | * |
||
1658 | * @param mixed $value the input value |
||
1659 | * @return float|int the normalized number value |
||
1660 | * @throws InvalidArgumentException if the input value is not numeric. |
||
1661 | */ |
||
1662 | 31 | protected function normalizeNumericValue($value) |
|
1676 | |||
1677 | /** |
||
1678 | * Creates a number formatter based on the given type and format. |
||
1679 | * |
||
1680 | * You may override this method to create a number formatter based on patterns. |
||
1681 | * |
||
1682 | * @param int $style the type of the number formatter. |
||
1683 | * Values: NumberFormatter::DECIMAL, ::CURRENCY, ::PERCENT, ::SCIENTIFIC, ::SPELLOUT, ::ORDINAL |
||
1684 | * ::DURATION, ::PATTERN_RULEBASED, ::DEFAULT_STYLE, ::IGNORE |
||
1685 | * @param int $decimals the number of digits after the decimal point. |
||
1686 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1687 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1688 | * @return NumberFormatter the created formatter instance |
||
1689 | */ |
||
1690 | 18 | protected function createNumberFormatter($style, $decimals = null, $options = [], $textOptions = []) |
|
1728 | } |
||
1729 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.