Passed
Pull Request — master (#54)
by Jonathon
02:30
created

StyleSheet::loadLocalesMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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;
11
12
use Seboettg\CiteProc\Exception\CiteProcException;
13
14
/**
15
 * Class StyleSheet
16
 *
17
 * Helper class for loading CSL styles and CSL locales
18
 *
19
 * @package Seboettg\CiteProc
20
 * @author Sebastian Böttger <[email protected]>
21
 */
22
class StyleSheet
23
{
24
25
    /**
26
     * Loads xml formatted CSL stylesheet of a given stylesheet name, e.g. "american-physiological-society" for
27
     * apa style.
28
     *
29
     * See in styles folder (which is included as git submodule) for all available style sheets
30
     *
31
     * @param string $styleName e.g. "american-physiological-society" for apa
32
     * @return string
33
     */
34
    public static function loadStyleSheet($styleName)
35
    {
36
        $stylesPath = self::vendorPath() . "/citation-style-language/styles-distribution/";
37
        return file_get_contents($stylesPath . $styleName . '.csl');
38
    }
39
40
    /**
41
     * Loads xml formatted locales of given language key
42
     *
43
     * @param string $langKey e.g. "en-US", or "de-CH"
44
     * @return string
45
     */
46
    public static function loadLocales($langKey)
47
    {
48
        $localesPath = self::vendorPath() . "/citation-style-language/locales/";
49
        $data = @file_get_contents($localesPath . "locales-" . $langKey . '.xml');
50
51
        if ($data === false) {
52
            $metadata = self::loadLocalesMetadata();
53
            if (!empty($metadata->{'primary-dialects'}->{$langKey})) {
54
            $data = file_get_contents($localesPath . "locales-" . $metadata->{'primary-dialects'}->{$langKey} . '.xml');
55
            }
56
        }
57
58
        return $data;
59
    }
60
61
    public static function loadLocalesMetadata()
62
    {
63
        $localesMetadataPath = self::vendorPath() . "/citation-style-language/locales/locales.json";
64
        return json_decode(file_get_contents($localesMetadataPath));
65
    }
66
67
    /**
68
     * @return bool|string
69
     * @throws CiteProcException
70
     */
71
    private static function vendorPath()
72
    {
73
        include_once __DIR__ . '/../../../vendorPath.php';
74
        if (!($vendorPath = vendorPath())) {
75
            // @codeCoverageIgnoreStart
76
            throw new CiteProcException('vendor path not found. Use composer to initialize your project');
77
            // @codeCoverageIgnoreEnd
78
        }
79
        return $vendorPath;
80
    }
81
}