Completed
Push — master ( 33cdc3...038af3 )
by Lorenzo
01:59
created

array.php ➔ str_replace_array()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 7
loc 7
rs 9.4285
1
<?php
2
3 View Code Duplication
if (!function_exists('get')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
4
    /**
5
     * Get an item from an array using "dot" notation.
6
     *
7
     * @param  array $array
8
     * @param  string $key
9
     * @param  mixed $default
10
     * @return mixed
11
     */
12
    function get($array, $key, $default = null)
13
    {
14
        if (is_null($key)) {
15
            return $array;
16
        }
17
        if (isset($array[$key])) {
18
            return $array[$key];
19
        }
20
        foreach (explode('.', $key) as $segment) {
21
            if (!is_array($array) || !array_key_exists($segment, $array)) {
22
                return value($default);
23
            }
24
            $array = $array[$segment];
25
        }
26
        return $array;
27
    }
28
}
29
30
if (!function_exists('set')) {
31
    /**
32
     * Set an array item to a given value using "dot" notation.
33
     *
34
     * If no key is given to the method, the entire array will be replaced.
35
     *
36
     * @param  array $array
37
     * @param  string $key
38
     * @param  mixed $value
39
     * @return array
40
     */
41
    function set(&$array, $key, $value)
42
    {
43
        if (is_null($key)) {
44
            return $array = $value;
45
        }
46
        $keys = explode('.', $key);
47
        while (count($keys) > 1) {
48
            $key = array_shift($keys);
49
            // If the key doesn't exist at this depth, we will just create an empty array
50
            // to hold the next value, allowing us to create the arrays to hold final
51
            // values at the correct depth. Then we'll keep digging into the array.
52
            if (!isset($array[$key]) || !is_array($array[$key])) {
53
                $array[$key] = array();
54
            }
55
            $array =& $array[$key];
56
        }
57
        $array[array_shift($keys)] = $value;
58
        return $array;
59
    }
60
}
61
62
if (!function_exists('head')) {
63
    /**
64
     * Get the first element of an array. Useful for method chaining.
65
     *
66
     * @param  array $array
67
     * @return mixed
68
     */
69
    function head($array)
70
    {
71
        return reset($array);
72
    }
73
}
74
75
if (!function_exists('last')) {
76
    /**
77
     * Get the last element from an array.
78
     *
79
     * @param  array $array
80
     * @return mixed
81
     */
82
    function last($array)
83
    {
84
        return end($array);
85
    }
86
}
87
88
if (!function_exists('array_has')) {
89
    /**
90
     * Check if an item exists in an array using "dot" notation.
91
     *
92
     * @param  array $array
93
     * @param  string $key
94
     * @return bool
95
     */
96
    function array_has($array, $key)
97
    {
98
        if (empty($array) || is_null($key)) {
99
            return false;
100
        }
101
        if (array_key_exists($key, $array)) {
102
            return true;
103
        }
104
        foreach (explode('.', $key) as $segment) {
105
            if (!is_array($array) || !array_key_exists($segment, $array)) {
106
                return false;
107
            }
108
            $array = $array[$segment];
109
        }
110
        return true;
111
    }
112
}
113
114 View Code Duplication
if (!function_exists('array_get')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    /**
116
     * Get an item from an array using "dot" notation.
117
     *
118
     * @param  array $array
119
     * @param  string $key
120
     * @param  mixed $default
121
     * @return mixed
122
     */
123
    function array_get($array, $key, $default = null)
124
    {
125
        if (is_null($key)) {
126
            return $array;
127
        }
128
        if (isset($array[$key])) {
129
            return $array[$key];
130
        }
131
        foreach (explode('.', $key) as $segment) {
132
            if (!is_array($array) || !array_key_exists($segment, $array)) {
133
                return value($default);
134
            }
135
            $array = $array[$segment];
136
        }
137
        return $array;
138
    }
139
}
140
141
/**
142
 * Return an array with only integers value contained in the array passed
143
 * @param array $array
144
 * @return array
145
 **/
146
function CleanUpArrayOfInt($array)
147
{
148
    $result = array();
149
    if (!is_array($array) || count($array) < 1) {
150
        return $result;
151
    }
152
    reset($array);
153
    while (list($key, $value) = each($array)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
154
        if (isInteger($value)) {
155
            $result[] = $value;
156
        }
157
    }
158
    reset($array);
159
160
    return $result;
161
}
162