PluralProvider::zeroPlural()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * BCKP Translator
7
 * (c) Radovan Kepák
8
 *
9
 * For the full copyright and license information, please view
10
 * the file license.md that was distributed with this source code.
11
 *
12
 * @author Radovan Kepak <[email protected]>
13
 */
14
15
namespace Bckp\Translator;
16
17
use Closure;
18
19
use function strtolower;
20
21
final class PluralProvider
22
{
23
	/**
24
	 * Czech plural selector (zero-one-few-other)
25
	 * @api
26
	 */
27
	public static function csPlural(?int $n): Plural
28
	{
29
		return match (true) {
30
			$n === 0 => Plural::Zero,
31
			$n === 1 => Plural::One,
32
			$n >= 2 && $n <= 4 => Plural::Few,
33
			default => Plural::Other,
34
		};
35
	}
36
37
	/**
38
	 * Default plural detector (zero-one-other)
39
	 * @api
40
	 */
41
	public static function enPlural(?int $n): Plural
42
	{
43
		return match (true) {
44
			$n === 0 => Plural::Zero,
45
			$n === 1 => Plural::One,
46
			default => Plural::Other,
47
		};
48
	}
49
50
	/**
51
	 * No plural detector (zero-other)
52
	 * @api
53
	 */
54
	public static function zeroPlural(?int $n): Plural
55
	{
56
		return $n === 0
57
			? Plural::Zero
58
			: Plural::Other;
59
	}
60
61
	public function getPlural(string $locale): callable
62
	{
63
		return match (strtolower($locale)) {
64
			'cs' => [$this, 'csPlural'],
65
			'id', 'ja', 'ka', 'ko', 'lo', 'ms', 'my', 'th', 'vi', 'zh' => [$this, 'zeroPlural'],
66
			default => [$this, 'enPlural'],
67
		};
68
	}
69
}
70