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

LocaleXmlParserTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 67
c 0
b 0
f 0
dl 0
loc 152
ccs 0
cts 91
cp 0
rs 10
wmc 23

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initLocaleXmlParser() 0 8 1
A getDateXml() 0 3 1
A getLatestDateXml() 0 4 1
A getLatestOptionsXml() 0 4 1
A getTermsXml() 0 4 1
D parseXml() 0 66 18
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Locale;
11
12
use Seboettg\Collection\ArrayList;
13
use SimpleXMLElement;
14
use stdClass;
15
16
/**
17
 * Trait LocaleXmlParserTrait
18
 * @package Seboettg\CiteProc\Locale
19
 * @author Sebastian Böttger <[email protected]>
20
 */
21
trait LocaleXmlParserTrait
22
{
23
24
    /**
25
     * @var ArrayList
26
     */
27
    private $options;
28
29
    /**
30
     * @var ArrayList
31
     */
32
    private $date;
33
34
    /**
35
     * @var ArrayList
36
     */
37
    private $terms;
38
39
    /**
40
     * @var ArrayList
41
     */
42
    private $optionsXml;
43
44
    /**
45
     * @var ArrayList
46
     */
47
    private $dateXml;
48
49
    /**
50
     * @var ArrayList
51
     */
52
    private $termsXml;
53
54
    /**
55
     * init parser
56
     */
57
    protected function initLocaleXmlParser()
58
    {
59
        $this->options = new ArrayList();
60
        $this->optionsXml = new ArrayList();
61
        $this->date = new ArrayList();
62
        $this->dateXml = new ArrayList();
63
        $this->terms = new ArrayList();
64
        $this->termsXml = new ArrayList();
65
    }
66
67
    /**
68
     * @param SimpleXMLElement $locale
69
     */
70
    private function parseXml(SimpleXMLElement $locale)
71
    {
72
        foreach ($locale as $node) {
73
            switch ($node->getName()) {
74
                case 'style-options':
75
                    $this->optionsXml->add('options', $node);
76
                    foreach ($node->attributes() as $name => $value) {
77
                        if ((string) $value == 'true') {
78
                            $this->options->add($name, [true]);
79
                        } else {
80
                            $this->options->add($name, [false]);
81
                        }
82
                    }
83
                    break;
84
                case 'terms':
85
                    $this->termsXml->add('terms', $node);
86
                    $plural = ['single', 'multiple'];
87
88
                    foreach ($node->children() as $child) {
89
                        $term = new Term();
90
91
                        foreach ($child->attributes() as $key => $value) {
92
                            $term->{$key} = (string) $value;
93
                        }
94
95
                        $subChildren = $child->children();
96
                        $count = $subChildren->count();
97
                        if ($count > 0) {
98
                            foreach ($subChildren as $subChild) {
99
                                $name = $subChild->getName();
100
                                $value = (string) $subChild;
101
                                if (in_array($subChild->getName(), $plural)) {
102
                                    $term->{$name} = $value;
103
                                }
104
                            }
105
                        } else {
106
                            $value = (string) $child;
107
                            $term->{'single'} = $value;
108
                            $term->{'multiple'} = $value;
109
                        }
110
                        if (!$this->terms->hasKey($term->getName())) {
111
                            $this->terms->add($term->getName(), []);
112
                        }
113
114
                        $this->terms->add($term->getName(), $term);
115
                    }
116
                    break;
117
                case 'date':
118
                    $form = (string) $node["form"];
119
                    $this->dateXml->add($form, $node);
120
                    foreach ($node->children() as $child) {
121
                        $date = new stdClass();
122
                        $name = "";
123
                        foreach ($child->attributes() as $key => $value) {
124
                            if ("name" === $key) {
125
                                $name = (string) $value;
126
                            }
127
                            $date->{$key} = (string) $value;
128
                        }
129
                        if ($child->getName() !== "name-part" && !$this->terms->hasKey($name)) {
130
                            $this->terms->add($name, []);
131
                        }
132
                        $this->date->add($form, $date);
133
                    }
134
135
                    break;
136
            }
137
        }
138
    }
139
140
    /**
141
     * @return SimpleXMLElement
142
     */
143
    public function getLatestOptionsXml()
144
    {
145
        $arr = $this->optionsXml->toArray();
146
        return array_pop($arr);
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function getDateXml()
153
    {
154
        return $this->dateXml->toArray();
155
    }
156
157
    /**
158
     * @return SimpleXMLElement
159
     */
160
    public function getLatestDateXml()
161
    {
162
        $arr = $this->dateXml->toArray();
163
        return array_pop($arr['date']);
164
    }
165
166
    /**
167
     * @return SimpleXMLElement
168
     */
169
    public function getTermsXml()
170
    {
171
        $arr = $this->termsXml->toArray();
172
        return array_pop($arr);
173
    }
174
}
175