Passed
Pull Request — master (#258)
by Alex
03:15
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 Doctrine\Inflector\Inflector;
0 ignored issues
show
Bug introduced by
The type Doctrine\Inflector\Inflector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Doctrine\Inflector\InflectorFactory;
0 ignored issues
show
Bug introduced by
The type Doctrine\Inflector\InflectorFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class Pluralizer
15
{
16
    /**
17
     * Uncountable word forms.
18
     *
19
     * @var array
20
     */
21
    public static $uncountable = [
22
        'audio',
23
        'bison',
24
        'cattle',
25
        'chassis',
26
        'compensation',
27
        'coreopsis',
28
        'data',
29
        'deer',
30
        'education',
31
        'emoji',
32
        'equipment',
33
        'evidence',
34
        'feedback',
35
        'firmware',
36
        'fish',
37
        'furniture',
38
        'gold',
39
        'hardware',
40
        'information',
41
        'jedi',
42
        'kin',
43
        'knowledge',
44
        'love',
45
        'metadata',
46
        'money',
47
        'moose',
48
        'news',
49
        'nutrition',
50
        'offspring',
51
        'plankton',
52
        'pokemon',
53
        'police',
54
        'rain',
55
        'recommended',
56
        'related',
57
        'rice',
58
        'series',
59
        'sheep',
60
        'software',
61
        'species',
62
        'swine',
63
        'traffic',
64
        'wheat',
65
    ];
66
67
    /**
68
     * Get the plural form of an English word.
69
     *
70
     * @param  string  $value
71
     * @param  int     $count
72
     * @return string
73
     */
74
    public static function plural($value, $count = 2)
75
    {
76
        if ((int) abs($count) === 1 || static::uncountable($value)) {
77
            return $value;
78
        }
79
80
        $inflec = InflectorFactory::create()->build();
81
82
        $plural = $inflec->pluralize($value);
83
84
        return static::matchCase($plural, $value);
85
    }
86
87
    /**
88
     * Get the singular form of an English word.
89
     *
90
     * @param  string  $value
91
     * @return string
92
     */
93
    public static function singular($value)
94
    {
95
        $singular = Inflector::singularize($value);
96
97
        return static::matchCase($singular, $value);
98
    }
99
100
    /**
101
     * Determine if the given value is uncountable.
102
     *
103
     * @param  string  $value
104
     * @return bool
105
     */
106
    protected static function uncountable($value)
107
    {
108
        return in_array(strtolower($value), static::$uncountable);
109
    }
110
111
    /**
112
     * Attempt to match the case on two strings.
113
     *
114
     * @param  string  $value
115
     * @param  string  $comparison
116
     * @return string
117
     */
118
    protected static function matchCase($value, $comparison)
119
    {
120
        $functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords'];
121
122
        foreach ($functions as $function) {
123
            if (call_user_func($function, $comparison) === $comparison) {
124
                return call_user_func($function, $value);
125
            }
126
        }
127
128
        return $value;
129
    }
130
131
}