Decimal
in package
Decimal number from user input to a numeric-string.
The single place that defines what a "number from a user" looks like: decimal comma or dot, thousand separators including non-breaking spaces. Form traits and the DecimalQuantityTrait entity trait must not keep private copies; they would diverge and the same input would end up differently in each form.
IMPORTANT: rounding to a number of decimal places does not belong here. Scale is a property of
the column (decimal(10,3) for a price, decimal(8,2) for a quantity, another for shipping)
and unifying it globally would change validation of discounts and shipping. The helper returns
the value as entered, only in dot form.
Table of Contents
Constants
- THOUSAND_SEPARATORS : array<string|int, mixed> = [' ', " ", " "]
- Thousand separators that really come from the browser and copy&paste (a plain space, non-breaking U+00A0 and narrow non-breaking U+202F).
Methods
- clean() : string
- Cleans a string to dot form without checking that it is a number.
- fromInput() : string|null
- Form value to a numeric-string, or `null` when it is not a number.
Constants
THOUSAND_SEPARATORS
Thousand separators that really come from the browser and copy&paste (a plain space, non-breaking U+00A0 and narrow non-breaking U+202F).
private
array<string|int, mixed>
THOUSAND_SEPARATORS
= [' ', " ", " "]
Methods
clean()
Cleans a string to dot form without checking that it is a number.
public
static clean(string $value) : string
Also used by the DecimalQuantityTrait entity trait,
which must not return null (its own rule is "invalid = zero") and needs only the cleanup.
Parameters
- $value : string
Return values
stringfromInput()
Form value to a numeric-string, or `null` when it is not a number.
public
static fromInput(mixed $value) : string|null
IMPORTANT: null means "no value", not "zero"; the caller decides (?? '1' for a quantity,
?? '0' for a price). If the helper returned zero, the difference between an empty and a
zero field would disappear.
An empty string or only spaces gives null, and so does a non-numeric remainder
(1 000 CZK); that is why forms put a mask (PATTERN) on these fields, so the user sees an
error instead of a silent fall back to the default.
Parameters
- $value : mixed