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

StringHelper   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Test Coverage

Coverage 98.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 97
c 1
b 0
f 0
dl 0
loc 272
ccs 80
cts 81
cp 0.9877
rs 9.52
wmc 36

15 Methods

Rating   Name   Duplication   Size   Complexity  
A checkUpperCaseString() 0 3 1
A isCyrillicString() 0 3 1
A implodeAndPreventConsecutiveChars() 0 14 4
A capitalizeAll() 0 9 1
A clearApostrophes() 0 3 1
A mb_ucfirst() 0 10 2
A replaceOuterQuotes() 0 11 2
A isLatinString() 0 3 1
A checkLowerCaseString() 0 3 1
A keepLowerCase() 0 8 5
A camelCase2Hyphen() 0 5 2
A removeBrackets() 0 3 1
A capitalizeForTitle() 0 21 5
B initializeBySpaceOrHyphen() 0 22 7
A mb_strrev() 0 7 2
1
<?php /** @noinspection PhpInternalEntityUsedInspection */
2
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\Util;
12
13
use Seboettg\CiteProc\CiteProc;
14
use Symfony\Polyfill\Mbstring\Mbstring;
15
16
/**
17
 * Class StringHelper
18
 * @package Seboettg\CiteProc\Util
19
 *
20
 * @author Sebastian Böttger <[email protected]>
21
 */
22
class StringHelper
23
{
24
25
    const PREPOSITIONS = [
26
        'on', 'in', 'at', 'since', 'for', 'ago', 'before', 'to', 'past', 'till', 'until', 'by', 'under', 'below',
27
        'over', 'above', 'across', 'through', 'into', 'towards', 'onto', 'from', 'of', 'off', 'about', 'via'
28
    ];
29
30
    const ARTICLES = [
31
        'a', 'an', 'the'
32
    ];
33
34
    const ADVERBS = [
35
        'yet', 'so', 'just', 'only'
36
    ];
37
38
    const CONJUNCTIONS = [
39
        'nor', 'so', 'and', 'or'
40
    ];
41
42
    const ADJECTIVES = [
43
        'down', 'up'
44
    ];
45
46
    const ISO_ENCODINGS = [
47
        'ISO-8859-1',
48
        'ISO-8859-2',
49
        'ISO-8859-3',
50
        'ISO-8859-4',
51
        'ISO-8859-5',
52
        'ISO-8859-6',
53
        'ISO-8859-7',
54
        'ISO-8859-8',
55
        'ISO-8859-9',
56
        'ISO-8859-10',
57
        'ISO-8859-11',
58
        'ISO-8859-13',
59
        'ISO-8859-14',
60
        'ISO-8859-15',
61
        'ISO-8859-16'
62
    ];
63
64
    /**
65
     * opening quote sign
66
     */
67
    const OPENING_QUOTE = "“";
68
69
    /**
70
     * closing quote sign
71
     */
72
    const CLOSING_QUOTE = "”";
73
74
    /**
75
     * @param $text
76
     * @return string
77
     */
78 1
    public static function capitalizeAll($text)
79
    {
80 1
        $wordArray = explode(" ", $text);
81
82
        array_walk($wordArray, function (&$word) {
83 1
            $word = ucfirst($word);
84 1
        });
85
86 1
        return implode(" ", $wordArray);
87
    }
88
89
    /**
90
     * @param $titleString
91
     * @return string
92
     */
93 10
    public static function capitalizeForTitle($titleString)
94
    {
95 10
        if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) {
96
            $titleString = $match[1] . StringHelper::mb_ucfirst($match[2]) . $match[3];
97
        }
98
99 10
        $wordArray = explode(" ", $titleString);
100
101
        array_walk($wordArray, function (&$word) {
102
103 10
            $words = explode("-", $word);
104 10
            if (count($words) > 1) {
105
                array_walk($words, function (&$w) {
106 3
                    $w = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
107 3
                });
108 3
                $word = implode("-", $words);
109
            }
110 10
            $word = StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
111 10
        });
112
113 10
        return implode(" ", $wordArray);
114
    }
115
116
    /**
117
     * @param $word
118
     * @return bool
119
     */
120 10
    public static function keepLowerCase($word)
121
    {
122
        // keep lower case if the first char is not an utf-8 letter
123 10
        return in_array($word, self::PREPOSITIONS) ||
124 10
            in_array($word, self::ARTICLES) ||
125 10
            in_array($word, self::CONJUNCTIONS) ||
126 10
            in_array($word, self::ADJECTIVES) ||
127 10
            (bool) preg_match("/[^\p{L}].+/", $word);
128
    }
129
130
    /**
131
     * @param $string
132
     * @param string $encoding
133
     * @return string
134
     */
135 23
    public static function mb_ucfirst($string, $encoding = 'UTF-8')
0 ignored issues
show
Coding Style introduced by
Method name "StringHelper::mb_ucfirst" is not in camel caps format
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$encoding" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$encoding"; expected 0 but found 1
Loading history...
136
    {
137 23
        $strlen = mb_strlen($string, $encoding);
138 23
        $firstChar = mb_substr($string, 0, 1, $encoding);
139 23
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
140
141
        /** @noinspection PhpInternalEntityUsedInspection */
142 23
        $encoding = Mbstring::mb_detect_encoding($firstChar, self::ISO_ENCODINGS, true);
143 23
        return in_array($encoding, self::ISO_ENCODINGS) ?
144 23
            Mbstring::mb_strtoupper($firstChar, $encoding) . $then : $firstChar . $then;
145
    }
