Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UString.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\base;
4
15class UString {
16
17 public static function startswith($hay, $needle) {
18 return \substr($hay, 0, strlen($needle)) === $needle;
19 }
20
21 public static function contains($needle, $haystack) {
22 return \strpos($haystack, $needle) !== false;
23 }
24
25 public static function containsValues(array $values, string $haystack): bool {
26 foreach ($values as $v) {
27 if (\strpos($haystack, $v) !== false) {
28 return true;
29 }
30 }
31 return false;
32 }
33
34 public static function endswith($hay, $needle) {
35 return \substr($hay, -strlen($needle)) === $needle;
36 }
37
38 public static function getBooleanStr($value) {
39 return ($value === true || $value === 'true' || $value == 1) ? 'true' : 'false';
40 }
41
42 public static function isNull($s) {
43 return (!isset ($s) || null === $s || '' === $s);
44 }
45
46 public static function isNotNull($s) {
47 return (isset ($s) && null !== $s && '' !== $s);
48 }
49
50 public static function isBooleanTrue($s) {
51 return filter_var($s, FILTER_VALIDATE_BOOLEAN) === true;
52 }
53
54 public static function isBooleanFalse($s) {
55 return \filter_var($s, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === false;
56 }
57
58 public static function isBoolean($value) {
59 return \is_bool($value);
60 }
61
62 public static function isBooleanStr($value) {
63 return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null;
64 }
65
75 public static function pluralize($count, $zero, $one, $other) {
76 $result = $other;
77 if ($count === 0) {
78 $result = $zero;
79 } elseif ($count === 1) {
80 $result = $one;
81 }
82 return \str_replace('{count}', $count, $result);
83 }
84
85 public static function firstReplace($haystack, $needle, $replace) {
86 $newstring = $haystack;
87 $pos = \strpos($haystack, $needle);
88 if ($pos !== false) {
89 $newstring = \substr_replace($haystack, $replace, $pos, \strlen($needle));
90 }
91 return $newstring;
92 }
93
94 public static function replaceFirstOccurrence($pattern, $replacement, $subject) {
95 $pattern = '/' . \preg_quote($pattern, '/') . '/';
96 return \preg_replace($pattern, $replacement, $subject, 1);
97 }
98
99 public static function replaceArray($haystack, $needleArray, $replace) {
100 $result = $haystack;
101 foreach ($needleArray as $needle) {
102 $result = self::firstReplace($result, $needle, $replace);
103 }
104 return $result;
105 }
106
107 public static function doubleBackSlashes($value) {
108 if (\is_string($value))
109 return \str_replace("\\", "\\\\", $value);
110 return $value;
111 }
112
113 public static function cleanAttribute($attr, $replacement = "-") {
114 $attr = \preg_replace('/[^a-zA-Z0-9\-]/s', $replacement, $attr);
115 while ($attr !== ($attr = \str_replace($replacement . $replacement, $replacement, $attr)))
116 ;
117 return $attr;
118 }
119
120 public static function mask($secretString, $maskChar = "*") {
121 return \str_repeat($maskChar, \strlen($secretString));
122 }
123
124 public static function isValid($value) {
125 return \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString'));
126 }
127
128 public static function isJson($value) {
129 return \is_object(\json_decode($value));
130 }
131
138 public static function toString($value) {
139 if (self::isValid($value)) {
140 return $value . '';
141 }
142 return '';
143 }
144
152 public static function explode($delimiters, $string) {
153 return \explode($delimiters [0], \str_replace($delimiters, $delimiters [0], $string));
154 }
155
161 public static function isExpression(string $v): bool {
162 $v = \trim($v);
163 return $v == 'nonce' || self::startswith($v, '$') || self::startswith($v, 'function')
164 || self::startswith($v, 'array(') || self::startswith($v, 'getenv(');
165 }
166}
167
String utilities.
Definition UString.php:15
static isBooleanStr($value)
Definition UString.php:62
static mask($secretString, $maskChar="*")
Definition UString.php:120
static startswith($hay, $needle)
Definition UString.php:17
static isBoolean($value)
Definition UString.php:58
static doubleBackSlashes($value)
Definition UString.php:107
static endswith($hay, $needle)
Definition UString.php:34
static cleanAttribute($attr, $replacement="-")
Definition UString.php:113
static contains($needle, $haystack)
Definition UString.php:21
static isExpression(string $v)
Returns true is value is a php expression.
Definition UString.php:161
static explode($delimiters, $string)
Explodes a string with an array of delimiters.
Definition UString.php:152
static replaceArray($haystack, $needleArray, $replace)
Definition UString.php:99
static toString($value)
Converts a value to a string.
Definition UString.php:138
static replaceFirstOccurrence($pattern, $replacement, $subject)
Definition UString.php:94
static pluralize($count, $zero, $one, $other)
Pluralize an expression.
Definition UString.php:75
static getBooleanStr($value)
Definition UString.php:38
static firstReplace($haystack, $needle, $replace)
Definition UString.php:85
static containsValues(array $values, string $haystack)
Definition UString.php:25