|
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\Argument\Helper; |
|
13
|
|
|
|
|
14
|
|
|
use Transliterator; |
|
15
|
|
|
use WBW\Library\Core\Argument\Exception\StringArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* String helper. |
|
19
|
|
|
* |
|
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
21
|
|
|
* @package WBW\Library\Core\Argument\Helper |
|
22
|
|
|
*/ |
|
23
|
|
|
class StringHelper { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create a DOM node. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $name The name. |
|
29
|
|
|
* @param string $value The value. |
|
30
|
|
|
* @param array $attributes The attributes. |
|
31
|
|
|
* @return string Returns the DOM node. |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function domNode($name, $value, array $attributes = []) { |
|
34
|
|
|
|
|
35
|
|
|
$template = "<%name%%attributes%>%text%</%name%>"; |
|
36
|
|
|
|
|
37
|
|
|
$attr = trim(StringHelper::parseArray($attributes)); |
|
38
|
|
|
if (0 < strlen($attr)) { |
|
39
|
|
|
$attr = " " . $attr; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$text = null !== $value ? trim($value, " ") : ""; |
|
43
|
|
|
|
|
44
|
|
|
return str_replace(["%name%", "%attributes%", "%text%"], [trim($name), $attr, $text], $template); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Determines if a value is a string. |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $value The value. |
|
51
|
|
|
* @return bool Returns true. |
|
52
|
|
|
* @throws StringArgumentException Throws a String argument exception if the value is not of expected type. |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function isString($value) { |
|
55
|
|
|
if (false === is_string($value)) { |
|
56
|
|
|
throw new StringArgumentException($value); |
|
57
|
|
|
} |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Parse an array. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $values The array. |
|
65
|
|
|
* @return string Returns the array converted into key="value". |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function parseArray(array $values) { |
|
68
|
|
|
|
|
69
|
|
|
$output = []; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($values as $key => $value) { |
|
72
|
|
|
|
|
73
|
|
|
if (null === $value) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (true === is_array($value)) { |
|
78
|
|
|
$buffer = trim(implode(" ", $value)); |
|
79
|
|
|
} else { |
|
80
|
|
|
$buffer = trim($value); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ("" !== $buffer) { |
|
84
|
|
|
$output[] = $key . "=\"" . preg_replace("/\s+/", " ", $buffer) . "\""; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return implode(" ", $output); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Parse a boolean. |
|
93
|
|
|
* |
|
94
|
|
|
* @param bool $value The boolean value. |
|
95
|
|
|
* @return string Returns "true" in case of success, "false" otherwise. |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function parseBoolean($value) { |
|
98
|
|
|
return true === $value ? "true" : "false"; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Remove accents. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $str The string. |
|
105
|
|
|
* @return string Returns the string without accents. |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function removeAccents($str) { |
|
108
|
|
|
return Transliterator::create("NFD; [:Nonspacing Mark:] Remove; NFC;")->transliterate($str); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|