LocaleXmlParserTrait::getLatestDateXml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Locale;
12
13
use Seboettg\Collection\ArrayList;
14
use Seboettg\Collection\Map\MapInterface;
15
use SimpleXMLElement;
16
use stdClass;
17
use function Seboettg\Collection\Lists\emptyList;
18
use function Seboettg\Collection\Map\emptyMap;
19
20
/**
21
 * Trait LocaleXmlParserTrait
22
 * @package Seboettg\CiteProc\Locale
23
 * @author Sebastian Böttger <[email protected]>
24
 */
25
trait LocaleXmlParserTrait
26
{
27
    private MapInterface $options;
28
    private MapInterface $date;
29
    private MapInterface $terms;
30
    private MapInterface $optionsXml;
31
    private MapInterface $dateXml;
32
    private MapInterface $termsXml;
33
34
    /**
35
     * init parser
36
     */
37
    protected function initLocaleXmlParser()
38
    {
39
        $this->options = emptyMap();
40
        $this->optionsXml = emptyMap();
41
        $this->date = emptyMap();
42
        $this->dateXml = emptyMap();
43
        $this->terms = emptyMap();
44
        $this->termsXml = emptyMap();
45
    }
46
47
    /**
48
     * @param SimpleXMLElement $locale
49
     */
50
    private function parseXml(SimpleXMLElement $locale)
51
    {
52
        /** @var SimpleXMLElement $node */
53
        foreach ($locale as $node) {
54
            switch ($node->getName()) {
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            switch ($node->/** @scrutinizer ignore-call */ getName()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
                case 'style-options':
56
                    $this->optionsXml->put('options', $node);
57
                    foreach ($node->attributes() as $name => $value) {
58
                        if ((string) $value == 'true') {
59
                            $this->options->put($name, [true]);
60
                        } else {
61
                            $this->options->put($name, [false]);
62
                        }
63
                    }
64
                    break;
65
                case 'terms':
66
                    $this->termsXml->put('terms', emptyList());
67
                    $this->termsXml["terms"]->add($node);
68
                    $plural = ['single', 'multiple'];
69
70
                    /** @var SimpleXMLElement $child */
71
                    foreach ($node->children() as $child) {
72
                        $term = new Term();
73
74
                        foreach ($child->attributes() as $key => $value) {
75
                            $term->{$key} = (string) $value;
76
                        }
77
78
                        $subChildren = $child->children();
79
                        $count = $subChildren->count();
80
                        if ($count > 0) {
81
                            /** @var SimpleXMLElement $subChild */
82
                            foreach ($subChildren as $subChild) {
83
                                $name = $subChild->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
                                /** @scrutinizer ignore-call */ 
84
                                $name = $subChild->getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
                                $value = (string) $subChild;
85
                                if (in_array($subChild->getName(), $plural)) {
86
                                    $term->{$name} = $value;
87
                                }
88
                            }
89
                        } else {
90
                            $value = (string) $child;
91
                            $term->{'single'} = $value;
92
                            $term->{'multiple'} = $value;
93
                        }
94
                        if (!$this->terms->containsKey($term->getName())) {
95
                            $this->terms->put($term->getName(), emptyList());
96
                        }
97
98
                        $this->terms[$term->getName()]->add($term);
99
                    }
100
                    break;
101
                case 'date':
102
                    $form = (string) $node["form"];
103
                    $this->dateXml->put($form, $node);
104
                    foreach ($node->children() as $child) {
105
                        $date = new stdClass();
106
                        $name = "";
107
                        foreach ($child->attributes() as $key => $value) {
108
                            if ("name" === $key) {
109
                                $name = (string) $value;
110
                            }
111
                            $date->{$key} = (string) $value;
112
                        }
113
                        if ($child->getName() !== "name-part" && !$this->terms->containsKey($name)) {
114
                            $this->terms->put($name, []);
115
                        }
116
                        $this->date->put($form, $date);
117
                    }
118
119
                    break;
120
            }
121
        }
122
    }
123
124
    /**
125
     * @return SimpleXMLElement
126
     */
127
    public function getLatestOptionsXml()
128
    {
129
        $arr = $this->optionsXml->toArray();
130
        return array_pop($arr);
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function getDateXml()
137
    {
138
        return $this->dateXml->toArray();
139
    }
140
141
    /**
142
     * @return SimpleXMLElement
143
     */
144
    public function getLatestDateXml()
145
    {
146
        $arr = $this->dateXml->toArray();
147
        return array_pop($arr['date']);
148
    }
149
150
    /**
151
     * @return SimpleXMLElement
152
     */
153
    public function getTermsXml()
154
    {
155
        $arr = $this->termsXml->toArray();
156
        return array_pop($arr);
157
    }
158
}
159