Arrayor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 94
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A camelizeIndex() 0 18 2
A implodeRecursive() 0 11 1
A _camelize() 0 13 2
1
<?php
2
namespace Xety\Arrayor;
3
4
class Arrayor
5
{
6
7
    /**
8
     * Camelize all index keys in the first level.
9
     *
10
     * Passed :
11
     * $array = [
12
     *     'Index key' => 1,
13
     *     'key_index' => 2
14
     * ];
15
     *
16
     * Return :
17
     * $array = [
18
     *     'indexKey' => 1,
19
     *     'keyIndex' => 2
20
     * ];
21
     *
22
     * @param array $array The array to be camelized.
23
     * @param string $delimiter The delimiter in the input string.
24
     *
25
     * @return bool|array
26
     */
27
    public static function camelizeIndex($array, $delimiter = '_')
28
    {
29
        if (!is_array($array)) {
30
            return false;
31
        }
32
33
        $array = array_combine(
34
            array_map(
35
                function ($key) use ($delimiter) {
36
                    return lcfirst(static::_camelize($key, $delimiter));
37
                },
38
                array_keys($array)
39
            ),
40
            array_values($array)
41
        );
42
43
        return $array;
44
    }
45
46
    /**
47
     * Implode an array into a string by both key and value.
48
     *
49
     * Passed :
50
     * $array = [
51
     *     'key-index' => 1,
52
     *     'key index' => 'value'
53
     * ];
54
     *
55
     * Return :
56
     * $string = 'key-index : 1 | key index : value';
57
     *
58
     * @param array $array The associative array.
59
     * @param string $glue The glue used for pairs of key/values.
60
     * @param string $separator The glue used to join pairs of key/values.
61
     *
62
     * @return string Imploded associative array
63
     */
64
    public static function implodeRecursive($array = [], $glue = ' : ', $separator = ' | ')
65
    {
66
        $keyArray = array_map(
67
            function ($val) use ($glue) {
68
                return implode($glue, $val);
69
            },
70
            array_map(null, array_keys($array), array_values($array))
71
        );
72
73
        return implode($separator, $keyArray);
74
    }
75
76
    /**
77
     * Returns the input lower_case_delimited_string as a CamelCasedString.
78
     *
79
     * @param string $string String to camelize.
80
     * @param string $delimiter The delimiter in the input string.
81
     *
82
     * @return string CamelizedStringLikeThis.
83
     */
84
    protected static function _camelize($string, $delimiter = '_')
85
    {
86
        $result = explode(' ', str_replace($delimiter, ' ', $string));
87
88
        foreach ($result as &$word) {
89
            $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
90
        }
91
        $result = implode(' ', $result);
92
93
        $result = str_replace(' ', '', $result);
94
95
        return $result;
96
    }
97
}
98