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 | * @var bool whether the [PHP intl extension](http://php.net/manual/en/book.intl.php) is loaded. |
||
360 | */ |
||
361 | private $_intlLoaded = false; |
||
362 | /** |
||
363 | * @var \ResourceBundle cached ResourceBundle object used to read unit translations |
||
364 | */ |
||
365 | private $_resourceBundle; |
||
366 | /** |
||
367 | * @var array cached unit translation patterns |
||
368 | */ |
||
369 | private $_unitMessages = []; |
||
370 | |||
371 | |||
372 | /** |
||
373 | * @inheritdoc |
||
374 | */ |
||
375 | 257 | public function init() |
|
399 | |||
400 | /** |
||
401 | * Formats the value based on the given format type. |
||
402 | * This method will call one of the "as" methods available in this class to do the formatting. |
||
403 | * For type "xyz", the method "asXyz" will be used. For example, if the format is "html", |
||
404 | * then [[asHtml()]] will be used. Format names are case insensitive. |
||
405 | * @param mixed $value the value to be formatted. |
||
406 | * @param string|array|Closure $format the format of the value, e.g., "html", "text" or an anonymous function |
||
407 | * returning the formatted value. |
||
408 | * |
||
409 | * To specify additional parameters of the formatting method, you may use an array. |
||
410 | * The first element of the array specifies the format name, while the rest of the elements will be used as the |
||
411 | * parameters to the formatting method. For example, a format of `['date', 'Y-m-d']` will cause the invocation |
||
412 | * of `asDate($value, 'Y-m-d')`. |
||
413 | * |
||
414 | * The anonymous function signature should be: `function($value, $formatter)`, |
||
415 | * where `$value` is the value that should be formatted and `$formatter` is an instance of the Formatter class, |
||
416 | * which can be used to call other formatting functions. |
||
417 | * The possibility to use an anonymous function is available since version 2.0.13. |
||
418 | * @return string the formatting result. |
||
419 | * @throws InvalidArgumentException if the format type is not supported by this class. |
||
420 | */ |
||
421 | 11 | public function format($value, $format) |
|
443 | |||
444 | |||
445 | // simple formats |
||
446 | |||
447 | |||
448 | /** |
||
449 | * Formats the value as is without any formatting. |
||
450 | * This method simply returns back the parameter without any format. |
||
451 | * The only exception is a `null` value which will be formatted using [[nullDisplay]]. |
||
452 | * @param mixed $value the value to be formatted. |
||
453 | * @return string the formatted result. |
||
454 | */ |
||
455 | 1 | public function asRaw($value) |
|
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) |
|
477 | |||
478 | /** |
||
479 | * Formats the value as an HTML-encoded plain text with newlines converted into breaks. |
||
480 | * @param string $value the value to be formatted. |
||
481 | * @return string the formatted result. |
||
482 | */ |
||
483 | 1 | public function asNtext($value) |
|
491 | |||
492 | /** |
||
493 | * Formats the value as HTML-encoded text paragraphs. |
||
494 | * Each text paragraph is enclosed within a `<p>` tag. |
||
495 | * One or multiple consecutive empty lines divide two paragraphs. |
||
496 | * @param string $value the value to be formatted. |
||
497 | * @return string the formatted result. |
||
498 | */ |
||
499 | 1 | public function asParagraphs($value) |
|
507 | |||
508 | /** |
||
509 | * Formats the value as HTML text. |
||
510 | * The value will be purified using [[HtmlPurifier]] to avoid XSS attacks. |
||
511 | * Use [[asRaw()]] if you do not want any purification of the value. |
||
512 | * @param string $value the value to be formatted. |
||
513 | * @param array|null $config the configuration for the HTMLPurifier class. |
||
514 | * @return string the formatted result. |
||
515 | */ |
||
516 | public function asHtml($value, $config = null) |
||
524 | |||
525 | /** |
||
526 | * Formats the value as a mailto link. |
||
527 | * @param string $value the value to be formatted. |
||
528 | * @param array $options the tag options in terms of name-value pairs. See [[Html::mailto()]]. |
||
529 | * @return string the formatted result. |
||
530 | */ |
||
531 | 1 | public function asEmail($value, $options = []) |
|
539 | |||
540 | /** |
||
541 | * Formats the value as an image tag. |
||
542 | * @param mixed $value the value to be formatted. |
||
543 | * @param array $options the tag options in terms of name-value pairs. See [[Html::img()]]. |
||
544 | * @return string the formatted result. |
||
545 | */ |
||
546 | 1 | public function asImage($value, $options = []) |
|
554 | |||
555 | /** |
||
556 | * Formats the value as a hyperlink. |
||
557 | * @param mixed $value the value to be formatted. |
||
558 | * @param array $options the tag options in terms of name-value pairs. See [[Html::a()]]. |
||
559 | * @return string the formatted result. |
||
560 | */ |
||
561 | 1 | public function asUrl($value, $options = []) |
|
573 | |||
574 | /** |
||
575 | * Formats the value as a boolean. |
||
576 | * @param mixed $value the value to be formatted. |
||
577 | * @return string the formatted result. |
||
578 | * @see booleanFormat |
||
579 | */ |
||
580 | 1 | public function asBoolean($value) |
|
588 | |||
589 | |||
590 | // date and time formats |
||
591 | |||
592 | |||
593 | /** |
||
594 | * Formats the value as a date. |
||
595 | * @param int|string|DateTime $value the value to be formatted. The following |
||
596 | * types of value are supported: |
||
597 | * |
||
598 | * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. |
||
599 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
600 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
601 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone |
||
602 | * for the DateTime object to specify the source time zone. |
||
603 | * |
||
604 | * The formatter will convert date values according to [[timeZone]] before formatting it. |
||
605 | * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. |
||
606 | * Also no conversion will be performed on values that have no time information, e.g. `"2017-06-05"`. |
||
607 | * |
||
608 | * @param string $format the format used to convert the value into a date string. |
||
609 | * If null, [[dateFormat]] will be used. |
||
610 | * |
||
611 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
612 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). |
||
613 | * |
||
614 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
615 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
616 | * |
||
617 | * @return string the formatted result. |
||
618 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
619 | * @throws InvalidConfigException if the date format is invalid. |
||
620 | * @see dateFormat |
||
621 | */ |
||
622 | 168 | public function asDate($value, $format = null) |
|
630 | |||
631 | /** |
||
632 | * Formats the value as a time. |
||
633 | * @param int|string|DateTime $value the value to be formatted. The following |
||
634 | * types of value are supported: |
||
635 | * |
||
636 | * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. |
||
637 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
638 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
639 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone |
||
640 | * for the DateTime object to specify the source time zone. |
||
641 | * |
||
642 | * The formatter will convert date values according to [[timeZone]] before formatting it. |
||
643 | * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. |
||
644 | * |
||
645 | * @param string $format the format used to convert the value into a date string. |
||
646 | * If null, [[timeFormat]] will be used. |
||
647 | * |
||
648 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
649 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). |
||
650 | * |
||
651 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
652 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
653 | * |
||
654 | * @return string the formatted result. |
||
655 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
656 | * @throws InvalidConfigException if the date format is invalid. |
||
657 | * @see timeFormat |
||
658 | */ |
||
659 | 148 | public function asTime($value, $format = null) |
|
667 | |||
668 | /** |
||
669 | * Formats the value as a datetime. |
||
670 | * @param int|string|DateTime $value the value to be formatted. The following |
||
671 | * types of value are supported: |
||
672 | * |
||
673 | * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. |
||
674 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
675 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
676 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone |
||
677 | * for the DateTime object to specify the source time zone. |
||
678 | * |
||
679 | * The formatter will convert date values according to [[timeZone]] before formatting it. |
||
680 | * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. |
||
681 | * |
||
682 | * @param string $format the format used to convert the value into a date string. |
||
683 | * If null, [[datetimeFormat]] will be used. |
||
684 | * |
||
685 | * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. |
||
686 | * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). |
||
687 | * |
||
688 | * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the |
||
689 | * PHP [date()](http://php.net/manual/en/function.date.php)-function. |
||
690 | * |
||
691 | * @return string the formatted result. |
||
692 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
693 | * @throws InvalidConfigException if the date format is invalid. |
||
694 | * @see datetimeFormat |
||
695 | */ |
||
696 | 152 | public function asDatetime($value, $format = null) |
|
704 | |||
705 | /** |
||
706 | * @var array map of short format names to IntlDateFormatter constant values. |
||
707 | */ |
||
708 | private $_dateFormats = [ |
||
709 | 'short' => 3, // IntlDateFormatter::SHORT, |
||
710 | 'medium' => 2, // IntlDateFormatter::MEDIUM, |
||
711 | 'long' => 1, // IntlDateFormatter::LONG, |
||
712 | 'full' => 0, // IntlDateFormatter::FULL, |
||
713 | ]; |
||
714 | |||
715 | /** |
||
716 | * @param int|string|DateTime $value the value to be formatted. The following |
||
717 | * types of value are supported: |
||
718 | * |
||
719 | * - an integer representing a UNIX timestamp |
||
720 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
721 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
722 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
723 | * |
||
724 | * @param string $format the format used to convert the value into a date string. |
||
725 | * @param string $type 'date', 'time', or 'datetime'. |
||
726 | * @throws InvalidConfigException if the date format is invalid. |
||
727 | * @return string the formatted result. |
||
728 | */ |
||
729 | 176 | private function formatDateTimeValue($value, $format, $type) |
|
788 | |||
789 | /** |
||
790 | * Normalizes the given datetime value as a DateTime object that can be taken by various date/time formatting methods. |
||
791 | * |
||
792 | * @param int|string|DateTime $value the datetime value to be normalized. The following |
||
793 | * types of value are supported: |
||
794 | * |
||
795 | * - an integer representing a UNIX timestamp |
||
796 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
797 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
798 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
799 | * |
||
800 | * @param bool $checkDateTimeInfo whether to also check if the date/time value has some time and date information attached. |
||
801 | * Defaults to `false`. If `true`, the method will then return an array with the first element being the normalized |
||
802 | * timestamp, the second a boolean indicating whether the timestamp has time information and third a boolean indicating |
||
803 | * whether the timestamp has date information. |
||
804 | * This parameter is available since version 2.0.1. |
||
805 | * @return DateTime|array the normalized datetime value. |
||
806 | * Since version 2.0.1 this may also return an array if `$checkDateTimeInfo` is true. |
||
807 | * The first element of the array is the normalized timestamp and the second is a boolean indicating whether |
||
808 | * the timestamp has time information or it is just a date value. |
||
809 | * Since version 2.0.12 the array has third boolean element indicating whether the timestamp has date information |
||
810 | * or it is just a time value. |
||
811 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
812 | */ |
||
813 | 181 | protected function normalizeDatetimeValue($value, $checkDateTimeInfo = false) |
|
849 | |||
850 | /** |
||
851 | * Formats a date, time or datetime in a float number as UNIX timestamp (seconds since 01-01-1970). |
||
852 | * @param int|string|DateTime $value the value to be formatted. The following |
||
853 | * types of value are supported: |
||
854 | * |
||
855 | * - an integer representing a UNIX timestamp |
||
856 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
857 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
858 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
859 | * |
||
860 | * @return string the formatted result. |
||
861 | */ |
||
862 | 145 | public function asTimestamp($value) |
|
870 | |||
871 | /** |
||
872 | * Formats the value as the time interval between a date and now in human readable form. |
||
873 | * |
||
874 | * This method can be used in three different ways: |
||
875 | * |
||
876 | * 1. Using a timestamp that is relative to `now`. |
||
877 | * 2. Using a timestamp that is relative to the `$referenceTime`. |
||
878 | * 3. Using a `DateInterval` object. |
||
879 | * |
||
880 | * @param int|string|DateTime|DateInterval $value the value to be formatted. The following |
||
881 | * types of value are supported: |
||
882 | * |
||
883 | * - an integer representing a UNIX timestamp |
||
884 | * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). |
||
885 | * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. |
||
886 | * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object |
||
887 | * - a PHP DateInterval object (a positive time interval will refer to the past, a negative one to the future) |
||
888 | * |
||
889 | * @param int|string|DateTime $referenceTime if specified the value is used as a reference time instead of `now` |
||
890 | * when `$value` is not a `DateInterval` object. |
||
891 | * @return string the formatted result. |
||
892 | * @throws InvalidArgumentException if the input value can not be evaluated as a date value. |
||
893 | */ |
||
894 | 92 | public function asRelativeTime($value, $referenceTime = null) |
|
974 | |||
975 | /** |
||
976 | * Represents the value as duration in human readable format. |
||
977 | * |
||
978 | * @param DateInterval|string|int $value the value to be formatted. Acceptable formats: |
||
979 | * - [DateInterval object](http://php.net/manual/ru/class.dateinterval.php) |
||
980 | * - integer - number of seconds. For example: value `131` represents `2 minutes, 11 seconds` |
||
981 | * - ISO8601 duration format. For example, all of these values represent `1 day, 2 hours, 30 minutes` duration: |
||
982 | * `2015-01-01T13:00:00Z/2015-01-02T13:30:00Z` - between two datetime values |
||
983 | * `2015-01-01T13:00:00Z/P1D2H30M` - time interval after datetime value |
||
984 | * `P1D2H30M/2015-01-02T13:30:00Z` - time interval before datetime value |
||
985 | * `P1D2H30M` - simply a date interval |
||
986 | * `P-1D2H30M` - a negative date interval (`-1 day, 2 hours, 30 minutes`) |
||
987 | * |
||
988 | * @param string $implodeString will be used to concatenate duration parts. Defaults to `, `. |
||
989 | * @param string $negativeSign will be prefixed to the formatted duration, when it is negative. Defaults to `-`. |
||
990 | * @return string the formatted duration. |
||
991 | * @since 2.0.7 |
||
992 | */ |
||
993 | 2 | public function asDuration($value, $implodeString = ', ', $negativeSign = '-') |
|
1040 | |||
1041 | |||
1042 | // number formats |
||
1043 | |||
1044 | |||
1045 | /** |
||
1046 | * Formats the value as an integer number by removing any decimal digits without rounding. |
||
1047 | * |
||
1048 | * @param mixed $value the value to be formatted. |
||
1049 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1050 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1051 | * @return string the formatted result. |
||
1052 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1053 | */ |
||
1054 | 7 | public function asInteger($value, $options = [], $textOptions = []) |
|
1073 | |||
1074 | /** |
||
1075 | * Formats the value as a decimal number. |
||
1076 | * |
||
1077 | * Property [[decimalSeparator]] will be used to represent the decimal point. The |
||
1078 | * value is rounded automatically to the defined decimal digits. |
||
1079 | * |
||
1080 | * @param mixed $value the value to be formatted. |
||
1081 | * @param int $decimals the number of digits after the decimal point. |
||
1082 | * If not given, the number of digits depends in the input value and is determined based on |
||
1083 | * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured |
||
1084 | * using [[$numberFormatterOptions]]. |
||
1085 | * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value is `2`. |
||
1086 | * If you want consistent behavior between environments where intl is available and not, you should explicitly |
||
1087 | * specify a value here. |
||
1088 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1089 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1090 | * @return string the formatted result. |
||
1091 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1092 | * @see decimalSeparator |
||
1093 | * @see thousandSeparator |
||
1094 | */ |
||
1095 | 14 | public function asDecimal($value, $decimals = null, $options = [], $textOptions = []) |
|
1118 | |||
1119 | |||
1120 | /** |
||
1121 | * Formats the value as a percent number with "%" sign. |
||
1122 | * |
||
1123 | * @param mixed $value the value to be formatted. It must be a factor e.g. `0.75` will result in `75%`. |
||
1124 | * @param int $decimals the number of digits after the decimal point. |
||
1125 | * If not given, the number of digits depends in the input value and is determined based on |
||
1126 | * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured |
||
1127 | * using [[$numberFormatterOptions]]. |
||
1128 | * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value is `0`. |
||
1129 | * If you want consistent behavior between environments where intl is available and not, you should explicitly |
||
1130 | * specify a value here. |
||
1131 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1132 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1133 | * @return string the formatted result. |
||
1134 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1135 | */ |
||
1136 | 2 | public function asPercent($value, $decimals = null, $options = [], $textOptions = []) |
|
1160 | |||
1161 | /** |
||
1162 | * Formats the value as a scientific number. |
||
1163 | * |
||
1164 | * @param mixed $value the value to be formatted. |
||
1165 | * @param int $decimals the number of digits after the decimal point. |
||
1166 | * If not given, the number of digits depends in the input value and is determined based on |
||
1167 | * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured |
||
1168 | * using [[$numberFormatterOptions]]. |
||
1169 | * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value depends on your PHP configuration. |
||
1170 | * If you want consistent behavior between environments where intl is available and not, you should explicitly |
||
1171 | * specify a value here. |
||
1172 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1173 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1174 | * @return string the formatted result. |
||
1175 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1176 | */ |
||
1177 | 2 | public function asScientific($value, $decimals = null, $options = [], $textOptions = []) |
|
1200 | |||
1201 | /** |
||
1202 | * Formats the value as a currency number. |
||
1203 | * |
||
1204 | * This function does not require the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed |
||
1205 | * to work, but it is highly recommended to install it to get good formatting results. |
||
1206 | * |
||
1207 | * @param mixed $value the value to be formatted. |
||
1208 | * @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use. |
||
1209 | * If null, [[currencyCode]] will be used. |
||
1210 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1211 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1212 | * @return string the formatted result. |
||
1213 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1214 | * @throws InvalidConfigException if no currency is given and [[currencyCode]] is not defined. |
||
1215 | */ |
||
1216 | 4 | public function asCurrency($value, $currency = null, $options = [], $textOptions = []) |
|
1252 | |||
1253 | /** |
||
1254 | * Formats the value as a number spellout. |
||
1255 | * |
||
1256 | * This function requires the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed. |
||
1257 | * |
||
1258 | * @param mixed $value the value to be formatted |
||
1259 | * @return string the formatted result. |
||
1260 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1261 | * @throws InvalidConfigException when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available. |
||
1262 | */ |
||
1263 | 1 | public function asSpellout($value) |
|
1281 | |||
1282 | /** |
||
1283 | * Formats the value as a ordinal value of a number. |
||
1284 | * |
||
1285 | * This function requires the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed. |
||
1286 | * |
||
1287 | * @param mixed $value the value to be formatted |
||
1288 | * @return string the formatted result. |
||
1289 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1290 | * @throws InvalidConfigException when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available. |
||
1291 | */ |
||
1292 | 2 | public function asOrdinal($value) |
|
1310 | |||
1311 | /** |
||
1312 | * Formats the value in bytes as a size in human readable form for example `12 KB`. |
||
1313 | * |
||
1314 | * This is the short form of [[asSize]]. |
||
1315 | * |
||
1316 | * If [[sizeFormatBase]] is 1024, [binary prefixes](http://en.wikipedia.org/wiki/Binary_prefix) (e.g. kibibyte/KiB, mebibyte/MiB, ...) |
||
1317 | * are used in the formatting result. |
||
1318 | * |
||
1319 | * @param string|int|float $value value in bytes to be formatted. |
||
1320 | * @param int $decimals the number of digits after the decimal point. |
||
1321 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1322 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1323 | * @return string the formatted result. |
||
1324 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1325 | * @see sizeFormatBase |
||
1326 | * @see asSize |
||
1327 | */ |
||
1328 | 5 | public function asShortSize($value, $decimals = null, $options = [], $textOptions = []) |
|
1368 | |||
1369 | /** |
||
1370 | * Formats the value in bytes as a size in human readable form, for example `12 kilobytes`. |
||
1371 | * |
||
1372 | * If [[sizeFormatBase]] is 1024, [binary prefixes](http://en.wikipedia.org/wiki/Binary_prefix) (e.g. kibibyte/KiB, mebibyte/MiB, ...) |
||
1373 | * are used in the formatting result. |
||
1374 | * |
||
1375 | * @param string|int|float $value value in bytes to be formatted. |
||
1376 | * @param int $decimals the number of digits after the decimal point. |
||
1377 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1378 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1379 | * @return string the formatted result. |
||
1380 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1381 | * @see sizeFormatBase |
||
1382 | * @see asShortSize |
||
1383 | */ |
||
1384 | 6 | public function asSize($value, $decimals = null, $options = [], $textOptions = []) |
|
1424 | |||
1425 | /** |
||
1426 | * Formats the value as a length in human readable form for example `12 meters`. |
||
1427 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1428 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1429 | * |
||
1430 | * @param float|int $value value to be formatted. |
||
1431 | * @param int $decimals the number of digits after the decimal point. |
||
1432 | * @param array $numberOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1433 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1434 | * @return string the formatted result. |
||
1435 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1436 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1437 | * @see asLength |
||
1438 | * @since 2.0.13 |
||
1439 | * @author John Was <[email protected]> |
||
1440 | */ |
||
1441 | public function asLength($value, $decimals = null, $numberOptions = [], $textOptions = []) |
||
1445 | |||
1446 | /** |
||
1447 | * Formats the value as a length in human readable form for example `12 m`. |
||
1448 | * This is the short form of [[asLength]]. |
||
1449 | * |
||
1450 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1451 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1452 | * |
||
1453 | * @param float|int $value value to be formatted. |
||
1454 | * @param int $decimals the number of digits after the decimal point. |
||
1455 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1456 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1457 | * @return string the formatted result. |
||
1458 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1459 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1460 | * @see asLength |
||
1461 | * @since 2.0.13 |
||
1462 | * @author John Was <[email protected]> |
||
1463 | */ |
||
1464 | 1 | public function asShortLength($value, $decimals = null, $options = [], $textOptions = []) |
|
1468 | |||
1469 | /** |
||
1470 | * Formats the value as a weight in human readable form for example `12 kilograms`. |
||
1471 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1472 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1473 | * |
||
1474 | * @param float|int $value value to be formatted. |
||
1475 | * @param int $decimals the number of digits after the decimal point. |
||
1476 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1477 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1478 | * @return string the formatted result. |
||
1479 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1480 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1481 | * @since 2.0.13 |
||
1482 | * @author John Was <[email protected]> |
||
1483 | */ |
||
1484 | 1 | public function asWeight($value, $decimals = null, $options = [], $textOptions = []) |
|
1488 | |||
1489 | /** |
||
1490 | * Formats the value as a weight in human readable form for example `12 kg`. |
||
1491 | * This is the short form of [[asWeight]]. |
||
1492 | * |
||
1493 | * Check properties [[baseUnits]] if you need to change unit of value as the multiplier |
||
1494 | * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. |
||
1495 | * |
||
1496 | * @param float|int $value value to be formatted. |
||
1497 | * @param int $decimals the number of digits after the decimal point. |
||
1498 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1499 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1500 | * @return string the formatted result. |
||
1501 | * @throws InvalidParamException if the input value is not numeric or the formatting failed. |
||
1502 | * @throws InvalidConfigException when INTL is not installed or does not contain required information. |
||
1503 | * @since 2.0.13 |
||
1504 | * @author John Was <[email protected]> |
||
1505 | */ |
||
1506 | public function asShortWeight($value, $decimals = null, $options = [], $textOptions = []) |
||
1510 | |||
1511 | /** |
||
1512 | * @param string $unitType one of [[UNIT_WEIGHT]], [[UNIT_LENGTH]] |
||
1513 | * @param string $unitFormat one of [[FORMAT_WIDTH_SHORT]], [[FORMAT_WIDTH_LONG]] |
||
1514 | * @param float|int $value to be formatted |
||
1515 | * @param float $baseUnit unit of value as the multiplier of the smallest unit. When `null`, property [[baseUnits]] |
||
1516 | * will be used to determine base unit using $unitType and $unitSystem. |
||
1517 | * @param string $unitSystem either [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. When `null`, property [[systemOfUnits]] will be used. |
||
1518 | * @param int $decimals the number of digits after the decimal point. |
||
1519 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1520 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1521 | * @return string |
||
1522 | * @throws InvalidConfigException when INTL is not installed or does not contain required information |
||
1523 | */ |
||
1524 | 2 | private function formatUnit($unitType, $unitFormat, $value, $baseUnit, $unitSystem, $decimals, $options, $textOptions) |
|
1547 | |||
1548 | /** |
||
1549 | * @param string $unitType one of [[UNIT_WEIGHT]], [[UNIT_LENGTH]] |
||
1550 | * @param string $unitFormat one of [[FORMAT_WIDTH_SHORT]], [[FORMAT_WIDTH_LONG]] |
||
1551 | * @param string $system either [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. When `null`, property [[systemOfUnits]] will be used. |
||
1552 | * @param int $position internal position of size unit |
||
1553 | * @return string |
||
1554 | * @throws InvalidConfigException when INTL is not installed or does not contain required information |
||
1555 | */ |
||
1556 | 2 | private function getUnitMessage($unitType, $unitFormat, $system, $position) |
|
1590 | |||
1591 | /** |
||
1592 | * Given the value in bytes formats number part of the human readable form. |
||
1593 | * |
||
1594 | * @param string|int|float $value value in bytes to be formatted. |
||
1595 | * @param int $decimals the number of digits after the decimal point |
||
1596 | * @param int $maxPosition maximum internal position of size unit, ignored if $formatBase is an array |
||
1597 | * @param array|int $formatBase the base at which each next unit is calculated, either 1000 or 1024, or an array |
||
1598 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1599 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1600 | * @return array [parameters for Yii::t containing formatted number, internal position of size unit] |
||
1601 | * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. |
||
1602 | */ |
||
1603 | 11 | private function formatNumber($value, $decimals, $maxPosition, $formatBase, $options, $textOptions) |
|
1658 | |||
1659 | /** |
||
1660 | * Normalizes a numeric input value. |
||
1661 | * |
||
1662 | * - everything [empty](http://php.net/manual/en/function.empty.php) will result in `0` |
||
1663 | * - a [numeric](http://php.net/manual/en/function.is-numeric.php) string will be casted to float |
||
1664 | * - everything else will be returned if it is [numeric](http://php.net/manual/en/function.is-numeric.php), |
||
1665 | * otherwise an exception is thrown. |
||
1666 | * |
||
1667 | * @param mixed $value the input value |
||
1668 | * @return float|int the normalized number value |
||
1669 | * @throws InvalidArgumentException if the input value is not numeric. |
||
1670 | */ |
||
1671 | 31 | protected function normalizeNumericValue($value) |
|
1685 | |||
1686 | /** |
||
1687 | * Creates a number formatter based on the given type and format. |
||
1688 | * |
||
1689 | * You may override this method to create a number formatter based on patterns. |
||
1690 | * |
||
1691 | * @param int $style the type of the number formatter. |
||
1692 | * Values: NumberFormatter::DECIMAL, ::CURRENCY, ::PERCENT, ::SCIENTIFIC, ::SPELLOUT, ::ORDINAL |
||
1693 | * ::DURATION, ::PATTERN_RULEBASED, ::DEFAULT_STYLE, ::IGNORE |
||
1694 | * @param int $decimals the number of digits after the decimal point. |
||
1695 | * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. |
||
1696 | * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. |
||
1697 | * @return NumberFormatter the created formatter instance |
||
1698 | */ |
||
1699 | 18 | protected function createNumberFormatter($style, $decimals = null, $options = [], $textOptions = []) |
|
1737 | } |
||
1738 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.