1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\base; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* String utilities |
7
|
|
|
* |
8
|
|
|
* @author jc |
9
|
|
|
* @version 1.0.0.2 |
10
|
|
|
*/ |
11
|
|
|
class UString { |
12
|
|
|
|
13
|
6 |
|
public static function startswith($hay, $needle) { |
14
|
6 |
|
return \substr ( $hay, 0, strlen ( $needle ) ) === $needle; |
15
|
|
|
} |
16
|
|
|
|
17
|
6 |
|
public static function endswith($hay, $needle) { |
18
|
6 |
|
return \substr ( $hay, - strlen ( $needle ) ) === $needle; |
19
|
|
|
} |
20
|
|
|
|
21
|
2 |
|
public static function getBooleanStr($value) { |
22
|
2 |
|
return $value ? 'true' : 'false'; |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
public static function isNull($s) { |
26
|
1 |
|
return (! isset ( $s ) || NULL === $s || "" === $s); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public static function isNotNull($s) { |
30
|
1 |
|
return (isset ( $s ) && NULL !== $s && "" !== $s); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public static function isBooleanTrue($s) { |
34
|
1 |
|
return filter_var ( $s, FILTER_VALIDATE_BOOLEAN ) === true; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public static function isBooleanFalse($s) { |
38
|
1 |
|
return filter_var ( $s, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) === false; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public static function isBoolean($value) { |
42
|
1 |
|
return \is_bool ( $value ); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public static function isBooleanStr($value) { |
46
|
2 |
|
return filter_var ( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) !== NULL; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Pluralize an expression |
51
|
|
|
* |
52
|
|
|
* @param int $count |
53
|
|
|
* the count of elements |
54
|
|
|
* @param string $zero |
55
|
|
|
* value to return if count==0, can contains {count} mask |
56
|
|
|
* @param string $one |
57
|
|
|
* value to return if count==1, can contains {count} mask |
58
|
|
|
* @param string $other |
59
|
|
|
* value to return if count>1, can contains {count} mask |
60
|
|
|
* @return string the pluralized expression |
61
|
|
|
*/ |
62
|
1 |
|
public static function pluralize($count, $zero, $one, $other) { |
63
|
1 |
|
$result = $other; |
64
|
1 |
|
if ($count === 0) { |
65
|
1 |
|
$result = $zero; |
66
|
1 |
|
} elseif ($count === 1) { |
67
|
1 |
|
$result = $one; |
68
|
|
|
} |
69
|
1 |
|
return \str_replace ( '{count}', $count, $result ); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public static function firstReplace($haystack, $needle, $replace) { |
73
|
2 |
|
$newstring = $haystack; |
74
|
2 |
|
$pos = strpos ( $haystack, $needle ); |
75
|
2 |
|
if ($pos !== false) { |
76
|
2 |
|
$newstring = \substr_replace ( $haystack, $replace, $pos, strlen ( $needle ) ); |
77
|
|
|
} |
78
|
2 |
|
return $newstring; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public static function replaceFirstOccurrence($pattern, $replacement, $subject) { |
82
|
1 |
|
$pattern = '/' . preg_quote ( $pattern, '/' ) . '/'; |
83
|
1 |
|
return preg_replace ( $pattern, $replacement, $subject, 1 ); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public static function replaceArray($haystack, $needleArray, $replace) { |
87
|
1 |
|
$result = $haystack; |
88
|
1 |
|
foreach ( $needleArray as $needle ) { |
89
|
1 |
|
$result = self::firstReplace ( $result, $needle, $replace ); |
90
|
|
|
} |
91
|
1 |
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
public static function doubleBackSlashes($value) { |
95
|
2 |
|
if (is_string ( $value )) |
96
|
2 |
|
return str_replace ( "\\", "\\\\", $value ); |
97
|
1 |
|
return $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public static function cleanAttribute($attr, $replacement = "-") { |
101
|
1 |
|
$attr = preg_replace ( '/[^a-zA-Z0-9\-]/s', $replacement, $attr ); |
102
|
1 |
|
while ( $attr !== ($attr = \str_replace ( $replacement . $replacement, $replacement, $attr )) ) |
103
|
|
|
; |
104
|
1 |
|
return $attr; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public static function mask($secretString, $maskChar = "*") { |
108
|
1 |
|
return str_repeat ( $maskChar, strlen ( $secretString ) ); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
public static function isValid($value) { |
112
|
2 |
|
return is_scalar ( $value ) || (\is_object ( $value ) && method_exists ( $value, '__toString' )); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Converts a value to a string |
117
|
|
|
* |
118
|
|
|
* @param mixed $value |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
1 |
|
public static function toString($value) { |
122
|
1 |
|
if (self::isValid ( $value )) { |
123
|
1 |
|
return $value . ''; |
124
|
|
|
} |
125
|
1 |
|
return ''; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|