|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Talentify\ValueObject; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @internal |
|
9
|
|
|
*/ |
|
10
|
|
|
class StringUtils |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Remove not wanted whitespace-like characters. |
|
14
|
|
|
* |
|
15
|
|
|
* @see https://en.wikipedia.org/wiki/Whitespace_character |
|
16
|
|
|
* @see https://en.wikipedia.org/wiki/Regular_expression#Examples |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function trimSpacesWisely(string $value) : ?string |
|
19
|
|
|
{ |
|
20
|
|
|
// remove tab, return and new line |
|
21
|
|
|
$value = mb_ereg_replace('[\t\r\n]', '', trim($value)); |
|
22
|
|
|
// replace two or more whitespaces with only one |
|
23
|
|
|
$value = preg_replace('!\s+!', ' ', $value); |
|
24
|
|
|
|
|
25
|
|
|
return empty($value) ? null : $value; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function removeSpaces(string $value) : ?string |
|
29
|
|
|
{ |
|
30
|
|
|
return mb_ereg_replace('\s', '', $value); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function removeNonWordCharacters(string $value) : string |
|
34
|
|
|
{ |
|
35
|
|
|
return mb_ereg_replace("[^ \w]+", '', $value); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function convertCaseToTitle(string $value) : string |
|
39
|
|
|
{ |
|
40
|
|
|
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function convertCaseToUpper(string $value) : string |
|
44
|
|
|
{ |
|
45
|
|
|
return mb_convert_case($value, MB_CASE_UPPER, 'UTF-8'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function convertCaseToLower(string $value) : string |
|
49
|
|
|
{ |
|
50
|
|
|
return mb_convert_case($value, MB_CASE_LOWER, 'UTF-8'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function countCharacters(string $value) : int |
|
54
|
|
|
{ |
|
55
|
|
|
return strlen($value); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|