Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.38% |
62 / 65 |
|
72.73% |
8 / 11 |
CRAP | |
0.00% |
0 / 1 |
PageNumber | |
95.38% |
62 / 65 |
|
72.73% |
8 / 11 |
30 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getFormattedPageNumber | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
9 | |||
getRawPageNumber | |
95.24% |
20 / 21 |
|
0.00% |
0 / 1 |
9 | |||
isEmpty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDisplayMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isNumeric | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isRecto | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatRectoVerso | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
rawRectoVerso | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
getDisplayModes | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
formatICU | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Pagination; |
4 | |
5 | use Language; |
6 | use MediaWiki\Parser\Sanitizer; |
7 | use NumberFormatter; |
8 | use ProofreadPage\Pagination\CustomNumberFormatters\BengaliCurrencyFormat; |
9 | |
10 | /** |
11 | * @license GPL-2.0-or-later |
12 | */ |
13 | class PageNumber { |
14 | |
15 | public const DISPLAY_NORMAL = 'normal'; |
16 | public const DISPLAY_HIGHROMAN = 'highroman'; |
17 | public const DISPLAY_ROMAN = 'roman'; |
18 | public const DISPLAY_FOLIO = 'folio'; |
19 | public const DISPLAY_FOLIOHIGHROMAN = 'foliohighroman'; |
20 | public const DISPLAY_FOLIOROMAN = 'folioroman'; |
21 | public const DISPLAY_BENGALI_CURRENCY = 'prpbengalicurrency'; |
22 | public const DISPLAY_EMPTY = 'empty'; |
23 | |
24 | /** |
25 | * Mapping between the ProofreadPage number formats ids and the one from ICU. |
26 | * The full list of ICU number formats is available here: |
27 | * https://github.com/unicode-org/cldr/blob/master/common/supplemental/numberingSystems.xml |
28 | */ |
29 | private const DISPLAY_FROM_ICU = [ |
30 | 'beng' => 'beng', |
31 | 'deva' => 'deva', |
32 | 'highroman' => 'roman', |
33 | 'roman' => 'romanlow', |
34 | 'tamldec' => 'tamldec', |
35 | 'guru' => 'guru', |
36 | 'gujr' => 'gujr', |
37 | 'telu' => 'telu', |
38 | 'knda' => 'knda', |
39 | 'mlym' => 'mlym', |
40 | 'orya' => 'orya', |
41 | 'thai' => 'thai', |
42 | ]; |
43 | |
44 | /** @var string */ |
45 | private $number; |
46 | |
47 | /** @var string */ |
48 | private $displayMode; |
49 | |
50 | /** @var bool */ |
51 | private $isEmpty; |
52 | |
53 | /** @var bool */ |
54 | private $isRecto; |
55 | |
56 | /** |
57 | * @param string $number the page number |
58 | * @param string $displayMode the display mode (one of the DISPLAY_* constant) |
59 | * @param bool $isEmpty |
60 | * @param bool $isRecto true if recto, false if verso (for folio modes only) |
61 | */ |
62 | public function __construct( |
63 | string $number, |
64 | string $displayMode = self::DISPLAY_NORMAL, |
65 | bool $isEmpty = false, |
66 | bool $isRecto = true |
67 | ) { |
68 | $this->number = $number; |
69 | $this->displayMode = $displayMode; |
70 | $this->isEmpty = $isEmpty; |
71 | $this->isRecto = $isRecto; |
72 | } |
73 | |
74 | /** |
75 | * Returns the formatted page number as it should be displayed |
76 | * |
77 | * @param Language $language the language used for formatting |
78 | * @return string |
79 | */ |
80 | public function getFormattedPageNumber( Language $language ): string { |
81 | if ( !is_numeric( $this->number ) ) { |
82 | return $this->number; |
83 | } |
84 | |
85 | $number = (int)$this->number; |
86 | switch ( $this->displayMode ) { |
87 | case self::DISPLAY_NORMAL: |
88 | return Sanitizer::escapeHtmlAllowEntities( $language->formatNumNoSeparators( $number ) ); |
89 | case self::DISPLAY_FOLIO: |
90 | return Sanitizer::escapeHtmlAllowEntities( $language->formatNumNoSeparators( $number ) ) . |
91 | $this->formatRectoVerso(); |
92 | case self::DISPLAY_FOLIOHIGHROMAN: |
93 | return self::formatICU( $language, 'roman', $number ) . |
94 | $this->formatRectoVerso(); |
95 | case self::DISPLAY_FOLIOROMAN: |
96 | return self::formatICU( $language, 'romanlow', $number ) . |
97 | $this->formatRectoVerso(); |
98 | case self::DISPLAY_BENGALI_CURRENCY: |
99 | $formatter = new BengaliCurrencyFormat(); |
100 | return $formatter->formatNumber( $language, $number ); |
101 | default: |
102 | if ( array_key_exists( $this->displayMode, self::DISPLAY_FROM_ICU ) ) { |
103 | return self::formatICU( $language, self::DISPLAY_FROM_ICU[$this->displayMode], $number ); |
104 | } else { |
105 | return $this->number; |
106 | } |
107 | } |
108 | } |
109 | |
110 | /** |
111 | * Returns the raw page number, without any formatting |
112 | * |
113 | * @param Language $language |
114 | * @return string |
115 | */ |
116 | public function getRawPageNumber( Language $language ): string { |
117 | if ( !is_numeric( $this->number ) ) { |
118 | return $this->number; |
119 | } |
120 | |
121 | $number = (int)$this->number; |
122 | switch ( $this->displayMode ) { |
123 | case self::DISPLAY_NORMAL: |
124 | return Sanitizer::escapeHtmlAllowEntities( $language->formatNumNoSeparators( $number ) ); |
125 | case self::DISPLAY_FOLIO: |
126 | return Sanitizer::escapeHtmlAllowEntities( $language->formatNumNoSeparators( $number ) ) . |
127 | $this->rawRectoVerso(); |
128 | case self::DISPLAY_FOLIOHIGHROMAN: |
129 | return self::formatICU( $language, 'roman', $number ) . |
130 | $this->rawRectoVerso(); |
131 | case self::DISPLAY_FOLIOROMAN: |
132 | return self::formatICU( $language, 'romanlow', $number ) . |
133 | $this->rawRectoVerso(); |
134 | case self::DISPLAY_BENGALI_CURRENCY: |
135 | $formatter = new BengaliCurrencyFormat(); |
136 | return $formatter->formatNumber( $language, $number ); |
137 | default: |
138 | if ( array_key_exists( $this->displayMode, self::DISPLAY_FROM_ICU ) ) { |
139 | return self::formatICU( $language, self::DISPLAY_FROM_ICU[$this->displayMode], $number ); |
140 | } else { |
141 | return $this->number; |
142 | } |
143 | } |
144 | } |
145 | |
146 | /** |
147 | * @return bool |
148 | */ |
149 | public function isEmpty(): bool { |
150 | return $this->isEmpty; |
151 | } |
152 | |
153 | /** |
154 | * @return string |
155 | */ |
156 | public function getDisplayMode(): string { |
157 | return $this->displayMode; |
158 | } |
159 | |
160 | /** |
161 | * @return bool |
162 | */ |
163 | public function isNumeric(): bool { |
164 | return is_numeric( $this->number ); |
165 | } |
166 | |
167 | /** |
168 | * @return bool |
169 | */ |
170 | public function isRecto(): bool { |
171 | return $this->isRecto; |
172 | } |
173 | |
174 | /** |
175 | * @return string |
176 | */ |
177 | private function formatRectoVerso(): string { |
178 | return $this->isRecto ? '<sup>r</sup>' : '<sup>v</sup>'; |
179 | } |
180 | |
181 | /** |
182 | * @return string |
183 | */ |
184 | private function rawRectoVerso(): string { |
185 | return $this->isRecto ? 'r' : 'v'; |
186 | } |
187 | |
188 | /** |
189 | * @return string[] |
190 | */ |
191 | public static function getDisplayModes(): array { |
192 | $modes = array_keys( self::DISPLAY_FROM_ICU ); |
193 | $modes[] = self::DISPLAY_NORMAL; |
194 | $modes[] = self::DISPLAY_FOLIO; |
195 | $modes[] = self::DISPLAY_FOLIOHIGHROMAN; |
196 | $modes[] = self::DISPLAY_FOLIOROMAN; |
197 | $modes[] = self::DISPLAY_BENGALI_CURRENCY; |
198 | return $modes; |
199 | } |
200 | |
201 | /** |
202 | * Formats a number in $language using the name numbering system using the ICU data |
203 | * |
204 | * @param Language $language |
205 | * @param string|null $name |
206 | * @param int $number |
207 | * @return string|false |
208 | */ |
209 | private static function formatICU( Language $language, ?string $name, int $number ) { |
210 | $locale = $language->getCode(); |
211 | if ( $name !== null ) { |
212 | $locale .= '-u-nu-' . $name; |
213 | } |
214 | $formatter = new NumberFormatter( $locale, NumberFormatter::DEFAULT_STYLE ); |
215 | $formatter->setSymbol( NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '' ); |
216 | return $formatter->format( $number ); |
217 | } |
218 | } |