|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class StringLib |
|
18
|
|
|
*/ |
|
19
|
|
|
class StringLib |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Converts CamelCase text to under_score equivalent. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $input |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
9 |
|
public static function convertCamelCaseToUnderscore(string $input) : string |
|
28
|
|
|
{ |
|
29
|
9 |
|
$input[0] = strtolower($input[0]); |
|
30
|
9 |
|
return strtolower(preg_replace('/([A-Z])/', '_\\1', $input)); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Converts under_score to CamelCase equivalent. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $input |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public static function convertUnderscoreToCamelCase(string $input) : string |
|
40
|
|
|
{ |
|
41
|
4 |
|
$input = preg_replace('/_([a-zA-Z0-9])/', '#\\1', $input); |
|
42
|
4 |
|
$parts = explode('#', $input); |
|
43
|
4 |
|
array_walk( |
|
44
|
|
|
$parts, |
|
45
|
4 |
|
function (&$value) { |
|
46
|
4 |
|
$value = ucfirst(strtolower($value)); |
|
47
|
4 |
|
} |
|
48
|
|
|
); |
|
49
|
4 |
|
return implode($parts); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Converts all non-alphanumeric and additional extra characters to underscore. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $input |
|
56
|
|
|
* @param string $extraCharacters |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
11 |
|
public static function convertNonAlphanumericToUnderscore(string $input, string $extraCharacters = '') : string |
|
60
|
|
|
{ |
|
61
|
|
|
// Escape some characters that can affect badly the regular expression. |
|
62
|
11 |
|
$extraCharacters = str_replace( |
|
63
|
11 |
|
['-', '[', ']', '(', ')', '/', '$', '^'], |
|
64
|
11 |
|
['\\-', '\\[', '\\]', '\\(', '\\)', '\\/', '\\$', '\\^'], |
|
65
|
|
|
$extraCharacters |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
11 |
|
$output = preg_replace('/[^a-zA-Z0-9'.$extraCharacters.']/', '_', $input); |
|
69
|
|
|
|
|
70
|
11 |
|
while (strpos($output, '__') !== false) { |
|
71
|
2 |
|
$output = str_replace('__', '_', $output); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
11 |
|
return trim($output, '_'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|