|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
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\Helper\Argument; |
|
13
|
|
|
|
|
14
|
|
|
use Transliterator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* String helper. |
|
18
|
|
|
* |
|
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
20
|
|
|
* @package WBW\Library\Core\Helper\Argument |
|
21
|
|
|
*/ |
|
22
|
|
|
class StringHelper { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Parse an array. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $values The array. |
|
28
|
|
|
* @return string Returns the array converted into key="value". |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function parseArray(array $values) { |
|
31
|
|
|
|
|
32
|
|
|
// Initialize the output. |
|
33
|
|
|
$output = []; |
|
34
|
|
|
|
|
35
|
|
|
// Handle each value. |
|
36
|
|
|
foreach ($values as $key => $value) { |
|
37
|
|
|
|
|
38
|
|
|
// Check if the value is null. |
|
39
|
|
|
if (null === $value) { |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Check if the value is an array. |
|
44
|
|
|
if (true === is_array($value)) { |
|
45
|
|
|
$buffer = trim(implode(" ", $value)); |
|
46
|
|
|
} else { |
|
47
|
|
|
$buffer = trim($value); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Check if the buffer is not empty. |
|
51
|
|
|
if ("" !== $buffer) { |
|
52
|
|
|
$output[] = $key . "=\"" . preg_replace("/\s+/", " ", $buffer) . "\""; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Concatenates all attributes. |
|
57
|
|
|
return implode(" ", $output); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Parse a boolean. |
|
62
|
|
|
* |
|
63
|
|
|
* @param boolean $value The boolean value. |
|
64
|
|
|
* @return string Returns "true" in case of success, "false" otherwise. |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function parseBoolean($value) { |
|
67
|
|
|
return true === $value ? "true" : "false"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Replace. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $subject The subject. |
|
74
|
|
|
* @param array $searches The searches. |
|
75
|
|
|
* @param array $replaces The replaces. |
|
76
|
|
|
* @return string Returns the replaced string. |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function replace($subject, array $searches, array $replaces) { |
|
79
|
|
|
return str_replace($searches, $replaces, $subject); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Remove accents. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $str The string. |
|
86
|
|
|
* @return string Returns the string without accents. |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function removeAccents($str) { |
|
89
|
|
|
return Transliterator::create("NFD; [:Nonspacing Mark:] Remove; NFC;")->transliterate($str); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|