Passed
Push — new-api ( 18d26d...074931 )
by Sebastian
04:59
created

LocaleParser   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 93
ccs 53
cts 53
cp 1
rs 10
wmc 18

1 Method

Rating   Name   Duplication   Size   Complexity  
D parse() 0 81 18
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php: LocaleParser.php
5
 * User: Sebastian Böttger <[email protected]>
6
 * created at 29.12.20, 20:19
7
 */
8
9
namespace Seboettg\CiteProc\Locale;
10
11
use Seboettg\Collection\ArrayList as ArrayList;
12
use Seboettg\Collection\ArrayList\ArrayListInterface;
13
use SimpleXMLElement;
14
use stdClass;
15
16
class LocaleParser
17
{
18
    /**
19
     * @param SimpleXMLElement $locale
20
     * @param ArrayListInterface|null $options
21
     * @param ArrayListInterface|null $optionsXml
22
     * @param ArrayListInterface|null $date
23
     * @param ArrayListInterface|null $dateXml
24
     * @param ArrayListInterface|null $terms
25
     * @param ArrayListInterface|null $termsXml
26
     * @return ArrayListInterface[]
27
     */
28 184
    public function parse(
29
        SimpleXMLElement $locale,
30
        ?ArrayListInterface $options = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$options" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$options"; expected 0 but found 1
Loading history...
31
        ?ArrayListInterface $optionsXml = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$optionsXml" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$optionsXml"; expected 0 but found 1
Loading history...
32
        ?ArrayListInterface $date = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$date" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$date"; expected 0 but found 1
Loading history...
33
        ?ArrayListInterface $dateXml = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$dateXml" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$dateXml"; expected 0 but found 1
Loading history...
34
        ?ArrayListInterface $terms = null,
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$terms" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$terms"; expected 0 but found 1
Loading history...
35
        ?ArrayListInterface $termsXml = null
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$termsXml" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$termsXml"; expected 0 but found 1
Loading history...
36
    ): array {
37 184
        $options = $options ?? new ArrayList();
38 184
        $optionsXml = $optionsXml ?? new ArrayList();
39 184
        $date = $date ?? new ArrayList();
40 184
        $dateXml = $dateXml ?? new ArrayList();
41 184
        $terms = $terms ?? new ArrayList();
42 184
        $termsXml = $termsXml ?? new ArrayList();
43 184
        foreach ($locale as $node) {
44 184
            switch ($node->getName()) {
45 184
                case 'style-options':
46 184
                    $optionsXml->add('options', $node);
47 184
                    foreach ($node->attributes() as $name => $value) {
48 184
                        if ((string) $value == 'true') {
49 172
                            $options->add($name, [true]);
50
                        } else {
51 184
                            $options->add($name, [false]);
52
                        }
53
                    }
54 184
                    break;
55 184
                case 'terms':
56 184
                    $termsXml->add('terms', $node);
57 184
                    $plural = ['single', 'multiple'];
58
59 184
                    foreach ($node->children() as $child) {
60 184
                        $term = new Term();
61
62 184
                        foreach ($child->attributes() as $key => $value) {
63 184
                            $term->{$key} = (string) $value;
64
                        }
65
66 184
                        $subChildren = $child->children();
67 184
                        $count = $subChildren->count();
68 184
                        if ($count > 0) {
69 184
                            foreach ($subChildren as $subChild) {
70 184
                                $name = $subChild->getName();
71 184
                                $value = (string) $subChild;
72 184
                                if (in_array($subChild->getName(), $plural)) {
73 184
                                    $term->{$name} = $value;
74
                                }
75
                            }
76
                        } else {
77 184
                            $value = (string) $child;
78 184
                            $term->{'single'} = $value;
79 184
                            $term->{'multiple'} = $value;
80
                        }
81 184
                        if (!$terms->hasKey($term->getName())) {
82 184
                            $terms->add($term->getName(), []);
83
                        }
84
85 184
                        $terms->add($term->getName(), $term);
86
                    }
87 184
                    break;
88 184
                case 'date':
89 184
                    $form = (string) $node["form"];
90 184
                    $dateXml->add($form, $node);
91 184
                    foreach ($node->children() as $child) {
92 184
                        $d = new stdClass();
93 184
                        $name = "";
94 184
                        foreach ($child->attributes() as $key => $value) {
95 184
                            if ("name" === $key) {
96 184
                                $name = (string) $value;
97
                            }
98 184
                            $d->{$key} = (string) $value;
99
                        }
100 184
                        if ($child->getName() !== "name-part" && !$terms->hasKey($name)) {
101 184
                            $terms->add($name, []);
102
                        }
103 184
                        $date->add($form, $d);
104
                    }
105 184
                    break;
106
            }
107
        }
108 184
        return [$options, $optionsXml, $date, $dateXml, $terms, $termsXml];
109
    }
110
}
111