Passed
Push — master ( 234df8...613434 )
by Radovan
01:55
created

PluralProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 89
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A zeroPlural() 0 5 2
A enPlural() 0 7 3
A csPlural() 0 9 5
A getPlural() 0 9 3
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
     * Default plural provider
28
     */
29
    public const DEFAULT = 'enPlural';
30
31
    /**
32
     * Plural provider
33
     * @var string[]
34
     */
35
    private $plurals = [
36
        'cs' => 'csPlural',
37
        'en' => 'enPlural',
38
        'id' => 'zeroPlural',
39
        'ja' => 'zeroPlural',
40
        'ka' => 'zeroPlural',
41
        'ko' => 'zeroPlural',
42
        'lo' => 'zeroPlural',
43
        'ms' => 'zeroPlural',
44
        'my' => 'zeroPlural',
45
        'th' => 'zeroPlural',
46
        'vi' => 'zeroPlural',
47
        'zh' => 'zeroPlural',
48
    ];
49
50
    /**
51
     * Czech plural selector (zero-one-few-other)
52
     *
53
     * @param int|null $n
54
     * @return string
55
     */
56
    public static function csPlural(?int $n): string
57
    {
58
        return $n === 0
59
            ? IPlural::ZERO
60
            : ($n === 1
61
                ? IPlural::ONE
62
                : ($n >= 2 && $n < 5
63
                    ? IPlural::FEW
64
                    : IPlural::OTHER
65
                )
66
            );
67
    }
68
69
    /**
70
     * Default plural detector (zero-one-other)
71
     *
72
     * @param int|null $n
73
     * @return string
74
     */
75
    public static function enPlural(?int $n): string
76
    {
77
        return $n === 0
78
            ? IPlural::ZERO
79
            : ($n === 1
80
                ? IPlural::ONE
81
                : IPlural::OTHER
82
            );
83
    }
84
85
    /**
86
     * No plural detector (zero-other)
87
     *
88
     * @param int|null $n
89
     * @return string
90
     */
91
    public static function zeroPlural(?int $n): string
92
    {
93
        return $n === 0
94
            ? IPlural::ZERO
95
            : IPlural::OTHER;
96
    }
97
98
    /**
99
     * Get plural method
100
     *
101
     * @param string $locale
102
     * @return callable(int|null $n): string
103
     */
104
    public function getPlural(string $locale): callable
105
    {
106
        $locale = strtolower($locale);
107
        $callable = [$this, $this->plurals[$locale] ?? null];
108
109
        if ($callable[1] && is_callable($callable)) {
110
            return $callable;
111
        }
112
        return [$this, self::DEFAULT];
113
    }
114
}
115