Completed
Push — master ( 58b23d...229e63 )
by Alex
25s queued 16s
created

Pluralizer::singular()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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