Fr3nch13/CakePHP Utilities

NumberHelper extends Helper
in package

Number helper library.

Methods to make numbers more readable.

Tags
link
https://book.cakephp.org/4/en/views/helpers/number.html
see
Number

Table of Contents

Properties

$_config  : array<string, mixed>
Runtime config
$_configInitialized  : bool
Whether the config property has already been configured with defaults
$_defaultConfig  : array<string, mixed>
Default config for this class
$_engine  : Number
Cake\I18n\Number instance
$_helperMap  : array<string, array<string|int, mixed>>
A helper lookup table used to lazy load helper objects.
$_View  : View
The View instance this helper is attached to
$helpers  : array<string|int, mixed>
List of helpers used by this helper

Methods

__call()  : mixed
Call methods from Cake\I18n\Number utility class
__construct()  : mixed
Default Constructor
__debugInfo()  : array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
__get()  : Helper|null|void
Lazy loads helpers.
addClass()  : array<string, mixed>
Adds the given class to the element options
configShallow()  : $this
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
currency()  : string
Formats a number into a currency format.
defaultCurrency()  : string|null
Getter/setter for default currency
format()  : string
Formats a number into the correct locale format
formatDelta()  : string
Formats a number into the correct locale format to show deltas (signed differences in value).
getConfig()  : mixed
Returns the config.
getConfigOrFail()  : mixed
Returns the config for this specific key.
getView()  : View
Get the view instance this helper is bound to.
implementedEvents()  : array<string, mixed>
Event listeners.
initialize()  : void
Constructor hook method.
ordinal()  : string
Formats a number into locale specific ordinal suffix.
precision()  : string
Formats a number with a level of precision.
setConfig()  : $this
Sets the config.
toPercentage()  : string
Formats a number into a percentage string.
toReadableSize()  : string
Returns a formatted-for-humans file size.
_configDelete()  : void
Deletes a single config key.
_configRead()  : mixed
Reads a config key.
_configWrite()  : void
Writes a config key.
_confirm()  : string
Returns a string to be used as onclick handler for confirm dialogs.

Properties

$_configInitialized

Whether the config property has already been configured with defaults

protected bool $_configInitialized = false

$_defaultConfig

Default config for this class

protected array<string, mixed> $_defaultConfig = ['engine' => \Cake\I18n\Number::class]

$_helperMap

A helper lookup table used to lazy load helper objects.

protected array<string, array<string|int, mixed>> $_helperMap = []

$_View

The View instance this helper is attached to

protected View $_View

$helpers

List of helpers used by this helper

protected array<string|int, mixed> $helpers = []

Methods

__call()

Call methods from Cake\I18n\Number utility class

public __call(string $method, array<string|int, mixed> $params) : mixed
Parameters
$method : string

Method to invoke

$params : array<string|int, mixed>

Array of params for the method.

Return values
mixed

Whatever is returned by called method, or false on failure

__construct()

Default Constructor

public __construct(View $view[, array<string, mixed> $config = [] ]) : mixed

Settings:

  • engine Class name to use to replace Cake\I18n\Number functionality The class needs to be placed in the Utility directory.
Parameters
$view : View

The View this helper is being attached to.

$config : array<string, mixed> = []

Configuration settings for the helper

Tags
throws
CakeException

When the engine class could not be found.

__debugInfo()

Returns an array that can be used to describe the internal state of this object.

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

__get()

Lazy loads helpers.

public __get(string $name) : Helper|null|void
Parameters
$name : string

Name of the property being accessed.

Return values
Helper|null|void

Helper instance if helper with provided name exists

addClass()

Adds the given class to the element options

public addClass(array<string, mixed> $options, string $class[, string $key = 'class' ]) : array<string, mixed>
Parameters
$options : array<string, mixed>

Array options/attributes to add a class to

$class : string

The class name being added.

$key : string = 'class'

the key to use for class. Defaults to 'class'.

Return values
array<string, mixed>

Array of options with $key set.

configShallow()

Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.

public configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this

Setting a specific value:

$this->configShallow('key', $value);

Setting a nested value:

