Passed
Push — master ( eeeeb5...9c97a1 )
by Paul
03:55
created

Translator::getTranslationStrings()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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