Passed
Push — master ( 282e34...da194b )
by Jean-Christophe
06:14
created

UString   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 96.61%

Importance

Changes 0
Metric Value
wmc 34
eloc 40
dl 0
loc 130
ccs 57
cts 59
cp 0.9661
rs 9.68
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A startswith() 0 2 1
A isBooleanTrue() 0 2 1
A isNotNull() 0 2 3
A isNull() 0 2 3
A isBooleanFalse() 0 2 1
A getBooleanStr() 0 2 2
A contains() 0 2 1
A endswith() 0 2 1
A toString() 0 5 2
A doubleBackSlashes() 0 4 2
A isBooleanStr() 0 2 1
A cleanAttribute() 0 5 2
A mask() 0 2 1
A replaceFirstOccurrence() 0 3 1
A isBoolean() 0 2 1
A isValid() 0 2 3
A pluralize() 0 8 3
A replaceArray() 0 6 2
A firstReplace() 0 7 2
A explode() 0 2 1
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 46
	public static function startswith($hay, $needle) {
18 46
		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 29
	public static function endswith($hay, $needle) {
26 29
		return \substr ( $hay, - strlen ( $needle ) ) === $needle;
27
	}
28
29 22
	public static function getBooleanStr($value) {
30 22
		return $value ? 'true' : 'false';
31
	}
32
33 5
	public static function isNull($s) {
34 5
		return (! isset ( $s ) || NULL === $s || "" === $s);
35
	}
36
37 25
	public static function isNotNull($s) {
38 25
		return (isset ( $s ) && NULL !== $s && "" !== $s);
39
	}
40
41 4
	public static function isBooleanTrue($s) {
42 4
		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 26
	public static function isBooleanStr($value) {
54 26
		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 25
	public static function doubleBackSlashes($value) {
103 25
		if (is_string ( $value ))
104 25
			return str_replace ( "\\", "\\\\", $value );
105 1
		return $value;
106
	}
107
108 6
	public static function cleanAttribute($attr, $replacement = "-") {
109 6
		$attr = preg_replace ( '/[^a-zA-Z0-9\-]/s', $replacement, $attr );
110 6
		while ( $attr !== ($attr = \str_replace ( $replacement . $replacement, $replacement, $attr )) )
111
			;
112 6
		return $attr;
113
	}
114
115 1
	public static function mask($secretString, $maskChar = "*") {
116 1
		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
	/**
124
	 * Converts a value to a string
125
	 *
126
	 * @param mixed $value
127
	 * @return string
128
	 */
129 1
	public static function toString($value) {
130 1
		if (self::isValid ( $value )) {
131 1
			return $value . '';
132
		}
133 1
		return '';
134
	}
135
136
	/**
137
	 * Explodes a string with an array of delimiters
138
	 *
139
	 * @param array $delimiters
140
	 * @param string $string
141
	 * @return array
142
	 */
143
	public static function explode($delimiters, $string) {
144
		return explode ( $delimiters [0], str_replace ( $delimiters, $delimiters [0], $string ) );
145
	}
146
}
147
148