Completed
Push — master ( 5ff550...c55afe )
by Sebastian
06:16
created

StringHelper::initializeBySpaceOrHyphen()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 23
rs 6.7272
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\Util;
11
12
use Seboettg\CiteProc\CiteProc;
13
use Symfony\Polyfill\Mbstring\Mbstring;
14
15
/**
16
 * Class StringHelper
17
 * @package Seboettg\CiteProc\Util
18
 *
19
 * @author Sebastian Böttger <[email protected]>
20
 */
21
class StringHelper
22
{
23
24
    const PREPOSITIONS = [
25
        'on', 'in', 'at', 'since', 'for', 'ago', 'before', 'to', 'past', 'till', 'until', 'by', 'under', 'below', 'over',
26
        'above', 'across', 'through', 'into', 'towards', 'onto', 'from', 'of', 'off', 'about', 'via'
27
    ];
28
29
    const ARTICLES = [
30
        'a', 'an', 'the'
31
    ];
32
33
    const ADVERBS = [
34
        'yet', 'so', 'just', 'only'
35
    ];
36
37
    const CONJUNCTIONS = [
38
        'nor', 'so', 'and', 'or'
39
    ];
40
41
    const ADJECTIVES = [
42
        'down', 'up'
43
    ];
44
45
    const ISO_ENCODINGS = [
46
        'ISO-8859-1',
47
        'ISO-8859-2',
48
        'ISO-8859-3',
49
        'ISO-8859-4',
50
        'ISO-8859-5',
51
        'ISO-8859-6',
52
        'ISO-8859-7',
53
        'ISO-8859-8',
54
        'ISO-8859-9',
55
        'ISO-8859-10',
56
        'ISO-8859-11',
57
        'ISO-8859-13',
58
        'ISO-8859-14',
59
        'ISO-8859-15',
60
        'ISO-8859-16'
61
    ];
62
63
    /**
64
     * opening quote sign
65
     */
66
    const OPENING_QUOTE = "“";
67
68
    /**
69
     * closing quote sign
70
     */
71
    const CLOSING_QUOTE = "”";
72
73
    /**
74
     * @param $text
75
     * @return string
76
     */
77
    public static function capitalizeAll($text)
78
    {
79
        $wordArray = explode(" ", $text);
80
81
        array_walk($wordArray, function(&$word) {
82
            $word = ucfirst($word);
83
        });
84
85
        return implode(" ", $wordArray);
86
    }
87
88
    /**
89
     * @param $titleString
90
     * @return string
91
     */
92
    public static function capitalizeForTitle($titleString)
93
    {
94
        if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) {
95
            $titleString = $match[1] . StringHelper::mb_ucfirst($match[2]) . $match[3];
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
96
        }
97
98
        $wordArray = explode(" ", $titleString);
99
100
        array_walk($wordArray, function(&$word) {
101
102
            $words = explode("-", $word);
103
            if (count($words) > 1) {
104
                array_walk($words, function(&$w) {
105
                    $w = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
106
                });
107
                $word = implode("-", $words);
108
            }
109
            $word = StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
110
        });
111
112
        return implode(" ", $wordArray);
113
    }
114
115
    /**
116
     * @param $word
117
     * @return bool
118
     */
119
    public static function keepLowerCase($word)
120
    {
121
        $lowerCase = in_array($word, self::PREPOSITIONS) ||
122
            in_array($word, self::ARTICLES) ||
123
            in_array($word, self::CONJUNCTIONS) ||
124
            in_array($word, self::ADJECTIVES);
125
        return $lowerCase;
126
127
    }
128
129
    /**
130
     * @param $string
131
     * @param string $encoding
132
     * @return string
133
     */
134
    public static function mb_ucfirst($string, $encoding = 'UTF-8')
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
135
    {
136
        $strlen = mb_strlen($string, $encoding);
137
        $firstChar = mb_substr($string, 0, 1, $encoding);
138
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
139
140
        $encoding = Mbstring::mb_detect_encoding($firstChar, self::ISO_ENCODINGS, true);
141
        return in_array($encoding, self::ISO_ENCODINGS) ? Mbstring::mb_strtoupper($firstChar, $encoding) . $then : $firstChar . $then;
142
    }
