Completed
Push — v5-dev ( a42eb3...269a1b )
by Oscar
01:14
created

ArrayGenerator::includeHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Gettext\Generator;
4
5
use Gettext\Translations;
6
use Gettext\Translation;
7
use Gettext\Headers;
8
9
final class ArrayGenerator extends Generator
10
{
11
    private $includeHeaders = true;
12
13
    public function includeHeaders(bool $includeHeaders = true): void
14
    {
15
        $this->includeHeaders = $includeHeaders;
16
    }
17
18
    public function generateString(Translations $translations): string
19
    {
20
        $array = $this->generateArray($translations);
21
22
        return sprintf('<?php return %s;', var_export($array, true));
23
    }
24
25
    public function generateArray(Translations $translations): array
26
    {
27
        $pluralForm = $translations->getHeaders()->getPluralForm();
28
        $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null;
29
        $messages = [];
30
31 View Code Duplication
        if ($this->includeHeaders) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
            $headers = [];
33
34
            foreach ($translations->getHeaders() as $name => $value) {
35
                $headers[] = sprintf("%s: %s", $name, $value);
36
            }
37
38
            $messages[''] = ['' => implode("\n", $headers)];
39
        }
40
41
        foreach ($translations as $translation) {
42
            if ($translation->isDisabled()) {
43
                continue;
44
            }
45
46
            $context = $translation->getContext() ?: '';
47
            $original = $translation->getOriginal();
48
49
            if (!isset($messages[$context])) {
50
                $messages[$context] = [];
51
            }
52
53
            if (self::hasPluralTranslations($translation)) {
54
                $messages[$context][$original] = $translation->getPluralTranslations($pluralSize);
55
                array_unshift($messages[$context][$original], $translation->getTranslation());
56
            } else {
57
                $messages[$context][$original] = $translation->getTranslation();
58
            }
59
        }
60
61
        return [
62
            'domain' => $translations->getDomain(),
63
            'plural-forms' => $translations->getHeaders()->get(Headers::HEADER_PLURAL),
64
            'messages' => $messages,
65
        ];
66
    }
67
68
    private static function hasPluralTranslations(Translation $translation): bool
69
    {
70
        return implode('', $translation->getPluralTranslations()) !== '';
71
    }
72
}
73