Passed
Push — master ( e48b7c...1886e0 )
by Jean-Christophe
25:18 queued 16:06
created

UString::replaceArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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