Completed
Push — master ( 44b4d6...c54f73 )
by WEBEWEB
06:15
created

StringHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseArray() 0 29 5
A parseBoolean() 0 3 2
A replace() 0 3 1
A removeAccents() 0 3 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\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