DictionaryTrait::fromArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 11
rs 9.4285
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
    private static function toArray(Translations $translations, $includeHeaders)
24
    {
25
        $messages = [];
26
27
        if ($includeHeaders) {
28
            $messages[''] = self::generateHeaders($translations);
29
        }
30
31
        foreach ($translations as $translation) {
32
            $messages[$translation->getOriginal()] = $translation->getTranslation();
33
        }
34
35
        return $messages;
36
    }
37
38
    /**
39
     * Extract the entries from a dictionary.
40
     *
41
     * @param array        $messages
42
     * @param Translations $translations
43
     */
44
    private static function fromArray(array $messages, Translations $translations)
45
    {
46
        foreach ($messages as $original => $translation) {
47
            if ($original === '') {
48
                self::extractHeaders($translation, $translations);
49
                continue;
50
            }
51
52
            $translations->insert(null, $original)->setTranslation($translation);
53
        }
54
    }
55
}
56