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