CasesHelper   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0
wmc 22
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllCases() 0 12 1
A composeCasesFromWords() 0 11 3
D canonizeCase() 0 37 18
1
<?php
2
namespace morphos;
3
4
use InvalidArgumentException;
5
6
trait CasesHelper
7
{
8
    /**
9
     * @param $case
10
     * @return string
11
     * @throws InvalidArgumentException If passed case is invalid.
12
     */
13 6
    public static function canonizeCase($case)
14
    {
15 6
        $case = S::lower($case);
16
        switch ($case) {
17 6
            case Cases::NOMINATIVE:
18 6
            case 'nominativus':
19 5
            case 'n':
20 1
                return Cases::NOMINATIVE;
21
22 5
            case Cases::GENITIVE:
23 5
            case Cases::GENETIVE:
24 5
            case 'genetivus':
25 4
            case 'g':
26 1
                return Cases::GENITIVE;
27
28 4
            case Cases::DATIVE:
29 4
            case 'dativus':
30 3
            case 'd':
31 1
                return Cases::DATIVE;
32
33 3
            case Cases::ACCUSATIVE:
34
                return Cases::ACCUSATIVE;
35
36 3
            case Cases::ABLATIVE:
37 3
            case 'ablativus':
38 2
            case 'a':
39 1
                return Cases::ABLATIVE;
40
41 2
            case Cases::PREPOSITIONAL:
42 2
            case 'praepositionalis':
43 1
            case 'p':
44 1
                return Cases::PREPOSITIONAL;
45
46
            default:
47 1
                throw new InvalidArgumentException('Invalid case: '.$case);
48
        }
49
    }
50
51
    /**
52
     * @return array
53
     */
54 25
    public static function getAllCases()
55
    {
56
        return [
57 25
            Cases::NOMINATIVE,
58
            Cases::GENITIVE,
59
            Cases::GENETIVE,
60
            Cases::DATIVE,
61
            Cases::ACCUSATIVE,
62
            Cases::ABLATIVE,
63
            Cases::PREPOSITIONAL,
64
        ];
65
    }
66
67
    /**
68
     * Составляет один массив с падежами из нескольких массивов падежей разных слов
69
     * @param array $words Двумерный массив слов и их падежей
70
     * @param string $delimiter Разделитель между падежами слов
71
     * @return array Одномерный массив падежей
72
     */
73 25
    public static function composeCasesFromWords(array $words, $delimiter = ' ') {
74 25
        $cases = [];
75 25
        foreach (CasesHelper::getAllCases() as $case) {
76 25
            $composed_case = [];
77 25
            foreach ($words as $wordCases) {
78 25
                $composed_case[] = $wordCases[$case];
79
            }
80 25
            $cases[$case] = implode($delimiter, $composed_case);
81
        }
82 25
        return $cases;
83
    }
84
}
85