Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

Translator::normalizeTranslationArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
class Translator
6
{
7
    /**
8
     * @param string $original
9
     * @param string $domain
10
     * @return string
11
     */
12 8
    public function translate($original, $domain, array $args)
13
    {
14 8
        $domains = glsr()->filterArray('translator/domains', [glsr()->id]);
15 8
        if (!in_array($domain, $domains)) {
16
            return $original;
17
        }
18 8
        $args = $this->normalizeTranslationArgs($args);
19 8
        $strings = $this->getTranslationStrings($args['single'], $args['plural']);
20 8
        if (empty($strings)) {
21 8
            return $original;
22
        }
23
        $string = current($strings);
24
        return 'plural' == $string['type']
25
            ? $this->translatePlural($domain, $string, $args)
26
            : $this->translateSingle($domain, $string, $args);
27
    }
28
29
    /**
30
     * Used when search/replacing a default text-domain translation.
31
     * @return string
32
     */
33
    public function getTranslation(array $args)
34
    {
35
        $args = $this->normalizeTranslationArgs($args);
36
        return get_translations_for_domain(glsr()->id)->translate_plural($args['single'], $args['plural'], $args['number']);
37
    }
38
39
    /**
40
     * @param string $single
41
     * @param string $plural
42
     * @return array
43
     */
44 8
    protected function getTranslationStrings($single, $plural)
45
    {
46
        return array_filter(glsr(Translation::class)->translations(), function ($string) use ($single, $plural) {
47
            return $string['s1'] == html_entity_decode($single, ENT_COMPAT, 'UTF-8')
48
                && $string['p1'] == html_entity_decode($plural, ENT_COMPAT, 'UTF-8');
49 8
        });
50
    }
51
52
    /**
53
     * @return array
54
     */
55 8
    protected function normalizeTranslationArgs(array $args)
56
    {
57
        $defaults = [
58 8
            'context' => '',
59
            'number' => 1,
60
            'plural' => '',
61
            'single' => '',
62
        ];
63 8
        return shortcode_atts($defaults, $args);
64
    }
65
66
    /**
67
     * @param string $domain
68
     * @return string
69
     */
70
    protected function translatePlural($domain, array $string, array $args)
71
    {
72
        if (!empty($string['s2'])) {
73
            $args['single'] = $string['s2'];
74
        }
75
        if (!empty($string['p2'])) {
76
            $args['plural'] = $string['p2'];
77
        }
78
        return get_translations_for_domain($domain)->translate_plural(
79
            $args['single'],
80
            $args['plural'],
81
            $args['number'],
82
            $args['context']
83
        );
84
    }
85
86
    /**
87
     * @param string $domain
88
     * @return string
89
     */
90
    protected function translateSingle($domain, array $string, array $args)
91
    {
92
        if (!empty($string['s2'])) {
93
            $args['single'] = $string['s2'];
94
        }
95
        return get_translations_for_domain($domain)->translate(
96
            $args['single'],
97
            $args['context']
98
        );
99
    }
100
}
101