143
144
    /**
145
     * @param $string
146
     * @param $initializeSign
147
     * @return string
148
     */
149
    public static function initializeBySpaceOrHyphen($string, $initializeSign)
150
    {
151
        $initializeWithHyphen = CiteProc::getContext()->getGlobalOptions()->isInitializeWithHyphen();
152
        $res = "";
153
        $exploded = explode("-", $string);
154
        $i = 0;
155
        foreach ($exploded as $explode) {
156
            $spaceExploded = explode(" ", $explode);
157
            foreach ($spaceExploded as $givenPart) {
158
                $firstLetter = mb_substr($givenPart, 0, 1, "UTF-8");
159
                if (StringHelper::isLatinString($firstLetter)) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
160
                    $res .= ctype_upper($firstLetter) ? $firstLetter . $initializeSign : " " . $givenPart . " ";
161
                } else {
162
                    $res .= $firstLetter . $initializeSign;
163
                }
164
            }
165
            if ($i < count($exploded) - 1 && $initializeWithHyphen) {
166
                $res = rtrim($res) . "-";
167
            }
168
            ++$i;
169
        }
170
        return $res;
171
    }
172
173
    /**
174
     * @param $string
175
     * @return mixed|string
176
     */
177
    public static function camelCase2Hyphen($string)
178
    {
179
        $hyphenated = preg_replace("/([A-Z])/", "-$1", $string);
180
        $hyphenated = substr($hyphenated, 0, 1) === "-" ? substr($hyphenated, 1) : $hyphenated;
181
        return mb_strtolower($hyphenated);
182
    }
183
184
    /**
185
     * @param $string
186
     * @return bool
187
     */
188
    public static function checkLowerCaseString($string)
189
    {
190
        return ($string === mb_strtolower($string));
191
    }
192
193
    /**
194
     * @param $string
195
     * @return bool
196
     */
197
    public static function checkUpperCaseString($string)
198
    {
199
        return ($string === mb_strtoupper($string));
200
    }
201
202
    /**
203
     * @param $string
204
     * @return mixed
205
     */
206
    public static function clearApostrophes($string)
207
    {
208
        return preg_replace("/\'/", "’", $string);
209
    }
210
211
    /**
212
     * replaces outer quotes of $text by given inner quotes
213
     *
214
     * @param $text
215
     * @param $outerOpenQuote
216
     * @param $outerCloseQuote
217
     * @param $innerOpenQuote
218
     * @param $innerCloseQuote
219
     * @return string
220
     */
221
    public static function replaceOuterQuotes($text, $outerOpenQuote, $outerCloseQuote, $innerOpenQuote, $innerCloseQuote)
222
    {
223
        if (preg_match("/(.*)$outerOpenQuote(.+)$outerCloseQuote(.*)/u", $text, $match)) {
224
            return $match[1] . $innerOpenQuote . $match[2] . $innerCloseQuote . $match[3];
225
        }
226
        return $text;
227
    }
228
229
    /**
230
     * @param $string
231
     * @return bool
232
     */
233
    public static function isLatinString($string)
234
    {
235
        return boolval(preg_match_all("/^[\p{Latin}\s\p{P}]*$/u", $string));
236
        //return !$noLatin;
237
    }
238
239
    /**
240
     * @param $string
241
     * @return bool
242
     */
243
    public static function isCyrillicString($string)
244
    {
245
        return boolval(preg_match("/^[\p{Cyrillic}\s\p{P}]*$/u", $string));
246
    }
247
248
    /**
249
     * removes all kind of brackets from a given string
250
     * @param $datePart
251
     * @return mixed
252
     */
253
    public static function removeBrackets($datePart) {
254
        return str_replace(["[","]", "(", ")", "{", "}"], "", $datePart);
255
    }
256
}