UnitConverter implements UnitConverterInterface
The actual unit converter object. Extend this object if you would like to implement your own custom converter.
Tags
Interfaces, Classes and Traits
- UnitConverterInterface
- The interface for any and all unit converter classes.
Table of Contents
- CONVERSION_HASH_LENGTH = [0, 7]
- $calculator : CalculatorInterface
- The converters internal calculator used to handle mathematical operations
- $convert : float
- The value being converted.
- $from : string
- The unit of measure being converted **from**.
- $log : array<string|int, mixed>
- The log of events for the current conversion calculations
- $loggingEnabled : bool
- Are conversions going to be logged?
- $precision : int
- The decimal precision to be calculated
- $registry : UnitRegistryInterface
- The registry that the unit converter accesses available units from
- $to : string
- The unit of measure being converted **to**.
- $hash : string
- The current conversions unique hash.
- $types : array<string|int, mixed>
- A static array of supported scalar types for a unit's value.
- __construct() : mixed
- Public constructor function for the UnitConverter class.
- all() : array<string|int, mixed>
- Convert a unit to all other possible units of measurement. The results will be an associative array in the form of `symbol => conversion`.
- binary() : UnitConverterInterface
- Returns a unit converter set up for binary use cases
- calculatorExists() : bool
- Determine whether or not the converter has an active calculator.
- convert() : static
- Set the unit converters' value to be converted. This method is the first method to be called in the chain of conversion methods.
- createBuilder() : ConverterBuilder
- Returns a builder object for quickly scaffolding out a new converter.
- default() : UnitConverterInterface
- Returns a unit converter set up for common use cases
- disableConversionLog() : UnitConverterInterface
- Disables the logging of conversions & their order of operations.
- enableConversionLog() : UnitConverterInterface
- Enables the logging of conversions & their order of operations.
- from() : static
- Set the unit converters' unit to be converted **from**. This method is the second to be called in the chain of conversion methods.
- getCalculator() : CalculatorInterface
- Return the current calculator instance for the unit converter.
- getConversionLog() : array<string|int, mixed>
- Return an array, containing a list of events in the order they occured for the current calculation.
- getRegistry() : UnitRegistryInterface
- Return the current registry instance for the unit converter.
- registryExists() : bool
- Determine whether or not the converter has an active registry.
- setCalculator() : UnitConverterInterface
- Set the unit converter calculator to perform mathematical operations with.
- setRegistry() : UnitConverterInterface
- Set the unit converter registry for storing units of measure to convert values with.
- spellout() : string
- Like `to`, but will present the conversion result as words instead of a numeric value.
- to() : int|float|string
- Set the unit converters' unit to be converted **to**. This method is the third to be called in the chain of conversion methods.
- whichCalculator() : null|string
- Determine which calculator is currently being used
- conversionExists() : bool
- Generates a conversion hash for the current set of parameters & checks if it matches any previous conversions. If logging is disabled, a conversion hash **will not** be generated & the check will not be made.
- generateConversionHash() : string
- Sets & returns a unique md5 hash of the current conversion.
- getConversionHash() : null|string
- Return a unique md5 hash of the current conversion.
Constants
CONVERSION_HASH_LENGTH
public
mixed
CONVERSION_HASH_LENGTH
= [0, 7]
Properties
$calculator
The converters internal calculator used to handle mathematical operations
protected
CalculatorInterface
$calculator
$convert
The value being converted.
protected
float
$convert
$from
The unit of measure being converted **from**.
protected
string
$from
$log
The log of events for the current conversion calculations
protected
array<string|int, mixed>
$log
= []
$loggingEnabled
Are conversions going to be logged?
protected
bool
$loggingEnabled
$precision
The decimal precision to be calculated
protected
int
$precision
$registry
The registry that the unit converter accesses available units from
protected
UnitRegistryInterface
$registry
$to
The unit of measure being converted **to**.
protected
string
$to
$hash
The current conversions unique hash.
private
string
$hash
$types
A static array of supported scalar types for a unit's value.
private
static array<string|int, mixed>
$types
= ["int", "float", "string"]
Methods
__construct()
Public constructor function for the UnitConverter class.
public
__construct(array<string|int, UnitInterface> $registry, CalculatorInterface $calculator) : mixed
Parameters
- $registry : array<string|int, UnitInterface>
-
A two-dimensional array of UnitInterface objects.
- $calculator : CalculatorInterface
-
The calculator that the converter will use to perform mathematical operations.
Return values
mixed —all()
Convert a unit to all other possible units of measurement. The results will be an associative array in the form of `symbol => conversion`.
public
all() : array<string|int, mixed>
Return values
array<string|int, mixed> —binary()
Returns a unit converter set up for binary use cases
public
static binary() : UnitConverterInterface
Return values
UnitConverterInterface —calculatorExists()
Determine whether or not the converter has an active calculator.
public
calculatorExists() : bool
Return values
bool —convert()
Set the unit converters' value to be converted. This method is the first method to be called in the chain of conversion methods.
public
convert(mixed $value[, int $precision = null ]) : static
Parameters
- $value : mixed
-
The numerical value being converted.
- $precision : int = null
-
The decimal precision to be rounded to
Return values
static —createBuilder()
Returns a builder object for quickly scaffolding out a new converter.
public
static createBuilder() : ConverterBuilder
Return values
ConverterBuilder —default()
Returns a unit converter set up for common use cases
public
static default() : UnitConverterInterface
Return values
UnitConverterInterface —disableConversionLog()
Disables the logging of conversions & their order of operations.
public
disableConversionLog() : UnitConverterInterface
Return values
UnitConverterInterface —enableConversionLog()
Enables the logging of conversions & their order of operations.
public
enableConversionLog() : UnitConverterInterface
Return values
UnitConverterInterface —from()
Set the unit converters' unit to be converted **from**. This method is the second to be called in the chain of conversion methods.
public
from(string $unit) : static
Parameters
- $unit : string
-
The unit being conerted from. The unit must first be registered to the UnitRegistry.
Return values
static —getCalculator()
Return the current calculator instance for the unit converter.
public
getCalculator() : CalculatorInterface
Return values
CalculatorInterface —getConversionLog()
Return an array, containing a list of events in the order they occured for the current calculation.
public
getConversionLog() : array<string|int, mixed>
Return values
array<string|int, mixed> —getRegistry()
Return the current registry instance for the unit converter.
public
getRegistry() : UnitRegistryInterface
Return values
UnitRegistryInterface —registryExists()
Determine whether or not the converter has an active registry.
public
registryExists() : bool
Return values
bool —setCalculator()
Set the unit converter calculator to perform mathematical operations with.
public
setCalculator(CalculatorInterface $calculator) : UnitConverterInterface
Parameters
- $calculator : CalculatorInterface
-
An instance of a CalculatorInterface
Return values
UnitConverterInterface —setRegistry()
Set the unit converter registry for storing units of measure to convert values with.
public
setRegistry(UnitRegistryInterface $registry) : UnitConverterInterface
Parameters
- $registry : UnitRegistryInterface
-
An instance of UnitRegistry.
Return values
UnitConverterInterface —spellout()
Like `to`, but will present the conversion result as words instead of a numeric value.
public
spellout(string $unit[, string $locale = 'en_CA' ]) : string
Parameters
- $unit : string
-
The unit being converted to. The unit must first be registered to the UnitRegistry.
- $locale : string = 'en_CA'
-
The locale to translate the number with. Defaults to Canadian English
Tags
Return values
string —to()
Set the unit converters' unit to be converted **to**. This method is the third to be called in the chain of conversion methods.
public
to(string $unit) : int|float|string
Parameters
- $unit : string
-
The unit being converted to. The unit must first be registered to the UnitRegistry.
Return values
int|float|string —whichCalculator()
Determine which calculator is currently being used
public
whichCalculator() : null|string
Return values
null|string —conversionExists()
Generates a conversion hash for the current set of parameters & checks if it matches any previous conversions. If logging is disabled, a conversion hash **will not** be generated & the check will not be made.
private
conversionExists() : bool
Return values
bool —generateConversionHash()
Sets & returns a unique md5 hash of the current conversion.
private
generateConversionHash() : string
Return values
string —getConversionHash()
Return a unique md5 hash of the current conversion.
private
getConversionHash() : null|string