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