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