Passed
Pull Request — master (#4)
by Radovan
01:49
created

PluralProvider::enPlural()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * BCKP Translator
5
 * (c) Radovan Kepák
6
 *
7
 * For the full copyright and license information, please view
8
 * the file license.md that was distributed with this source code.
9
 *
10
 * @author Radovan Kepak <[email protected]>
11
 */
12
13
declare(strict_types=1);
14
15
namespace Bckp\Translator;
16
17
use function strtolower;
18
19
/**
20
 * Class PluralProvider
21
 *
22
 * @package Bckp\Translator
23
 */
24
final class PluralProvider implements IPlural
25
{
26
    /**
27
     * Plural provider
28
     * @var string[]
29
     */
30
    private $plurals = [
31
        'cs' => 'csPlural',
32
        'en' => 'enPlural',
33
        'id' => 'zeroPlural',
34
        'ja' => 'zeroPlural',
35
        'ka' => 'zeroPlural',
36
        'ko' => 'zeroPlural',
37
        'lo' => 'zeroPlural',
38
        'ms' => 'zeroPlural',
39
        'my' => 'zeroPlural',
40
        'th' => 'zeroPlural',
41
        'vi' => 'zeroPlural',
42
        'zh' => 'zeroPlural',
43
    ];
44
45
    /**
46
     * Czech plural selector (zero-one-few-other)
47
     *
48
     * @param int|null $n
49
     * @return string
50
     */
51
    public static function csPlural(?int $n): string
52
    {
53
        return $n === 0
54
            ? IPlural::ZERO
55
            : ($n === 1
56
                ? IPlural::ONE
57
                : ($n >= 2 && $n < 5
58
                    ? IPlural::FEW
59
                    : IPlural::OTHER
60
                )
61
            );
62
    }
63
64
    /**
65
     * Default plural detector (zero-one-other)
66
     *
67
     * @param int|null $n
68
     * @return string
69
     */
70
    public static function enPlural(?int $n): string
71
    {
72
        return $n === 0
73
            ? IPlural::ZERO
74
            : ($n === 1
75
                ? IPlural::ONE
76
                : IPlural::OTHER
77
            );
78
    }
79
80
    /**
81
     * No plural detector (zero-other)
82
     *
83
     * @param int|null $n
84
     * @return string
85
     */
86
    public static function zeroPlural(?int $n): string
87
    {
88
        return $n === 0
89
            ? IPlural::ZERO
90
            : IPlural::OTHER;
91
    }
92
93
    /**
94
     * Get plural method
95
     *
96
     * @param string $locale
97
     * @return callable(int|null $n): string
98
     */
99
    public function getPlural(string $locale): callable
100
    {
101
        $locale = strtolower($locale);
102
        $callable = [$this, $this->plurals[$locale] ?? null];
103
104
        if (is_callable($callable)) {
105
            return $callable;
106
        }
107
        return [$this, 'enPlural'];
108
    }
109
}
110