Completed
Push — master ( 880e77...a3efcb )
by Andrii
05:38
created

ArrayHelper::insertLast()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 3
eloc 6
nc 4
nop 2
1
<?php
2
3
/*
4
 * Collection library for PHP
5
 *
6
 * @link      https://github.com/hiqdev/php-collection
7
 * @package   php-collection
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\php\collection;
13
14
class ArrayHelper
15
{
16
    /**
17
     * Recursive safe merge.
18
     * Based on Yii2 yii\helpers\BaseArrayHelper::merge.
19
     *
20
     * Merges two or more arrays into one recursively.
21
     * If each array has an element with the same string key value, the latter
22
     * will overwrite the former (different from array_merge_recursive).
23
     * Recursive merging will be conducted if both arrays have an element of array
24
     * type and are having the same key.
25
     * For integer-keyed elements, the elements from the latter array will                    
26
     * be appended to the former array.
27
     *
28
     * @param array $a array to be merged to
29
     * @param array $b array to be merged from
30
     *
31
     * @return array the merged array
32
     */
33
    public static function merge($a, $b)
2 ignored issues
show
Unused Code introduced by
The parameter $a is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $b is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        $args = func_get_args();
36
        $res = array_shift($args);
37
        foreach ($args as $items) {
38
            if (!is_array($items)) {
39
                continue;
40
            }
41
            foreach ($items as $k => $v) {
42
                if (is_int($k)) {
43
                    if (isset($res[$k])) {
44
                        $res[] = $v;
45
                    } else {
46
                        $res[$k] = $v;
47
                    }
48
                } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
49
                    $res[$k] = self::merge($res[$k], $v);
50
                } else {
51
                    $res[$k] = $v;
52
                }
53
            }
54
        }
55
56
        return $res;
57
    }
58
59
    /**
60
     * Get specified items as array.
61
     *
62
     * @param mixed $keys specification
63
     *
64
     * @return array
65
     */
66
    public static function getItems($array, $keys = null)
67
    {
68
        if (is_null($keys)) {
69
            return $array;
70
        } elseif (is_scalar($keys)) {
71
            $keys = [$keys => $array[$keys]];
72
        }
73
        $res = [];
74
        foreach ($keys as $k) {
75
            if (array_key_exists($k, $array)) {
76
                $res[$k] = $array[$k];
77
            }
78
        }
79
        return $res;
80
    }
81
    /**
82
     * Inserts items in front of array.
83
     * rough method: unset and then set, think of better.
84
     */
85
    public static function insertLast(array $array, array $items)
86
    {
87
        foreach ($items as $k => $v) {
88
            unset($array[$k]);
89
        }
90
        foreach ($items as $k => $v) {
91
            $array[$k] = $v;
92
        }
93
94
        return $array;
95
    }
96
97
    /**
98
     * Inserts items in front of array.
99
     * rough method: unset and then set, think of better.
100
     */
101
    public static function insertFirst(array $array, array $items)
102
    {
103
        foreach ($items as $k => $v) {
104
            unset($array[$k]);
105
        }
106
        $array = array_merge($items, $array);
107
108
        return $array;
109
    }
110
111
    /**
112
     * Inserts items inside of array.
113
     * rough method: unset and then set, think of better.
114
     *
115
     * @param array        $array source array
116
     * @param array        $items array of items.
117
     * @param string|array $where where to insert
118
     *
119
     * @return array new items list
120
     *
121
     * @see add()
122
     */
123
    public static function insertInside(array $array, $items, $where)
124
    {
125
        foreach ($items as $k => $v) {
126
            unset($array[$k]);
127
        }
128
        $before = self::prepareWhere($array, $where['before']);
129
        $after  = self::prepareWhere($array, $where['after']);
130
        $new    = [];
131
        $found  = false;
132
        /// TODO think of realizing it better
133
        foreach ($array as $k => $v) {
134 View Code Duplication
            if (!$found && $k === $before) {
1 ignored issue
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...
135
                foreach ($items as $i => $c) {
136
                    $new[$i] = $c;
137
                }
138
                $found = true;
139
            }
140
            $new[$k] = $v;
141 View Code Duplication
            if (!$found && $k === $after) {
1 ignored issue
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...
142
                foreach ($items as $i => $c) {
143
                    $new[$i] = $c;
144
                }
145
                $found = true;
146
            }
147
        }
148
        if (!$found) {
149
            foreach ($items as $i => $c) {
150
                $new[$i] = $c;
151
            }
152
        }
153
154
        return $new;
155
    }
156
157
    /**
158
     * Internal function to prepare where list for insertInside.
159
     *
160
     * @param array        $array source array
161
     * @param array|string $list  array to convert
162
     *
163
     * @return array
164
     */
165
    protected static function prepareWhere(array $array, $list)
166
    {
167
        if (!is_array($list)) {
168
            $list = [$list];
169
        }
170
        foreach ($list as $v) {
171
            if (array_key_exists($v, $array)) {
172
                return $v;
173
            }
174
        }
175
176
        return null;
177
    }
178
179
    /**
180
     * Recursively removes duplicate values from non-associative arrays.
181
     */
182
    public static function unique($array)
183
    {
184
        $suitable = true;
185
        foreach ($array as $k => &$v) {
186
            if (is_array($v)) {
187
                $v = self::unique($v);
188
            } elseif (!is_int($k)) {
189
                $suitable = false;
190
            }
191
        }
192
193
        return $suitable ? self::uniqueFlat($array) : $array;
194
    }
195
196
    /**
197
     * Non-recursively removes duplicate values from non-associative arrays.
198
     */
199
    public static function uniqueFlat($array)
200
    {
201
        $uniqs = [];
202
        $res = [];
203
        foreach ($array as $k => $v) {
204
            $uv = var_export($v, true);
205
            if (array_key_exists($uv, $uniqs)) {
206
                continue;
207
            }
208
            $uniqs[$uv] = 1;
209
            $res[$k] = $v;
210
        }
211
212
        return $res;
213
    }
214
215
    public static function toArray($object)
216
    {
217
        $res = (array) $object;
218
        foreach ($res as $k => &$v) {
219
            if (is_object($v)) {
220
                $v = self::toArray($v);
221
            }
222
        }
223
224
        return $res;
225
    }
226
}
227