Completed
Push — master ( 7942ad...1c8947 )
by WEBEWEB
01:50
created

StringUtility::parseArray()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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