StringHelper::domNode()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
cc 5
nc 8
nop 4
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|null $value The value.
30
     * @param array $attributes The attributes.
31
     * @param bool $shortTag Short tag ?
32
     * @return string Returns the DOM node.
33
     */
34
    public static function domNode(string $name, ?string $value, array $attributes = [], bool $shortTag = false): string {
35
36
        $template = "<%name%%attributes%>%text%</%name%>";
37
38
        $attr = trim(static::parseArray($attributes));
39
        if (0 < strlen($attr)) {
40
            $attr = " " . $attr;
41
        }
42
43
        $text = null !== $value ? trim($value, " ") : "";
44
        if ("" === $text && true === $shortTag) {
45
            $template = str_replace(">%text%</%name%>", "/>", $template);
46
        }
47
48
        return str_replace(["%name%", "%attributes%", "%text%"], [trim($name), $attr, $text], $template);
49
    }
50
51
    /**
52
     * Determines if a value is a string.
53
     *
54
     * @param mixed $value The value.
55
     * @return void
56
     * @throws StringArgumentException Throws a String argument exception if the value is not of expected type.
57
     */
58
    public static function isString($value): void {
59
        if (false === is_string($value)) {
60
            throw new StringArgumentException($value);
61
        }
62
    }
63
64
    /**
65
     * Parse an array.
66
     *
67
     * @param array $values The array.
68
     * @return string Returns the array converted into key="value".
69
     */
70
    public static function parseArray(array $values): string {
71
72
        $output = [];
73
74
        foreach ($values as $key => $value) {
75
76
            if (null === $value) {
77
                continue;
78
            }
79
80
            if (true === is_array($value)) {
81
                $buffer = trim(implode(" ", $value));
82
            } else {
83
                $buffer = trim($value);
84
            }
85
86
            if ("" !== $buffer) {
87
                $output[] = $key . '="' . preg_replace("/\s+/", " ", $buffer) . '"';
88
            }
89
        }
90
91
        return implode(" ", $output);
92
    }
93
94
    /**
95
     * Parse a boolean.
96
     *
97
     * @param bool|null $value The boolean value.
98
     * @return string Returns "true" in case of success, "false" otherwise.
99
     */
100
    public static function parseBoolean(?bool $value): string {
101
        return true === $value ? "true" : "false";
102
    }
103
104
    /**
105
     * Remove accents.
106
     *
107
     * @param string $str The string.
108
     * @return string Returns the string without accents.
109
     */
110
    public static function removeAccents(string $str): string {
111
        return Transliterator::create("NFD; [:Nonspacing Mark:] Remove; NFC;")->transliterate($str);
112
    }
113
}
114