Passed
Push — master ( f36a08...6258cd )
by Sebastian
12:50
created

StyleSheet::loadLocalesMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
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
     * @throws CiteProcException
34
     */
35 12
    public static function loadStyleSheet($styleName)
36
    {
37 12
        $stylesPath = self::vendorPath() . "/citation-style-language/styles-distribution/";
38 12
        return file_get_contents($stylesPath . $styleName . '.csl');
39
    }
40
41
    /**
42
     * Loads xml formatted locales of given language key
43
     *
44
     * @param string $langKey e.g. "en-US", or "de-CH"
45
     * @return string
46
     * @throws CiteProcException
47
     */
48 164
    public static function loadLocales($langKey)
49
    {
50 164
        $data = null;
51 164
        $localesPath = self::vendorPath() . "/citation-style-language/locales/";
52 164
        $localeFile = $localesPath . "locales-" . $langKey . '.xml';
53 164
        if (file_exists($localeFile)) {
54 164
            $data = file_get_contents($localeFile);
55
        } else {
56
            $metadata = self::loadLocalesMetadata();
57
            if (!empty($metadata->{'primary-dialects'}->{$langKey})) {
58
                $data = file_get_contents(
59
                    $localesPath . "locales-" . $metadata->{'primary-dialects'}->{$langKey} . '.xml'
60
                );
61
            }
62
        }
63
64 164
        return $data;
65
    }
66
67
    /**
68
     * @return mixed
69
     * @throws CiteProcException
70
     */
71
    public static function loadLocalesMetadata()
72
    {
73
        $localesMetadataPath = self::vendorPath() . "/citation-style-language/locales/locales.json";
74
        return json_decode(file_get_contents($localesMetadataPath));
75
    }
76
77
    /**
78
     * @return bool|string
79
     * @throws CiteProcException
80
     */
81 164
    private static function vendorPath()
82
    {
83 164
        include_once realpath(__DIR__ . '/../') . '/vendorPath.php';
84 164
        if (!($vendorPath = vendorPath())) {
85
            // @codeCoverageIgnoreStart
86
            throw new CiteProcException('vendor path not found. Use composer to initialize your project');
87
            // @codeCoverageIgnoreEnd
88
        }
89 164
        return $vendorPath;
90
    }
91
}
92