Format
in component
Formats and parses values (numbers, prices, dates, weights, lengths, sizes) with language based settings.
Table of Contents
Properties
Methods
- __construct() : mixed
- Loads language strings and length and weight classes.
- convertToByte() : int
- Converts values like 10M to bytes. Supports 'k', 'm' and 'g' suffixes.
- convertToMegabyte() : string
- Converts a value to human readable format like 10.1 Mb.
- formatDate() : bool
- Converts a date to YYYY-MM-DD format. Supports 'yyyy-mm-dd', 'mm/dd/yyyy' and 'dd.mm.yyyy'.
- formatLength() : string
- Formats a length with 2 decimals and its unit.
- formatPassedTime() : string
- Formats a time interval like '1 days 2 hours 3 minutes 4 seconds'.
- formatPrice() : float
- Converts a price to float.
- formatWeight() : string
- Formats a weight with 2 decimals and its unit.
- getLengthClassIdByUnit() : int|string
- Returns a length class ID by its unit.
- getLengthUnit() : string
- Returns a length unit by the length class ID.
- getWeightClassIdByUnit() : int|string
- Returns a weight class ID by its unit.
- getWeightUnit() : string
- Returns a weight unit by the weight class ID.
- parseDate() : string
- Parses a date string.
- parseLength() : array<string|int, mixed>
- Parses a length value like '10cm'. The function does NOT create a new length class.
- parseNumber() : string
- Removes all characters except digits, '-', '.', ',' and '`' and replaces commas with dots.
- parsePrice() : float|string
- Parses a price. Supports values like '.99', '0,99', '$1,200.00', '123,456.78' => '123456.78'.
- parseWeight() : array<string|int, mixed>
- Parses a weight value like '0.0234g'. The function does NOT create a new weight class.
- strip() : string
- Trims the string and removes the characters from its beginning and end.
Properties
$config
protected
mixed
$config
$db
protected
mixed
$db
$language
protected
mixed
$language
$language_id
protected
mixed
$language_id
$length_classes_by_id
protected
mixed
$length_classes_by_id
$length_classes_by_unit
protected
mixed
$length_classes_by_unit
$model_localisation_language
protected
mixed
$model_localisation_language
$registry
protected
mixed
$registry
$weight_classes_by_id
protected
mixed
$weight_classes_by_id
$weight_classes_by_unit
protected
mixed
$weight_classes_by_unit
Methods
__construct()
Loads language strings and length and weight classes.
public
__construct([mixed $language_id = null ]) : mixed
$language_id — language ID; the current admin or store language by default.
Parameters
- $language_id : mixed = null
convertToByte()
Converts values like 10M to bytes. Supports 'k', 'm' and 'g' suffixes.
public
convertToByte(mixed $file_size) : int
$file_size — size string.
Parameters
- $file_size : mixed
Return values
intconvertToMegabyte()
Converts a value to human readable format like 10.1 Mb.
public
convertToMegabyte(mixed $val) : string
$val — number of bytes or a size string like 10M.
Parameters
- $val : mixed
Return values
stringformatDate()
Converts a date to YYYY-MM-DD format. Supports 'yyyy-mm-dd', 'mm/dd/yyyy' and 'dd.mm.yyyy'.
public
formatDate(mixed &$date) : bool
$date — date string, it is replaced with the formatted date.
Parameters
- $date : mixed
Return values
bool —True if the date was recognized.
formatLength()
Formats a length with 2 decimals and its unit.
public
formatLength(mixed $val, mixed $class_id[, mixed $decimal_point = '.' ][, mixed $thousand_point = '' ]) : string
$val — length value. $class_id — length class ID. $decimal_point — decimal separator. $thousand_point — thousands separator.
Parameters
- $val : mixed
- $class_id : mixed
- $decimal_point : mixed = '.'
- $thousand_point : mixed = ''
Return values
stringformatPassedTime()
Formats a time interval like '1 days 2 hours 3 minutes 4 seconds'.
public
formatPassedTime(mixed $diff) : string
$diff — number of seconds since specific moment.
Parameters
- $diff : mixed
Return values
stringformatPrice()
Converts a price to float.
public
formatPrice(mixed $price) : float
$price — price value.
Parameters
- $price : mixed
Return values
floatformatWeight()
Formats a weight with 2 decimals and its unit.
public
formatWeight(mixed $val, mixed $class_id[, mixed $decimal_point = '.' ][, mixed $thousand_point = '' ]) : string
$val — weight value. $class_id — weight class ID. $decimal_point — decimal separator. $thousand_point — thousands separator.
Parameters
- $val : mixed
- $class_id : mixed
- $decimal_point : mixed = '.'
- $thousand_point : mixed = ''
Return values
stringgetLengthClassIdByUnit()
Returns a length class ID by its unit.
public
getLengthClassIdByUnit(mixed $unit) : int|string
$unit — unit like 'cm'.
Parameters
- $unit : mixed
Return values
int|string —Empty string if the unit is not found.
getLengthUnit()
Returns a length unit by the length class ID.
public
getLengthUnit(mixed $class_id) : string
$class_id — length class ID.
Parameters
- $class_id : mixed
Return values
stringgetWeightClassIdByUnit()
Returns a weight class ID by its unit.
public
getWeightClassIdByUnit(mixed $unit) : int|string
$unit — unit like 'kg'.
Parameters
- $unit : mixed
Return values
int|string —Empty string if the unit is not found.
getWeightUnit()
Returns a weight unit by the weight class ID.
public
getWeightUnit(mixed $class_id) : string
$class_id — weight class ID.
Parameters
- $class_id : mixed
Return values
stringparseDate()
Parses a date string.
public
parseDate(mixed $str) : string
$str — date string.
Parameters
- $str : mixed
Return values
string —Date in 'Y-m-d H:i:s' format; empty string on error.
parseLength()
Parses a length value like '10cm'. The function does NOT create a new length class.
public
parseLength(mixed $length) : array<string|int, mixed>
$length — length string.
Parameters
- $length : mixed
Return values
array<string|int, mixed> —Array with 'value' and 'length_class_id' keys.
parseNumber()
Removes all characters except digits, '-', '.', ',' and '`' and replaces commas with dots.
public
static parseNumber(mixed $number) : string
$number — value to parse.
Parameters
- $number : mixed
Return values
stringparsePrice()
Parses a price. Supports values like '.99', '0,99', '$1,200.00', '123,456.78' => '123456.78'.
public
static parsePrice(mixed $price) : float|string
$price — value to parse.
Parameters
- $price : mixed
Return values
float|stringparseWeight()
Parses a weight value like '0.0234g'. The function does NOT create a new weight class.
public
parseWeight(mixed $weight) : array<string|int, mixed>
$weight — weight string.
Parameters
- $weight : mixed
Return values
array<string|int, mixed> —Array like array('value' => 0.0234, 'weight_class_id' => 4).
strip()
Trims the string and removes the characters from its beginning and end.
public
strip(mixed $str, mixed $chars) : string
$str — string. $chars — a character or array of characters.
Parameters
- $str : mixed
- $chars : mixed