Completed
Pull Request — master (#2252)
by ྅༻ Ǭɀħ
02:27
created

Translations::translate_plural()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 3
nop 4
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the POMO package.
4
 *
5
 * @copyright 2014 POMO
6
 * @license GPL
7
 */
8
9
namespace POMO\Translations;
10
11
/**
12
 * Class for a set of entries for translation and their associated headers
13
 */
14
class Translations implements TranslationsInterface
15
{
16
    /**
17
     * Entries provided availible
18
     *
19
     * @var array
20
     */
21
    public $entries = array();
22
    public $headers = array();
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function add_entry($entry)
28
    {
29
        if (is_array($entry)) {
30
            $entry = new EntryTranslations($entry);
31
        }
32
        $key = $entry->key();
33
        if (false === $key) {
34
            return false;
35
        }
36
        $this->entries[$key] = &$entry;
37
38
        return true;
39
    }
40
41
    public function add_entry_or_merge($entry)
42
    {
43
        if (is_array($entry)) {
44
            $entry = new EntryTranslations($entry);
45
        }
46
        $key = $entry->key();
47
        if (false === $key) {
48
            return false;
49
        }
50
        if (isset($this->entries[$key])) {
51
            $this->entries[$key]->merge_with($entry);
52
        } else {
53
            $this->entries[$key] = &$entry;
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function set_header($header, $value)
63
    {
64
        $this->headers[$header] = $value;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function set_headers($headers)
71
    {
72
        foreach ($headers as $header => $value) {
73
            $this->set_header($header, $value);
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function get_header($header)
81
    {
82
        return isset($this->headers[$header]) ? $this->headers[$header] : false;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function translate_entry(&$entry)
89
    {
90
        $key = $entry->key();
91
92
        return isset($this->entries[$key]) ? $this->entries[$key] : false;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function translate($singular, $context = null)
99
    {
100
        $entry = new EntryTranslations(array(
101
                'singular' => $singular,
102
                'context' => $context
103
            ));
104
        $translated = $this->translate_entry($entry);
105
106
        return ($translated && !empty($translated->translations)) ?
107
            $translated->translations[0] :
108
            $singular;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function select_plural_form($count)
115
    {
116
        return 1 == $count ? 0 : 1;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function get_plural_forms_count()
123
    {
124
        return 2;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function translate_plural(
131
        $singular,
132
        $plural,
133
        $count,
134
        $context = null
135
    ) {
136
        $entry = new EntryTranslations(array(
137
                'singular' => $singular,
138
                'plural' => $plural,
139
                'context' => $context
140
            ));
141
        $translated = $this->translate_entry($entry);
142
        $index = $this->select_plural_form($count);
143
        $total_plural_forms = $this->get_plural_forms_count();
144
        if ($translated && 0 <= $index && $index < $total_plural_forms &&
145
                is_array($translated->translations) &&
146
                isset($translated->translations[$index])) {
147
            return $translated->translations[$index];
148
        } else {
149
            return 1 == $count ? $singular : $plural;
150
        }
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function merge_with(&$other)
157
    {
158
        foreach ($other->entries as $entry) {
159
            $this->entries[$entry->key()] = $entry;
160
        }
161
    }
162
163
    public function merge_originals_with(&$other)
164
    {
165
        foreach ($other->entries as $entry) {
166
            if ( !isset( $this->entries[$entry->key()] )) {
167
                $this->entries[$entry->key()] = $entry;
168
            } else {
169
                $this->entries[$entry->key()]->merge_with($entry);
170
            }
171
        }
172
    }
173
}
174