API / Minn\I18n / LegacyPluralForms

LegacyPluralForms final class

Minn\I18n\LegacyPluralForms · public/minn/src/Minn/I18n/LegacyPluralForms.php · 190 lines

The plural expression reader the old POMO library carries (Plural_Forms), kept apart from PluralExpression because plugins see its quirks: it reads only n, whole numbers, parentheses, % and the comparisons, && and ||, and the ternary, skipping spaces and tabs alone. It turns the expression into postfix tokens once (operators of equal strength group to the left, the ternary to the right) and runs them on a stack per number: an operand the stack lacks is null, == and != compare strictly, values stay as PHP left them until the answer is cast to an integer, and % is PHP's own, so a zero divisor throws. Every refusal is an Exception with the reference's words (probe translation-files).

Constants and properties

privateOP_CHARS = '|&><!=%?:'
privateLEVELS = array ( 0 => array ( 0 => '(', 1 => ')', ), 1 => array ( 0 => '?:', 1 => '?', ), 2 => array ( 0 => '||', ), 3 => array ( 0 => '&&', ), 4 => array ( 0 => '==', 1 => '!=', ), 5 => array ( 0 => '<', 1 => '<=', 2 => '>', 3 => '>=', ), 6 => array ( 0 => '%', ), )

The operators by how loosely they bind, loosest first; the parentheses below everything.

Public

staticparse(string $expression): array#

The expression as postfix tokens: ['var'], ['value', int] or ['op', string].

@return list<array{0: string, 1?: int|string}>
staticexecute(array $tokens, int $n): int#

The value of postfix tokens for one number.

$tokenslist<array{0: string, 1?: int|string}>

Internals 6

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privatetoken(string $expression, int $at, array $output, array $stack): int#

Reads one token at an offset onto the output or the operator stack; the offset after it.

$outputlist<array{0: string, 1?: int|string}>
$stacklist<string>
privateoperator(string $expression, int $at, array $output, array $stack): int#

Reads the one- or two-character operator at an offset; its length.

$outputlist<array{0: string, 1?: int|string}>
$stacklist<string>
privatecloseParenthesis(array $output, array $stack): void#
$outputlist<array{0: string, 1?: int|string}>
$stacklist<string>
privatecloseTernary(array $output, array $stack): void#

A ":" moves operators out until its "?", which becomes the whole ternary.

$outputlist<array{0: string, 1?: int|string}>
$stacklist<string>
privateapply(string $operator, array $stack): mixed#

Runs one operator on the values it pops, rightmost first.

$stacklist<mixed>
privatestrengths(): array#

Each operator's strength: its level's place in LEVELS.

@return array<string, int>