Completed
Push — master ( 46f2b5...868fd1 )
by Oscar
02:25
created

PhpArray::toArray()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 14
nc 8
nop 1
1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
7
class PhpArray extends Generator implements GeneratorInterface
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    public static function toString(Translations $translations)
13
    {
14
        $array = self::toArray($translations);
15
16
        return '<?php return '.var_export($array, true).'; ?>';
17
    }
18
19
    /**
20
     * Generates an array with the translations.
21
     *
22
     * @param Translations $translations
23
     *
24
     * @return array
25
     */
26
    public static function toArray(Translations $translations)
27
    {
28
        $array = self::buildArray($translations);
29
30
        $domain = $translations->getDomain() ?: 'messages';
31
        $lang = $translations->getLanguage() ?: 'en';
32
33
        $fullArray = array(
34
            $domain => array(
35
                '' => array(
36
                    'domain' => $domain,
37
                    'lang' => $lang,
38
                    'plural-forms' => 'nplurals=2; plural=(n != 1);',
39
                ),
40
            ),
41
        );
42
43
        if ($translations->getHeader('Plural-Forms') !== null) {
44
            $fullArray[$domain]['']['plural-forms'] = $translations->getHeader('Plural-Forms');
45
        }
46
47
        $fullArray[$domain] = array_merge($fullArray[$domain], $array);
48
49
        return $fullArray;
50
    }
51
52
    /**
53
     * Generates an array with all translations
54
     * 
55
     * @param Translations $translations
56
     *
57
     * @return array
58
     */
59 View Code Duplication
    protected static function buildArray(Translations $translations)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61
        $array = array();
62
63
        $context_glue = "\004";
64
65
        foreach ($translations as $translation) {
66
            $key = ($translation->hasContext() ? $translation->getContext().$context_glue : '').$translation->getOriginal();
67
            $entry = array($translation->getPlural(), $translation->getTranslation());
68
69
            if ($translation->hasPluralTranslation()) {
70
                $entry = array_merge($entry, $translation->getPluralTranslation());
71
            }
72
73
            $array[$key] = $entry;
74
        }
75
76
        return $array;
77
    }
78
}
79