MediaWiki master
Plural.php
Go to the documentation of this file.
1<?php
8
10
27class Plural {
28
36 public function __construct(
37 private readonly Provider $provider,
38 ) {
39 }
40
54 public function process( float $count, array $forms ): string {
55 // For "explicit" forms such as "0=No items", "1=One item", or "other=Items"
56 // we’ll store them in an associative array if we parse them that way.
57 $explicitForms = [];
58
59 // For "default" (non-explicit) forms such as [ 'item', 'items' ],
60 // we store them in a sequential array with integer keys.
61 $defaultForms = [];
62
63 // Separate explicit forms ("n=text") from default forms
64 foreach ( $forms as $form ) {
65 if ( str_contains( $form, '=' ) ) {
66 [
67 $key,
68 $text,
69 ] = explode( '=', $form, 2 );
70 // If key is purely numeric AND matches $count, return immediately:
71 if ( is_numeric( $key ) && (float)$key === $count ) {
72 return $text;
73 }
74 // Otherwise, treat it as an explicit string key
75 $explicitForms[$key] = $text;
76 } else {
77 // Default form
78 $defaultForms[] = $form;
79 }
80 }
81
82 // Figure out the plural category: "one", "few", "other", etc.
83 $pluralType = $this->provider->getPluralProvider()->getPluralRuleType( $count );
84
85 // If we have an explicit form matching $pluralType` as a key, use it:
86 // e.g., "one" => "Item", "other" => "Items"
87 if ( array_key_exists( $pluralType, $explicitForms ) ) {
88 return $explicitForms[$pluralType];
89 }
90
91 // Otherwise, fallback to the default forms (sequential)
92 // If we find a default that exactly matches $pluralType as a string, use that:
93 $foundKey = array_search( $pluralType, $defaultForms, true );
94 if ( $foundKey !== false ) {
95 return $defaultForms[$foundKey];
96 }
97
98 // Else, use the numeric index from the language’s plural rules
99 // (e.g. 0 => singular form, 1 => plural form, etc.)
100 if ( count( $defaultForms ) > 0 ) {
101 $index = $this->provider->getPluralProvider()->getPluralRuleIndexNumber( $count );
102 // Guard in case $index is out of range
103 $index = min( $index, count( $defaultForms ) - 1 );
104
105 return $defaultForms[$index];
106 }
107
108 // If no forms were provided at all, just return an empty string
109 return '';
110 }
111}
__construct(private readonly Provider $provider,)
Initializes the Plural handler with the given language code and provider.
Definition Plural.php:36
process(float $count, array $forms)
Selects and returns the pluralized text form based on a numeric count.
Definition Plural.php:54