Completed
Push — master ( a18c65...35d03c )
by WEBEWEB
04:31
created

StringUtility   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parseArray() 0 29 5
A parseBoolean() 0 3 2
A replace() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Utility;
13
14
/**
15
 * String utility.
16
 *
17
 * @author NdC/WBW <https://github.com/webeweb/>
18
 * @package WBW\Library\Core\Utility
19
 * @final
20
 */
21
final class StringUtility {
22
23
	/**
24
	 * Parse an array.
25
	 *
26
	 * @param array $values The array.
27
	 * @return string Returns the array converted into key="value".
28
	 */
29
	public static function parseArray(array $values) {
30
31
		// Initialize the output.
32
		$output = [];
33
34
		// Handle each value.
35
		foreach ($values as $key => $value) {
36
37
			// Check if the value is null.
38
			if (null === $value) {
39
				continue;
40
			}
41
42
			// Check if the value is an array.
43
			if (true === is_array($value)) {
44
				$buffer = trim(implode(" ", $value));
45
			} else {
46
				$buffer = trim($value);
47
			}
48
49
			// Check if the buffer is not empty.
50
			if ("" !== $buffer) {
51
				$output[] = $key . "=\"" . preg_replace("/\s+/", " ", $buffer) . "\"";
52
			}
53
		}
54
55
		// Concatenates all attributes.
56
		return implode(" ", $output);
57
	}
58
59
	/**
60
	 * Parse a boolean.
61
	 *
62
	 * @param boolean $value The boolean value.
63
	 * @return string Returns "true" in case of success, "false" otherwise.
64
	 */
65
	public static function parseBoolean($value) {
66
		return true === $value ? "true" : "false";
67
	}
68
69
	/**
70
	 * Replace.
71
	 *
72
	 * @param string $subject The subject.
73
	 * @param array $searches The searches.
74
	 * @param array $replaces The replaces.
75
	 * @return string Returns the replaced string.
76
	 */
77
	public static function replace($subject, array $searches, array $replaces) {
78
		return str_replace($searches, $replaces, $subject);
79
	}
80
81
}
82