Passed
Push — master ( 7d2b7a...0ea348 )
by Jean-Christophe
02:57
created

UString::isBoolean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 2
	public static function startswith($hay, $needle) {
14 2
		return \substr ( $hay, 0, strlen ( $needle ) ) === $needle;
15
	}
16
17 2
	public static function endswith($hay, $needle) {
18 2
		return \substr ( $hay, - strlen ( $needle ) ) === $needle;
19
	}
20
21
	public static function getBooleanStr($value) {
22
		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
	public static function isBooleanTrue($s) {
34
		return filter_var ( $s, FILTER_VALIDATE_BOOLEAN ) === true;
35
	}
36
37
	public static function isBooleanFalse($s) {
38
		return filter_var ( $s, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) === false;
39
	}
40
41
	public static function isBoolean($value) {
42
		return \is_bool ( $value );
43
	}
44
45
	public static function isBooleanStr($value) {
46
		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
	public static function pluralize($count, $zero, $one, $other) {
63
		$result = $other;
64
		if ($count === 0) {
65
			$result = $zero;
66
		} elseif ($count === 1) {
67
			$result = $one;
68
		}
69
		return \str_replace ( '{count}', $count, $result );
70
	}
71
72
	public static function firstReplace($haystack, $needle, $replace) {
73
		$newstring = $haystack;
74
		$pos = strpos ( $haystack, $needle );
75
		if ($pos !== false) {
76
			$newstring = \substr_replace ( $haystack, $replace, $pos, strlen ( $needle ) );
77
		}
78
		return $newstring;
79
	}
80
81
	public static function replaceFirstOccurrence($pattern, $replacement, $subject) {
82
		$pattern = '/' . preg_quote ( $pattern, '/' ) . '/';
83
		return preg_replace ( $pattern, $replacement, $subject, 1 );
84
	}
85
86
	public static function replaceArray($haystack, $needleArray, $replace) {
87
		$result = $haystack;
88
		foreach ( $needleArray as $needle ) {
89
			$result = self::firstReplace ( $result, $needle, $replace );
90
		}
91
		return $result;
92
	}
93
94
	public static function doubleBackSlashes($value) {
95
		if (is_string ( $value ))
96
			return str_replace ( "\\", "\\\\", $value );
97
		return $value;
98
	}
99
100
	public static function cleanAttribute($attr, $replacement = "-") {
101
		$attr = preg_replace ( '/[^a-zA-Z0-9\-]/s', $replacement, $attr );
102
		while ( $attr !== ($attr = \str_replace ( $replacement . $replacement, $replacement, $attr )) )
103
			;
104
		return $attr;
105
	}
106
107
	public static function mask($secretString, $maskChar = "*") {
108
		return str_repeat ( $maskChar, strlen ( $secretString ) );
109
	}
110
111
	public static function isValid($value) {
112
		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
	public static function toString($value) {
122
		if (self::isValid ( $value )) {
123
			return $value . '';
124
		}
125
		return '';
126
	}
127
}
128
129