VatRate
in package
VAT rate (percent) as a numeric-string — the single place that says how a rate is parsed, stored, keyed and displayed.
Rates are not always whole numbers (5.5 %, 2.1 %, 13.5 % elsewhere in the EU), so:
- in PHP a rate is a string (never float — float keys of
$byRate[$rate]are truncated to int and float arithmetic does not belong next to bcmath), - in the DB it is
decimal(5,2)and arrives as'21.00'/'5.50', - as an array key / grouping identity use key() (
'21','5.5') —'21.00'and'21'must not form two groups,'5'and'5.5'must not merge into one, - for display use format() (
21,5,5) — no trailing zeros.
Table of Contents
Constants
- SCALE : int = 2
- Scale of the `decimal(5,2)` columns holding a rate.
Methods
- equals() : bool
- Rates equal by value (`'21'` = `'21.00'`), null-safe.
- format() : string
- Display form without trailing zeros and with the given decimal separator (`21` / `5,5` for cs/sk, `5.5` for en). The `%` sign is added by the template.
- formatFor() : string
- Display form for a locale / translator: `format()` with {@see separatorFor()}.
- formatInput() : string|null
- Returns the value for `<input type=number>` or machine use: `'21.00'` gives `'21'`, `'5.50'` gives `'5.5'` (dot).
- key() : string
- Identity of a rate for array keys and grouping: `'21.00'` → `'21'`, `'5.50'` → `'5.5'`, `'0.00'` → `'0'`. Invalid input → `'0'`.
- normalize() : numeric-string|null
- User/DB value → canonical `decimal(5,2)` string (`'5.50'`, `'21.00'`), or null when the value is not a number or lies outside 0–100. Accepts decimal comma and dot, int, float, `'21.00'`.
- normalizeOr() : string|null
- Same as {@see normalize()} but falls back to `$fallback` for empty/invalid input.
- separatorFor() : string
- Decimal separator of a UI/document locale (`cs`, `sk` → comma; `en`, `en_US` → dot; unknown → comma).
Constants
SCALE
Scale of the `decimal(5,2)` columns holding a rate.
public
int
SCALE
= 2
Methods
equals()
Rates equal by value (`'21'` = `'21.00'`), null-safe.
public
static equals(mixed $a, mixed $b) : bool
Parameters
- $a : mixed
- $b : mixed
Return values
boolformat()
Display form without trailing zeros and with the given decimal separator (`21` / `5,5` for cs/sk, `5.5` for en). The `%` sign is added by the template.
public
static format(mixed $value[, string $decimalSeparator = ',' ]) : string
Parameters
- $value : mixed
- $decimalSeparator : string = ','
Return values
stringformatFor()
Display form for a locale / translator: `format()` with {@see separatorFor()}.
public
static formatFor(mixed $value, string|object|null $locale) : string
Parameters
- $value : mixed
- $locale : string|object|null
Return values
stringformatInput()
Returns the value for `<input type=number>` or machine use: `'21.00'` gives `'21'`, `'5.50'` gives `'5.5'` (dot).
public
static formatInput(mixed $value) : string|null
Null or empty stays null, unlike format(), which would print an empty rate as "0".
Parameters
- $value : mixed
Return values
string|nullkey()
Identity of a rate for array keys and grouping: `'21.00'` → `'21'`, `'5.50'` → `'5.5'`, `'0.00'` → `'0'`. Invalid input → `'0'`.
public
static key(mixed $value) : string
NOTE: PHP turns a key like '21' into int 21; compare keys with == or cast back via (string).
Parameters
- $value : mixed
Return values
stringnormalize()
User/DB value → canonical `decimal(5,2)` string (`'5.50'`, `'21.00'`), or null when the value is not a number or lies outside 0–100. Accepts decimal comma and dot, int, float, `'21.00'`.
public
static normalize(mixed $value) : numeric-string|null
Parameters
- $value : mixed
Return values
numeric-string|nullnormalizeOr()
Same as {@see normalize()} but falls back to `$fallback` for empty/invalid input.
public
static normalizeOr(mixed $value, string|null $fallback) : string|null
Parameters
- $value : mixed
- $fallback : string|null
Return values
string|nullseparatorFor()
Decimal separator of a UI/document locale (`cs`, `sk` → comma; `en`, `en_US` → dot; unknown → comma).
public
static separatorFor(string|object|null $locale) : string
$locale is a locale string or a translator (Contributte Translator::getLocale(); the Nette
interface has no such method, hence the duck typing).
Parameters
- $locale : string|object|null