146
147 39
    public static function mb_strrev($string)
0 ignored issues
show
Coding Style introduced by
Method name "StringHelper::mb_strrev" is not in camel caps format
Loading history...
148
    {
149 39
        $result = '';
150 39
        for ($i = mb_strlen($string); $i >= 0; --$i) {
151 39
            $result .= mb_substr($string, $i, 1);
152
        }
153 39
        return $result;
154
    }
155
156
    /**
157
     * @param string $delimiter
158
     * @param string[] $arrayOfStrings
159
     * @return string;
160
     */
161 72
    public static function implodeAndPreventConsecutiveChars($delimiter, $arrayOfStrings)
162
    {
163 72
        $delim = trim($delimiter);
164 72
        if (!empty($delim)) {
165 39
            foreach ($arrayOfStrings as $key => $textPart) {
166 39
                $pos = mb_strpos(StringHelper::mb_strrev($textPart), StringHelper::mb_strrev($delim));
167 39
                if ($pos === 0) {
168 3
                    $length = mb_strlen($textPart) - mb_strlen($delim);
169 3
                    $textPart = mb_substr($textPart, 0, $length);
170 3
                    $arrayOfStrings[$key] = $textPart;
171
                }
172
            }
173
        }
174 72
        return implode($delimiter, $arrayOfStrings);
175
    }
176
177
    /**
178
     * @param $string
179
     * @param $initializeSign
180
     * @return string
181
     */
182 28
    public static function initializeBySpaceOrHyphen($string, $initializeSign)
183
    {
184 28
        $initializeWithHyphen = CiteProc::getContext()->getGlobalOptions()->isInitializeWithHyphen();
185 28
        $res = "";
186 28
        $exploded = explode("-", $string);
187 28
        $i = 0;
188 28
        foreach ($exploded as $explode) {
189 28
            $spaceExploded = explode(" ", $explode);
190 28
            foreach ($spaceExploded as $givenPart) {
191 28
                $firstLetter = mb_substr($givenPart, 0, 1, "UTF-8");
192 28
                if (StringHelper::isLatinString($firstLetter)) {
193 26
                    $res .= ctype_upper($firstLetter) ? $firstLetter . $initializeSign : " " . $givenPart . " ";
194
                } else {
195 2
                    $res .= $firstLetter . $initializeSign;
196
                }
197
            }
198 28
            if ($i < count($exploded) - 1 && $initializeWithHyphen) {
199 2
                $res = rtrim($res) . "-";
200
            }
201 28
            ++$i;
202
        }
203 28
        return $res;
204
    }
205
206
    /**
207
     * @param $string
208
     * @return mixed|string
209
     */
210 1
    public static function camelCase2Hyphen($string)
211
    {
212 1
        $hyphenated = preg_replace("/([A-Z])/", "-$1", $string);
213 1
        $hyphenated = substr($hyphenated, 0, 1) === "-" ? substr($hyphenated, 1) : $hyphenated;
214 1
        return mb_strtolower($hyphenated);
215
    }
216
217
    /**
218
     * @param $string
219
     * @return bool
220
     */
221 1
    public static function checkLowerCaseString($string)
222
    {
223 1
        return ($string === mb_strtolower($string));
224
    }
225
226
    /**
227
     * @param $string
228
     * @return bool
229
     */
230 2
    public static function checkUpperCaseString($string)
231
    {
232 2
        return ($string === mb_strtoupper($string));
233
    }
234
235
    /**
236
     * @param $string
237
     * @return mixed
238
     */
239 154
    public static function clearApostrophes($string)
240
    {
241 154
        return preg_replace("/\'/", "’", $string);
242
    }
243
244
    /**
245
     * replaces outer quotes of $text by given inner quotes
246
     *
247
     * @param $text
248
     * @param $outerOpenQuote
249
     * @param $outerCloseQuote
250
     * @param $innerOpenQuote
251
     * @param $innerCloseQuote
252
     * @return string
253
     */
254 7
    public static function replaceOuterQuotes(
255
        $text,
256
        $outerOpenQuote,
257
        $outerCloseQuote,
258
        $innerOpenQuote,
259
        $innerCloseQuote
260
    ) {
261 7
        if (preg_match("/(.*)$outerOpenQuote(.+)$outerCloseQuote(.*)/u", $text, $match)) {
262 3
            return $match[1] . $innerOpenQuote . $match[2] . $innerCloseQuote . $match[3];
263
        }
264 6
        return $text;
265
    }
266
267
    /**
268
     * @param $string
269
     * @return bool
270
     */
271 98
    public static function isLatinString($string)
272
    {
273 98
        return boolval(preg_match_all("/^[\p{Latin}\p{Common}]+$/u", $string));
274
        //return !$noLatin;
275
    }
276
277
    /**
278
     * @param $string
279
     * @return bool
280
     */
281 4
    public static function isCyrillicString($string)
282
    {
283 4
        return boolval(preg_match("/^[\p{Cyrillic}\p{Common}]+$/u", $string));
284
    }
285
286
    /**
287
     * removes all kind of brackets from a given string
288
     * @param $datePart
289
     * @return mixed
290
     */
291 55
    public static function removeBrackets($datePart)
292
    {
293 55
        return str_replace(["[","]", "(", ")", "{", "}"], "", $datePart);
294
    }
295
}
296