Open Journal Systems  3.3.0
LocaleXmlParserTrait.php
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 
13 use SimpleXMLElement;
14 use stdClass;
15 
22 {
23 
27  private $options;
28 
32  private $date;
33 
37  private $terms;
38 
42  private $optionsXml;
43 
47  private $dateXml;
48 
52  private $termsXml;
53 
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 
70  private function parseXml(SimpleXMLElement $locale)
71  {
73  foreach ($locale as $node) {
74  switch ($node->getName()) {
75  case 'style-options':
76  $this->optionsXml->add('options', $node);
77  foreach ($node->attributes() as $name => $value) {
78  if ((string) $value == 'true') {
79  $this->options->add($name, [true]);
80  } else {
81  $this->options->add($name, [false]);
82  }
83  }
84  break;
85  case 'terms':
86  $this->termsXml->add('terms', $node);
87  $plural = ['single', 'multiple'];
88 
90  foreach ($node->children() as $child) {
91  $term = new Term();
92 
93  foreach ($child->attributes() as $key => $value) {
94  $term->{$key} = (string) $value;
95  }
96 
97  $subChildren = $child->children();
98  $count = $subChildren->count();
99  if ($count > 0) {
101  foreach ($subChildren as $subChild) {
102  $name = $subChild->getName();
103  $value = (string) $subChild;
104  if (in_array($subChild->getName(), $plural)) {
105  $term->{$name} = $value;
106  }
107  }
108  } else {
109  $value = (string) $child;
110  $term->{'single'} = $value;
111  $term->{'multiple'} = $value;
112  }
113  if (!$this->terms->hasKey($term->getName())) {
114  $this->terms->add($term->getName(), []);
115  }
116 
117  $this->terms->add($term->getName(), $term);
118  }
119  break;
120  case 'date':
121  $form = (string) $node["form"];
122  $this->dateXml->add($form, $node);
123  foreach ($node->children() as $child) {
124  $date = new stdClass();
125  $name = "";
126  foreach ($child->attributes() as $key => $value) {
127  if ("name" === $key) {
128  $name = (string) $value;
129  }
130  $date->{$key} = (string) $value;
131  }
132  if ($child->getName() !== "name-part" && !$this->terms->hasKey($name)) {
133  $this->terms->add($name, []);
134  }
135  $this->date->add($form, $date);
136  }
137 
138  break;
139  }
140  }
141  }
142 
146  public function getLatestOptionsXml()
147  {
148  $arr = $this->optionsXml->toArray();
149  return array_pop($arr);
150  }
151 
155  public function getDateXml()
156  {
157  return $this->dateXml->toArray();
158  }
159 
163  public function getLatestDateXml()
164  {
165  $arr = $this->dateXml->toArray();
166  return array_pop($arr['date']);
167  }
168 
172  public function getTermsXml()
173  {
174  $arr = $this->termsXml->toArray();
175  return array_pop($arr);
176  }
177 }
Seboettg\CiteProc\Locale\getLatestOptionsXml
getLatestOptionsXml()
Definition: LocaleXmlParserTrait.php:164
Seboettg\CiteProc\Locale\LocaleXmlParserTrait
trait LocaleXmlParserTrait
Definition: LocaleXmlParserTrait.php:22
Seboettg\CiteProc\Locale\getDateXml
getDateXml()
Definition: LocaleXmlParserTrait.php:173
Seboettg\CiteProc\Locale\getLatestDateXml
getLatestDateXml()
Definition: LocaleXmlParserTrait.php:181
Seboettg\CiteProc\Locale
Definition: Locale.php:10
Seboettg\CiteProc\Locale\initLocaleXmlParser
initLocaleXmlParser()
Definition: LocaleXmlParserTrait.php:75
Seboettg\Collection\ArrayList
Definition: ArrayList.php:20
Seboettg\CiteProc\Locale\getTermsXml
getTermsXml()
Definition: LocaleXmlParserTrait.php:190