Open Journal Systems  3.3.0
Locale.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 
11 
12 use InvalidArgumentException;
16 use SimpleXMLElement;
17 use stdClass;
18 
33 class Locale
34 {
36 
40  private $localeXml;
41 
45  private $language;
46 
53  public function __construct($lang = "en-US", $xmlString = null)
54  {
55  $this->language = $lang;
56 
57  if (!empty($xmlString)) {
58  $this->localeXml = new SimpleXMLElement($xmlString);
59  } else {
60  $this->localeXml = new SimpleXMLElement(StyleSheet::loadLocales($lang));
61  }
62 
63  $this->initLocaleXmlParser();
64  $this->parseXml($this->localeXml);
65  }
66 
71  public function addXml(SimpleXMLElement $xml)
72  {
73  $lang = (string) $xml->attributes('http://www.w3.org/XML/1998/namespace')->{'lang'};
74  if (empty($lang) || $this->getLanguage() === $lang || explode('-', $this->getLanguage())[0] === $lang) {
75  $this->parseXml($xml);
76  }
77  return $this;
78  }
79 
83  public function getLanguage()
84  {
85  return $this->language;
86  }
87 
94  public function filter($type, $name, $form = "long")
95  {
96  if (!isset($this->{$type})) {
97  throw new InvalidArgumentException("There is no locale of type \"$type\".");
98  }
99 
101  $localeList = $this->{$type};
102 
103  if (is_null($name)) {
104  $name = "";
105  }
106 
107  //filter by name
108  $array = $localeList->get($name);
109 
110  if (empty($array)) {
111  $ret = new stdClass();
112  $ret->name = null;
113  $ret->single = null;
114  $ret->multiple = null;
115  return $ret;
116  }
117 
118  //filter by form
119  if ($type !== "options") {
121  $array = array_filter($array, function ($term) use ($form) {
122  return $term->form === $form;
123  });
124  }
125 
126  return array_pop($array);
127  }
128 }
Seboettg\CiteProc\Locale\Locale
Definition: Locale.php:33
Seboettg\CiteProc\Locale\LocaleXmlParserTrait
trait LocaleXmlParserTrait
Definition: LocaleXmlParserTrait.php:22
Seboettg\CiteProc\Locale\Locale\getLanguage
getLanguage()
Definition: Locale.php:89
Seboettg\CiteProc\StyleSheet\loadLocales
static loadLocales($langKey)
Definition: StyleSheet.php:48
Seboettg\CiteProc\Exception\CiteProcException
Definition: CiteProcException.php:20
Seboettg\CiteProc\Locale\Locale\__construct
__construct($lang="en-US", $xmlString=null)
Definition: Locale.php:59
Seboettg\CiteProc\StyleSheet
Definition: StyleSheet.php:22
Seboettg\CiteProc\Locale\Locale\filter
filter($type, $name, $form="long")
Definition: Locale.php:100
Seboettg\CiteProc\Locale
Definition: Locale.php:10
Seboettg\CiteProc\Locale\initLocaleXmlParser
initLocaleXmlParser()
Definition: LocaleXmlParserTrait.php:75
Seboettg\CiteProc\Locale\Locale\addXml
addXml(SimpleXMLElement $xml)
Definition: Locale.php:77
Seboettg\Collection\ArrayList
Definition: ArrayList.php:20