|
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 InvalidArgumentException; |
|
13
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
|
14
|
|
|
use Seboettg\CiteProc\StyleSheet; |
|
15
|
|
|
use Seboettg\Collection\ArrayList; |
|
16
|
|
|
use SimpleXMLElement; |
|
17
|
|
|
use stdClass; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Locale |
|
21
|
|
|
* |
|
22
|
|
|
* While localization data can be included in styles, locale files conveniently provide sets of default localization |
|
23
|
|
|
* data, consisting of terms, date formats and grammar options. These default localizations are drawn from the |
|
24
|
|
|
* “locales-xx-XX.xml” located in locales folder (which is included as git submodule). These default locales may be |
|
25
|
|
|
* redefined or supplemented with cs:locale elements, which should be placed in the style sheet directly after the |
|
26
|
|
|
* cs:info element. |
|
27
|
|
|
* |
|
28
|
|
|
* TODO: implement Locale Fallback (http://docs.citationstyles.org/en/stable/specification.html#locale-fallback) |
|
29
|
|
|
* |
|
30
|
|
|
* @package Seboettg\CiteProc\Locale |
|
31
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
class Locale |
|
34
|
|
|
{ |
|
35
|
|
|
use LocaleXmlParserTrait; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var SimpleXMLElement |
|
39
|
|
|
*/ |
|
40
|
|
|
private $localeXml; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $language; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Locale constructor. |
|
49
|
|
|
* @param string $lang |
|
50
|
|
|
* @param ?string $xmlString |
|
51
|
|
|
* @throws CiteProcException |
|
52
|
|
|
*/ |
|
53
|
176 |
|
public function __construct($lang = "en-US", $xmlString = null) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
176 |
|
$this->language = $lang; |
|
56
|
|
|
|
|
57
|
176 |
|
if (!empty($xmlString)) { |
|
58
|
|
|
$this->localeXml = new SimpleXMLElement($xmlString); |
|
59
|
|
|
} else { |
|
60
|
176 |
|
$this->localeXml = new SimpleXMLElement(StyleSheet::loadLocales($lang)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
176 |
|
$this->initLocaleXmlParser(); |
|
64
|
176 |
|
$this->parseXml($this->localeXml); |
|
65
|
176 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param SimpleXMLElement $xml |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
51 |
|
public function addXml(SimpleXMLElement $xml) |
|
72
|
|
|
{ |
|
73
|
51 |
|
$lang = (string) $xml->attributes('http://www.w3.org/XML/1998/namespace')->{'lang'}; |
|
74
|
51 |
|
if (empty($lang) || $this->getLanguage() === $lang || explode('-', $this->getLanguage())[0] === $lang) { |
|
75
|
48 |
|
$this->parseXml($xml); |
|
76
|
|
|
} |
|
77
|
51 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
34 |
|
public function getLanguage() |
|
84
|
|
|
{ |
|
85
|
34 |
|
return $this->language; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $type |
|
90
|
|
|
* @param $name |
|
91
|
|
|
* @param string $form |
|
92
|
|
|
* @return stdClass |
|
93
|
|
|
*/ |
|
94
|
118 |
|
public function filter($type, $name, $form = "long") |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
118 |
|
if ('options' === $type) { |
|
97
|
91 |
|
return $this->option($name); |
|
98
|
|
|
} |
|
99
|
94 |
|
if (!isset($this->{$type})) { |
|
100
|
|
|
throw new InvalidArgumentException("There is no locale of type \"$type\"."); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** @var ArrayList $localeList */ |
|
104
|
94 |
|
$localeList = $this->{$type}; |
|
105
|
|
|
|
|
106
|
94 |
|
if (is_null($name)) { |
|
107
|
|
|
$name = ""; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
//filter by name |
|
111
|
94 |
|
$array = $localeList->get($name); |
|
112
|
|
|
|
|
113
|
94 |
|
if (empty($array)) { |
|
114
|
30 |
|
$ret = new stdClass(); |
|
115
|
30 |
|
$ret->name = null; |
|
116
|
30 |
|
$ret->single = null; |
|
117
|
30 |
|
$ret->multiple = null; |
|
118
|
30 |
|
return $ret; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
//filter by form |
|
122
|
89 |
|
if ($type !== "options") { |
|
123
|
|
|
/** @var Term $value */ |
|
124
|
|
|
$array = array_filter($array, function ($term) use ($form) { |
|
125
|
89 |
|
return $term->form === $form; |
|
126
|
89 |
|
}); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
89 |
|
return array_pop($array); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
91 |
|
private function option($name) |
|
133
|
|
|
{ |
|
134
|
91 |
|
$result = null; |
|
135
|
91 |
|
foreach ($this->options as $key => $value) { |
|
136
|
91 |
|
if ($key === $name) { |
|
137
|
91 |
|
if (is_array($value) && isset($value[1]) && is_array($value[1])) { |
|
138
|
5 |
|
$result = reset($value[1]); |
|
139
|
|
|
} else { |
|
140
|
91 |
|
$result = reset($value); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
91 |
|
return $result; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|