1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* citeproc-php |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
6
|
|
|
* @copyright Copyright (c) 2020 Sebastian Böttger. |
7
|
|
|
* @license https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc; |
11
|
|
|
|
12
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* System locale-save implementation of \ucfirst. For example, when using the tr_TR locale, \ucfirst('i') yields "i". |
16
|
|
|
* This implementation of ucfirst is locale-independent. |
17
|
|
|
* @param string $string |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
function ucfirst(string $string): string |
21
|
|
|
{ |
22
|
88 |
|
$firstChar = substr($string, 0, 1); |
23
|
88 |
|
$firstCharUpper = strtr($firstChar, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); |
24
|
88 |
|
return $firstCharUpper . substr($string, 1); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
* @throws CiteProcException |
30
|
|
|
*/ |
31
|
|
|
function vendorPath(): string |
32
|
|
|
{ |
33
|
177 |
|
include_once realpath(__DIR__ . '/../') . '/vendorPath.php'; |
34
|
177 |
|
if (!($vendorPath = \vendorPath())) { |
35
|
|
|
// @codeCoverageIgnoreStart |
36
|
|
|
throw new CiteProcException('Vendor path not found. Use composer to initialize your project'); |
37
|
|
|
// @codeCoverageIgnoreEnd |
38
|
|
|
} |
39
|
177 |
|
return $vendorPath; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Loads xml formatted CSL stylesheet of a given stylesheet name, e.g. "american-physiological-society" for |
45
|
|
|
* apa style. |
46
|
|
|
* |
47
|
|
|
* See in styles folder (which is included as git submodule) for all available style sheets |
48
|
|
|
* |
49
|
|
|
* @param string $styleName e.g. "apa" for APA style |
50
|
|
|
* @return StyleSheet |
51
|
|
|
* @throws CiteProcException |
52
|
|
|
*/ |
53
|
|
|
function loadStyleSheet(string $styleName) |
54
|
|
|
{ |
55
|
12 |
|
$stylesPath = vendorPath() . "/citation-style-language/styles-distribution"; |
56
|
12 |
|
$fileName = sprintf('%s/%s.csl', $stylesPath, $styleName); |
57
|
12 |
|
if (!file_exists($fileName)) { |
58
|
|
|
throw new CiteProcException(sprintf('Stylesheet "%s" not found', $fileName)); |
59
|
|
|
} |
60
|
12 |
|
$styleSheet = file_get_contents($fileName); |
61
|
12 |
|
return new StyleSheet($styleSheet); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Loads xml formatted locales of given language key |
68
|
|
|
* |
69
|
|
|
* @param string $langKey e.g. "en-US", or "de-CH" |
70
|
|
|
* @return string |
71
|
|
|
* @throws CiteProcException |
72
|
|
|
*/ |
73
|
|
|
function loadLocales(string $langKey) |
74
|
|
|
{ |
75
|
177 |
|
$data = null; |
76
|
177 |
|
$localesPath = vendorPath() . "/citation-style-language/locales/"; |
77
|
177 |
|
$localeFile = $localesPath."locales-".$langKey.'.xml'; |
78
|
177 |
|
if (file_exists($localeFile)) { |
79
|
177 |
|
$data = file_get_contents($localeFile); |
80
|
|
|
} else { |
81
|
|
|
$metadata = loadLocalesMetadata(); |
82
|
|
|
if (!empty($metadata->{'primary-dialects'}->{$langKey})) { |
83
|
|
|
$data = file_get_contents( |
84
|
|
|
$localesPath."locales-".$metadata->{'primary-dialects'}->{$langKey}.'.xml' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
177 |
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return mixed |
93
|
|
|
* @throws CiteProcException |
94
|
|
|
*/ |
95
|
|
|
function loadLocalesMetadata() |
96
|
|
|
{ |
97
|
|
|
$localesMetadataPath = vendorPath() . "/citation-style-language/locales/locales.json"; |
98
|
|
|
return json_decode(file_get_contents($localesMetadataPath)); |
99
|
|
|
} |
100
|
|
|
|