$this->configShallow('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

Return values
$this

currency()

Formats a number into a currency format.

public currency(string|float $number[, string|null $currency = null ][, array<string, mixed> $options = [] ]) : string

Options

  • locale - The locale name to use for formatting the number, e.g. fr_FR
  • fractionSymbol - The currency symbol to use for fractional numbers.
  • fractionPosition - The position the fraction symbol should be placed valid options are 'before' & 'after'.
  • before - Text to display before the rendered number
  • after - Text to display after the rendered number
  • zero - The text to use for zero values, can be a string or a number. e.g. 0, 'Free!'
  • places - Number of decimal places to use. e.g. 2
  • precision - Maximum Number of decimal places to use, e.g. 2
  • pattern - An ICU number pattern to use for formatting the number. e.g #,##0.00
  • useIntlCode - Whether to replace the currency symbol with the international currency code.
  • escape - Whether to escape html in resulting string
Parameters
$number : string|float

Value to format.

$currency : string|null = null

International currency name such as 'USD', 'EUR', 'JPY', 'CAD'

$options : array<string, mixed> = []

Options list.

Return values
string

Number formatted as a currency.

defaultCurrency()

Getter/setter for default currency

public defaultCurrency(string|false|null $currency) : string|null

Use setDefaultCurrency()/getDefaultCurrency() instead.

Parameters
$currency : string|false|null

Default currency string to be used by currency() if $currency argument is not provided. If boolean false is passed, it will clear the currently stored value. Null reads the current default.

Return values
string|null

Currency

format()

Formats a number into the correct locale format

public format(string|int|float $number[, array<string, mixed> $options = [] ]) : string

Options:

  • places - Minimum number or decimals to use, e.g 0
  • precision - Maximum Number of decimal places to use, e.g. 2
  • locale - The locale name to use for formatting the number, e.g. fr_FR
  • before - The string to place before whole numbers, e.g. '['
  • after - The string to place after decimal numbers, e.g. ']'
  • escape - Whether to escape html in resulting string
Parameters
$number : string|int|float

A floating point number.

$options : array<string, mixed> = []

An array with options.

Tags
link
https://book.cakephp.org/4/en/views/helpers/number.html#formatting-numbers
Return values
string

Formatted number

formatDelta()

Formats a number into the correct locale format to show deltas (signed differences in value).

public formatDelta(string|float $value[, array<string, mixed> $options = [] ]) : string

Options

  • places - Minimum number or decimals to use, e.g 0
  • precision - Maximum Number of decimal places to use, e.g. 2
  • locale - The locale name to use for formatting the number, e.g. fr_FR
  • before - The string to place before whole numbers, e.g. '['
  • after - The string to place after decimal numbers, e.g. ']'
  • escape - Set to false to prevent escaping
Parameters
$value : string|float

A floating point number

$options : array<string, mixed> = []

Options list.

Return values
string

formatted delta

getConfig()

Returns the config.

public getConfig([string|null $key = null ][, mixed $default = null ]) : mixed

Usage

Reading the whole config:

$this->getConfig();

Reading a specific value:

$this->getConfig('key');

Reading a nested value:

$this->getConfig('some.nested.key');

Reading with default value:

$this->getConfig('some-key', 'default-value');
Parameters
$key : string|null = null

The key to get or null for the whole config.

$default : mixed = null

The return value when the key does not exist.

Return values
mixed

Configuration data at the named key or null if the key does not exist.

getConfigOrFail()

Returns the config for this specific key.

public getConfigOrFail(string $key) : mixed

The config value for this key must exist, it can never be null.

Parameters
$key : string

The key to get.

Tags
throws
InvalidArgumentException
Return values
mixed

Configuration data at the named key

getView()

Get the view instance this helper is bound to.

public getView() : View
Return values
View

The bound view instance.

implementedEvents()

Event listeners.

public implementedEvents() : array<string, mixed>
Return values
array<string, mixed>

initialize()

Constructor hook method.

public initialize(array<string, mixed> $config) : void

Implement this method to avoid having to overwrite the constructor and call parent.

Parameters
$config : array<string, mixed>

The configuration settings provided to this helper.

ordinal()

Formats a number into locale specific ordinal suffix.

public ordinal(float|int $value[, array<string, mixed> $options = [] ]) : string
Parameters
$value : float|int

An integer

$options : array<string, mixed> = []

An array with options.

Return values
string

formatted number

precision()

Formats a number with a level of precision.

public precision(string|float $number[, int $precision = 3 ][, array<string, mixed> $options = [] ]) : string
Parameters
$number : string|float

A floating point number.

$precision : int = 3

The precision of the returned number.

$options : array<string, mixed> = []

Additional options.

Tags
see
Number::precision()
link
https://book.cakephp.org/4/en/views/helpers/number.html#formatting-floating-point-numbers
Return values
string

Formatted float.

setConfig()

Sets the config.

public setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this

Usage

Setting a specific value:

$this->setConfig('key', $value);

Setting a nested value:

$this->setConfig('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

$merge : bool = true

Whether to recursively merge or overwrite existing config, defaults to true.

Tags
throws
CakeException

When trying to set a key that is invalid.

Return values
$this

toPercentage()

Formats a number into a percentage string.

public toPercentage(string|float $number[, int $precision = 2 ][, array<string, mixed> $options = [] ]) : string

Options:

  • multiply: Multiply the input value by 100 for decimal percentages.
Parameters
$number : string|float

A floating point number

$precision : int = 2

The precision of the returned number

$options : array<string, mixed> = []

Options

Tags
see
Number::toPercentage()
link
https://book.cakephp.org/4/en/views/helpers/number.html#formatting-percentages
Return values
string

Percentage string

_configDelete()

Deletes a single config key.

protected _configDelete(string $key) : void
Parameters
$key : string

Key to delete.

Tags
throws
CakeException

if attempting to clobber existing config

_configRead()

Reads a config key.

protected _configRead(string|null $key) : mixed
Parameters
$key : string|null

Key to read.

_configWrite()

Writes a config key.

protected _configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
$key : array<string, mixed>|string

Key to write to.

$value : mixed

Value to write.

$merge : string|bool = false

True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.

Tags
throws
CakeException

if attempting to clobber existing config

_confirm()

Returns a string to be used as onclick handler for confirm dialogs.

protected _confirm(string $okCode, string $cancelCode) : string
Parameters
$okCode : string

Code to be executed after user chose 'OK'

$cancelCode : string

Code to be executed after user chose 'Cancel'

Return values
string

"onclick" JS code


        
On this page

Search results