Pluralize::singular()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 18
rs 9.2222
1
<?php
2
3
namespace Erykai\Pluralize;
4
5
/**
6
 * Converts plural words to singular and singular to plural
7
 */
8
class Pluralize extends Resource
9
{
10
    /**
11
     * @param string $word
12
     * @return string
13
     */
14
    public function singular(string $word): string
15
    {
16
        if ($this->uncountable($word)) {
17
            return $word;
18
        }
19
        foreach ($this->getIrregular() as $result => $pattern) {
20
            $pattern = '/' . $pattern . '$/i';
21
22
            if (preg_match($pattern, $word)) {
23
                return preg_replace($pattern, $result, $word);
24
            }
25
        }
26
        foreach ($this->getSingular() as $pattern => $result) {
27
            if (preg_match($pattern, $word)) {
28
                return preg_replace($pattern, $result, $word);
29
            }
30
        }
31
        return $word;
32
    }
33
34
    /**
35
     * @param string $word
36
     * @return string
37
     */
38
    public function plural(string $word): string
39
    {
40
        if ($this->uncountable($word)) {
41
            return $word;
42
        }
43
        foreach ($this->getIrregular() as $pattern => $result) {
44
            $pattern = '/' . $pattern . '$/i';
45
46
            if (preg_match($pattern, $word)) {
47
                return preg_replace($pattern, $result, $word);
48
            }
49
        }
50
        foreach ($this->getPlural() as $pattern => $result) {
51
            if (preg_match($pattern, $word)) {
52
                return preg_replace($pattern, $result, $word);
53
            }
54
        }
55
56
        return $word;
57
    }
58
}