Completed
Push — master ( 160b9c...5c6970 )
by Sebastian
02:30
created

StringHelper::initializeBySpaceOrHyphen()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
cc 6
eloc 14
nc 7
nop 2
rs 8.8571
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 = substr($givenPart, 0, 1);
159
                $res .= ctype_upper($firstLetter) ? $firstLetter . $initializeSign : " " . $givenPart . " ";
160
            }
161
            if ($i < count($exploded) - 1 && $initializeWithHyphen) {
162
                $res = rtrim($res) . "-";
163
            }
164
            ++$i;
165
        }
166
        return $res;
167
    }
168
169
    /**
170
     * @param $string
171
     * @return mixed|string
172
     */
173
    public static function camelCase2Hyphen($string)
174
    {
175
        $hyphenated = preg_replace("/([A-Z])/", "-$1", $string);
176
        $hyphenated = substr($hyphenated, 0, 1) === "-" ? substr($hyphenated, 1) : $hyphenated;
177
        return mb_strtolower($hyphenated);
178
    }
179
180
    /**
181
     * @param $string
182
     * @return bool
183
     */
184
    public static function checkLowerCaseString($string)
185
    {
186
        return ($string === mb_strtolower($string));
187
    }
188
189
    /**
190
     * @param $string
191
     * @return bool
192
     */
193
    public static function checkUpperCaseString($string)
194
    {
195
        return ($string === mb_strtoupper($string));
196
    }
197
198
    /**
199
     * @param $string
200
     * @return mixed
201
     */
202
    public static function clearApostrophes($string)
203
    {
204
        return preg_replace("/\'/", "’", $string);
205
    }
206
207
    /**
208
     * replaces outer quotes of $text by given inner quotes
209
     *
210
     * @param $text
211
     * @param $outerOpenQuote
212
     * @param $outerCloseQuote
213
     * @param $innerOpenQuote
214
     * @param $innerCloseQuote
215
     * @return string
216
     */
217
    public static function replaceOuterQuotes($text, $outerOpenQuote, $outerCloseQuote, $innerOpenQuote, $innerCloseQuote)
218
    {
219
        if (preg_match("/(.*)$outerOpenQuote(.+)$outerCloseQuote(.*)/u", $text, $match)) {
220
            return $match[1] . $innerOpenQuote . $match[2] . $innerCloseQuote . $match[3];
221
        }
222
        return $text;
223
    }
224
225
    /**
226
     * @param $string
227
     * @return bool
228
     */
229
    public static function isLatinString($string)
230
    {
231
        return boolval(preg_match_all("/^[\p{Latin}\s\p{P}]*$/u", $string));
232
        //return !$noLatin;
233
    }
234
235
    /**
236
     * @param $string
237
     * @return bool
238
     */
239
    public static function isCyrillicString($string)
240
    {
241
        return boolval(preg_match("/^[\p{Cyrillic}\s\p{P}]*$/u", $string));
242
    }
243
244
}