Completed
Push — master ( 318d4f...769e58 )
by Jean-Christophe
04:58
created

UString::isNull()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 1
nc 3
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 18
 *
14 18
 */
15
class UString {
16
17 18
	public static function startswith($hay, $needle) {
18 18
		return \substr ( $hay, 0, strlen ( $needle ) ) === $needle;
19
	}
20
21 5
	public static function contains($needle, $haystack) {
22 5
		return strpos ( $haystack, $needle ) !== false;
23
	}
24
25 5
	public static function endswith($hay, $needle) {
26 5
		return \substr ( $hay, - strlen ( $needle ) ) === $needle;
27
	}
28
29 10
	public static function getBooleanStr($value) {
30 10
		return $value ? 'true' : 'false';
31
	}
32
33 3
	public static function isNull($s) {
34 3
		return (! isset ( $s ) || NULL === $s || "" === $s);
35
	}
36
37 1
	public static function isNotNull($s) {
38 1
		return (isset ( $s ) && NULL !== $s && "" !== $s);
39
	}
40
41 1
	public static function isBooleanTrue($s) {
42 1
		return filter_var ( $s, FILTER_VALIDATE_BOOLEAN ) === true;
43
	}
44
45 5
	public static function isBooleanFalse($s) {
46 5
		return filter_var ( $s, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) === false;
47
	}
48
49
	public static function isBoolean($value) {
50
		return \is_bool ( $value );
51
	}
52
53
	public static function isBooleanStr($value) {
54
		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 1
	 * @param string $zero
63 1
	 *        	value to return if count==0, can contains {count} mask
64 1
	 * @param string $one
65 1
	 *        	value to return if count==1, can contains {count} mask
66 1
	 * @param string $other
67 1
	 *        	value to return if count>1, can contains {count} mask
68
	 * @return string the pluralized expression
69 1
	 */
70
	public static function pluralize($count, $zero, $one, $other) {
71
		$result = $other;
72 2
		if ($count === 0) {
73 2
			$result = $zero;
74 2
		} elseif ($count === 1) {
75 2
			$result = $one;
76 2
		}
77
		return \str_replace ( '{count}', $count, $result );
78 2
	}
79
80
	public static function firstReplace($haystack, $needle, $replace) {
81 1
		$newstring = $haystack;
82 1
		$pos = strpos ( $haystack, $needle );
83 1
		if ($pos !== false) {
84
			$newstring = \substr_replace ( $haystack, $replace, $pos, strlen ( $needle ) );
85
		}
86 1
		return $newstring;
87 1
	}
88 1
89 1
	public static function replaceFirstOccurrence($pattern, $replacement, $subject) {
90
		$pattern = '/' . preg_quote ( $pattern, '/' ) . '/';
91 1
		return preg_replace ( $pattern, $replacement, $subject, 1 );
92
	}
93
94 5
	public static function replaceArray($haystack, $needleArray, $replace) {
95 5
		$result = $haystack;
96 5
		foreach ( $needleArray as $needle ) {
97 1
			$result = self::firstReplace ( $result, $needle, $replace );
98
		}
99
		return $result;
100 4
	}
101 4
102 4
	public static function doubleBackSlashes($value) {
103
		if (is_string ( $value ))
104 4
			return str_replace ( "\\", "\\\\", $value );
105
		return $value;
106
	}
107 1
108 1
	public static function cleanAttribute($attr, $replacement = "-") {
109
		$attr = preg_replace ( '/[^a-zA-Z0-9\-]/s', $replacement, $attr );
110
		while ( $attr !== ($attr = \str_replace ( $replacement . $replacement, $replacement, $attr )) )
111 2
			;
112 2
		return $attr;
113
	}
114
115
	public static function mask($secretString, $maskChar = "*") {
116
		return str_repeat ( $maskChar, strlen ( $secretString ) );
117
	}
118
119
	public static function isValid($value) {
120
		return is_scalar ( $value ) || (\is_object ( $value ) && method_exists ( $value, '__toString' ));
121 1
	}
122 1
123 1
	/**
124
	 * Converts a value to a string
125 1
	 *
126
	 * @param mixed $value
127
	 * @return string
128
	 */
129
	public static function toString($value) {
130
		if (self::isValid ( $value )) {
131
			return $value . '';
132
		}
133
		return '';
134
	}
135
}
136
137