Passed
Push — develop ( e162a8...209be3 )
by Sebastian
14:13 queued 09:03
created

StyleSheet::readFileContentsOrThrowException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc;
12
13
use Seboettg\CiteProc\Exception\CiteProcException;
14
15
/**
16
 * Class StyleSheet
17
 *
18
 * Helper class for loading CSL styles and CSL locales
19
 *
20
 * @package Seboettg\CiteProc
21
 * @author Sebastian Böttger <[email protected]>
22
 */
23
class StyleSheet
24
{
25
26
    /**
27
     * Loads xml formatted CSL stylesheet of a given stylesheet name, e.g. "american-physiological-society" for
28
     * apa style.
29
     *
30
     * See in styles folder (which is included as git submodule) for all available style sheets
31
     *
32
     * @param string $styleName e.g. "american-physiological-society" for apa
33
     * @return string
34
     * @throws CiteProcException
35
     */
36 13
    public static function loadStyleSheet(string $styleName): string
37
    {
38 13
        $stylesPath = self::vendorPath() . "/citation-style-language/styles";
39 13
        return self::readFileContentsOrThrowException("$stylesPath/$styleName.csl");
40
    }
41
42
    /**
43
     * Loads xml formatted locales of given language key
44
     *
45
     * @param string $langKey e.g. "en-US", or "de-CH"
46
     * @return string
47
     * @throws CiteProcException
48
     */
49 186
    public static function loadLocales(string $langKey): string
50
    {
51 186
        $localesPath = self::vendorPath()."/citation-style-language/locales";
52 186
        $localeFile = "$localesPath/locales-${langKey}.xml";
53 186
        if (file_exists($localeFile)) {
54 186
            return self::readFileContentsOrThrowException($localeFile);
55
        } else {
56
            $metadata = self::loadLocalesMetadata();
57
            if (!empty($metadata->{'primary-dialects'}->{$langKey})) {
58
                return self::readFileContentsOrThrowException(
59
                    sprintf("%s/locales-%s.xml", $localesPath, $metadata->{'primary-dialects'}->{$langKey})
60
                );
61
            }
62
        }
63
        throw new CiteProcException("No Locale file found for $langKey");
64
    }
65
66
    /**
67
     * @throws CiteProcException
68
     */
69 186
    private static function readFileContentsOrThrowException($path): string
70
    {
71 186
        $fileContent = file_get_contents($path);
72 186
        if (false === $fileContent) {
73
            throw new CiteProcException("Couldn't read $path");
74
        }
75 186
        return $fileContent;
76
    }
77
78
    /**
79
     * @return mixed
80
     * @throws CiteProcException
81
     */
82
    public static function loadLocalesMetadata()
83
    {
84
        $localesMetadataPath = self::vendorPath() . "/citation-style-language/locales/locales.json";
85
        return json_decode(self::readFileContentsOrThrowException($localesMetadataPath));
86
    }
87
88
    /**
89
     * @return bool|string
90
     * @throws CiteProcException
91
     */
92 186
    private static function vendorPath()
93
    {
94 186
        include_once realpath(__DIR__ . '/../') . '/vendorPath.php';
95 186
        if (!($vendorPath = vendorPath())) {
96
            throw new CiteProcException('vendor path not found. Use composer to initialize your project');
97
        }
98 186
        return $vendorPath;
99
    }
100
}
101