Completed
Push — master ( 5d3cd3...160b9c )
by Sebastian
10:22
created

StringHelper::isLatinString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Util;
11
12
use Symfony\Polyfill\Mbstring\Mbstring;
13
14
/**
15
 * Class StringHelper
16
 * @package Seboettg\CiteProc\Util
17
 *
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
class StringHelper
21
{
22
23
    const PREPOSITIONS = [
24
        'on', 'in', 'at', 'since', 'for', 'ago', 'before', 'to', 'past', 'till', 'until', 'by', 'under', 'below', 'over',
25
        'above', 'across', 'through', 'into', 'towards', 'onto', 'from', 'of', 'off', 'about', 'via'
26
    ];
27
28
    const ARTICLES = [
29
        'a', 'an', 'the'
30
    ];
31
32
    const ADVERBS = [
33
        'yet', 'so', 'just', 'only'
34
    ];
35
36
    const CONJUNCTIONS = [
37
        'nor', 'so', 'and', 'or'
38
    ];
39
40
    const ADJECTIVES = [
41
        'down', 'up'
42
    ];
43
44
    const ISO_ENCODINGS = [
45
        'ISO-8859-1',
46
        'ISO-8859-2',
47
        'ISO-8859-3',
48
        'ISO-8859-4',
49
        'ISO-8859-5',
50
        'ISO-8859-6',
51
        'ISO-8859-7',
52
        'ISO-8859-8',
53
        'ISO-8859-9',
54
        'ISO-8859-10',
55
        'ISO-8859-11',
56
        'ISO-8859-13',
57
        'ISO-8859-14',
58
        'ISO-8859-15',
59
        'ISO-8859-16'
60
    ];
61
62
    /**
63
     * opening quote sign
64
     */
65
    const OPENING_QUOTE = "“";
66
67
    /**
68
     * closing quote sign
69
     */
70
    const CLOSING_QUOTE = "”";
71
72
    /**
73
     * @param $text
74
     * @return string
75
     */
76
    public static function capitalizeAll($text)
77
    {
78
        $wordArray = explode(" ", $text);
79
80
        array_walk($wordArray, function(&$word) {
81
            $word = ucfirst($word);
82
        });
83
84
        return implode(" ", $wordArray);
85
    }
86
87
    /**
88
     * @param $titleString
89
     * @return string
90
     */
91
    public static function capitalizeForTitle($titleString)
92
    {
93
        if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) {
94
            $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...
95
        }
96
97
        $wordArray = explode(" ", $titleString);
98
99
        array_walk($wordArray, function(&$word) {
100
101
            $words = explode("-", $word);
102
            if (count($words) > 1) {
103
                array_walk($words, function(&$w) {
104
                    $w = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
105
                });
106
                $word = implode("-", $words);
107
            }
108
            $word = StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
109
        });
110
111
        return implode(" ", $wordArray);
112
    }
113
114
    /**
115
     * @param $word
116
     * @return bool
117
     */
118
    public static function keepLowerCase($word)
119
    {
120
        $lowerCase = in_array($word, self::PREPOSITIONS) ||
121
            in_array($word, self::ARTICLES) ||
122
            in_array($word, self::CONJUNCTIONS) ||
123
            in_array($word, self::ADJECTIVES);
124
        return $lowerCase;
125
126
    }
127
128
    /**
129
     * @param $string
130
     * @param string $encoding
131
     * @return string
132
     */
133
    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...
134
    {
135
        $strlen = mb_strlen($string, $encoding);
136
        $firstChar = mb_substr($string, 0, 1, $encoding);
137
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
138
139
        $encoding = Mbstring::mb_detect_encoding($firstChar, self::ISO_ENCODINGS, true);
140
        return in_array($encoding, self::ISO_ENCODINGS) ? Mbstring::mb_strtoupper($firstChar, $encoding) . $then : $firstChar . $then;
141
    }
142
143
    /**
144
     * @param $string
145
     * @param $initializeSign
146
     * @return string
147
     */
148
    public static function initializeBySpaceOrHyphen($string, $initializeSign)
149
    {
150
        $res = "";
151
        $exploded = explode("-", $string);
152
        $i = 0;
153
        foreach ($exploded as $explode) {
154
            $spaceExploded = explode(" ", $explode);
155
            foreach ($spaceExploded as $givenPart) {
156
                $res .= substr($givenPart, 0, 1) . $initializeSign;
157
            }
158
            if ($i < count($exploded) - 1) {
159
                $res = rtrim($res) . "-";
160
            }
161
            ++$i;
162
        }
163
        return $res;
164
    }
165
166
    /**
167
     * @param $string
168
     * @return mixed|string
169
     */
170
    public static function camelCase2Hyphen($string)
171
    {
172
        $hyphenated = preg_replace("/([A-Z])/", "-$1", $string);
173
        $hyphenated = substr($hyphenated, 0, 1) === "-" ? substr($hyphenated, 1) : $hyphenated;
174
        return mb_strtolower($hyphenated);
175
    }
176
177
    /**
178
     * @param $string
179
     * @return bool
180
     */
181
    public static function checkLowerCaseString($string)
182
    {
183
        return ($string === mb_strtolower($string));
184
    }
185
186
    /**
187
     * @param $string
188
     * @return bool
189
     */
190
    public static function checkUpperCaseString($string)
191
    {
192
        return ($string === mb_strtoupper($string));
193
    }
194
195
    /**
196
     * @param $string
197
     * @return mixed
198
     */
199
    public static function clearApostrophes($string)
200
    {
201
        return preg_replace("/\'/", "’", $string);
202
    }
203
204
    /**
205
     * replaces outer quotes of $text by given inner quotes
206
     *
207
     * @param $text
208
     * @param $outerOpenQuote
209
     * @param $outerCloseQuote
210
     * @param $innerOpenQuote
211
     * @param $innerCloseQuote
212
     * @return string
213
     */
214
    public static function replaceOuterQuotes($text, $outerOpenQuote, $outerCloseQuote, $innerOpenQuote, $innerCloseQuote)
215
    {
216
        if (preg_match("/(.*)$outerOpenQuote(.+)$outerCloseQuote(.*)/u", $text, $match)) {
217
            return $match[1] . $innerOpenQuote . $match[2] . $innerCloseQuote . $match[3];
218
        }
219
        return $text;
220
    }
221
222
    /**
223
     * @param $string
224
     * @return bool
225
     */
226
    public static function isLatinString($string)
227
    {
228
        return boolval(preg_match_all("/^[\p{Latin}\s\p{P}]*$/u", $string));
229
        //return !$noLatin;
230
    }
231
232
    /**
233
     * @param $string
234
     * @return bool
235
     */
236
    public static function isCyrillicString($string)
237
    {
238
        return boolval(preg_match("/^[\p{Cyrillic}\s\p{P}]*$/u", $string));
239
    }
240
241
}