Completed
Push — master ( 8f5bc0...179bc9 )
by WEBEWEB
03:06
created

StringHelper::parseArray()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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