Completed
Push — master ( 678b0e...adee67 )
by Antonio Carlos
01:52
created

Pluralizer::matchCase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Str\Support;
4
5
use Doctrine\Common\Inflector\Inflector;
6
7
class Pluralizer
8
{
9
    /**
10
     * Uncountable word forms.
11
     *
12
     * @var array
13
     */
14
    public static $uncountable = [
15
        'audio',
16
        'bison',
17
        'cattle',
18
        'chassis',
19
        'compensation',
20
        'coreopsis',
21
        'data',
22
        'deer',
23
        'education',
24
        'emoji',
25
        'equipment',
26
        'evidence',
27
        'feedback',
28
        'firmware',
29
        'fish',
30
        'furniture',
31
        'gold',
32
        'hardware',
33
        'information',
34
        'jedi',
35
        'kin',
36
        'knowledge',
37
        'love',
38
        'metadata',
39
        'money',
40
        'moose',
41
        'news',
42
        'nutrition',
43
        'offspring',
44
        'plankton',
45
        'pokemon',
46
        'police',
47
        'rain',
48
        'recommended',
49
        'related',
50
        'rice',
51
        'series',
52
        'sheep',
53
        'software',
54
        'species',
55
        'swine',
56
        'traffic',
57
        'wheat',
58
    ];
59
60
    /**
61
     * Get the plural form of an English word.
62
     *
63
     * @param  string  $value
64
     * @param  int     $count
65
     * @return string
66
     */
67 1
    public static function plural($value, $count = 2)
68
    {
69 1
        if ((int) abs($count) === 1 || static::uncountable($value)) {
70 1
            return $value;
71
        }
72
73 1
        $plural = Inflector::pluralize($value);
74
75 1
        return static::matchCase($plural, $value);
76
    }
77
78
    /**
79
     * Get the singular form of an English word.
80
     *
81
     * @param  string  $value
82
     * @return string
83
     */
84 1
    public static function singular($value)
85
    {
86 1
        $singular = Inflector::singularize($value);
87
88 1
        return static::matchCase($singular, $value);
89
    }
90
91
    /**
92
     * Determine if the given value is uncountable.
93
     *
94
     * @param  string  $value
95
     * @return bool
96
     */
97 1
    protected static function uncountable($value)
98
    {
99 1
        return in_array(strtolower($value), static::$uncountable);
100
    }
101
102
    /**
103
     * Attempt to match the case on two strings.
104
     *
105
     * @param  string  $value
106
     * @param  string  $comparison
107
     * @return string
108
     */
109 1
    protected static function matchCase($value, $comparison)
110
    {
111 1
        $functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords'];
112
113 1
        foreach ($functions as $function) {
114 1
            if ($function($comparison) === $comparison) {
115 1
                return $function($value);
116
            }
117
        }
118
119
        return $value;
120
    }
121
}
122