BoogieSingularizer::in()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\RestResource;
4
5
use ICanBoogie\Inflector;
6
use function strtolower;
7
8
final class BoogieSingularizer implements Singularizer
9
{
10
    /** @var Inflector[] */
11
    private $inflectors;
12
13
    private const LANGUAGES = [
14
        'english' => 'en',
15
        'french' => 'fr',
16
        'norwegian bokmal' => 'nb',
17
        'bokmal' => 'nb',
18
        'portuguese' => 'pt',
19
        'spanish' => 'es',
20
        'turkish' => 'tr',
21
    ];
22
23
    public function __construct(Inflector ...$inflectors)
24
    {
25
        $this->inflectors = $inflectors;
26
    }
27
28
    public static function default(): Singularizer
29
    {
30
        return new self(Inflector::get('en'));
31
    }
32
33
    public static function in(string $locale): Singularizer
34
    {
35
        return new self(
36
            Inflector::get(
37
                self::LANGUAGES[strtolower($locale)] ?? strtolower($locale)
38
            ),
39
            Inflector::get('en')
40
        );
41
    }
42
43
    public function convert(string $word): string
44
    {
45
        foreach ($this->inflectors as $inflector) {
46
            $singular = $inflector->singularize($word);
47
            if ($singular !== $word) {
48
                return $singular;
49
            }
50
        }
51
        return $word;
52
    }
53
}
54