DictionaryTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 6
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 14 3
A fromArray() 0 11 3
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