DictionaryTrait::toArray()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Gettext\Utils;
4
5
use Gettext\Translations;
6
7
/**
8
 * Trait used by all generators that exports the translations to plain dictionary (original => singular-translation).
9
 */
10
trait DictionaryTrait
11
{
12
    use HeadersGeneratorTrait;
13
    use HeadersExtractorTrait;
14
15
    /**
16
     * Returns a plain dictionary with the format [original => translation].
17
     *
18
     * @param Translations $translations
19
     * @param bool         $includeHeaders
20
     *
21
     * @return array
22
     */
23
    protected static function toArray(Translations $translations, $includeHeaders)
24
    {
25
        $messages = [];
26
27
        if ($includeHeaders) {
28
            $messages[''] = static::generateHeaders($translations);
29
        }
30
31
        foreach ($translations as $translation) {
32
            if ($translation->isDisabled()) {
33
                continue;
34
            }
35
36
            $messages[$translation->getOriginal()] = $translation->getTranslation();
37
        }
38
39
        return $messages;
40
    }
41
42
    /**
43
     * Extract the entries from a dictionary.
44
     *
45
     * @param array        $messages
46
     * @param Translations $translations
47
     */
48
    protected static function fromArray(array $messages, Translations $translations)
49
    {
50
        foreach ($messages as $original => $translation) {
51
            if ($original === '') {
52
                static::extractHeaders($translation, $translations);
53
                continue;
54
            }
55
56
            $translations->insert(null, $original)->setTranslation($translation);
57
        }
58
    }
59
}
60