Passed
Push — new-api ( f151f9...5a646f )
by Sebastian
04:44
created

array_clone()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 3
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) 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
use Seboettg\Collection\ArrayList\ArrayListInterface;
14
15
/**
16
 * System locale-save implementation of \ucfirst. For example, when using the tr_TR locale, \ucfirst('i') yields "i".
17
 * This implementation of ucfirst is locale-independent.
18
 * @param string $string
19
 * @return string
20
 */
21
function ucfirst(string $string): string
22
{
23 92
    $firstChar = substr($string, 0, 1);
24 92
    $firstCharUpper = strtr($firstChar, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
25 92
    return $firstCharUpper . substr($string, 1);
26
}
27
28
/**
29
 * @return string
30
 * @throws CiteProcException
31
 */
32
function vendorPath(): string
33
{
34 184
    include_once realpath(__DIR__ . '/../') . '/vendorPath.php';
35 184
    if (!($vendorPath = \vendorPath())) {
36
        // @codeCoverageIgnoreStart
37
        throw new CiteProcException('Vendor path not found. Use composer to initialize your project');
38
        // @codeCoverageIgnoreEnd
39
    }
40 184
    return $vendorPath;
41
}
42
43
44
/**
45
 * Loads xml formatted CSL stylesheet of a given stylesheet name, e.g. "american-physiological-society" for
46
 * apa style.
47
 *
48
 * See in styles folder (which is included as git submodule) for all available style sheets
49
 *
50
 * @param string $styleName e.g. "apa" for APA style
51
 * @return StyleSheet
52
 * @throws CiteProcException
53
 */
54
function loadStyleSheet(string $styleName)
55
{
56 12
    $stylesPath = vendorPath() . "/citation-style-language/styles-distribution";
57 12
    $fileName = sprintf('%s/%s.csl', $stylesPath, $styleName);
58 12
    if (!file_exists($fileName)) {
59
        throw new CiteProcException(sprintf('Stylesheet "%s" not found', $fileName));
60
    }
61 12
    $styleSheet = file_get_contents($fileName);
62 12
    return new StyleSheet($styleSheet);
63
}
64
65
66
67
/**
68
 * Loads xml formatted locales of given language key
69
 *
70
 * @param string $langKey e.g. "en-US", or "de-CH"
71
 * @return string
72
 * @throws CiteProcException
73
 */
74
function loadLocales(string $langKey)
75
{
76 184
    $data = null;
77 184
    $localesPath = vendorPath() . "/citation-style-language/locales/";
78 184
    $localeFile = $localesPath."locales-".$langKey.'.xml';
79 184
    if (file_exists($localeFile)) {
80 184
        $data = file_get_contents($localeFile);
81
    } else {
82
        $metadata = loadLocalesMetadata();
83
        if (!empty($metadata->{'primary-dialects'}->{$langKey})) {
84
            $data = file_get_contents(
85
                $localesPath."locales-".$metadata->{'primary-dialects'}->{$langKey}.'.xml'
86
            );
87
        }
88
    }
89 184
    return $data;
90
}
91
92
/**
93
 * @return mixed
94
 * @throws CiteProcException
95
 */
96
function loadLocalesMetadata()
97
{
98
    $localesMetadataPath = vendorPath() . "/citation-style-language/locales/locales.json";
99
    return json_decode(file_get_contents($localesMetadataPath));
100
}
101
102
/**
103
 * @param ArrayListInterface $list
104
 * @param mixed $id
105
 * @return mixed
106
 */
107
function getCurrentById(ArrayListInterface $list, $id)
108
{
109
    $l = $list->filter(function ($item) use ($id) {
110 7
        return $item->id === $id;
111 8
    });
112 8
    return $l->current();
113
}
114
115
function array_clone($array): array
116
{
117
    return array_map(function ($element) {
118 1
        return is_array($element) ? array_clone($element) : (is_object($element) ? clone $element : $element);
119 1
    }, $array);
120
}
121