Completed
Branch master (6af305)
by Andrii
02:24
created

ArrayHelper::insertLast()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 3
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-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\php\collection;
13
14
/**
15
 * Array Helper.
16
 *
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class ArrayHelper
20
{
21
    /**
22
     * Recursive safe merge.
23
     * Based on Yii2 yii\helpers\BaseArrayHelper::merge.
24
     *
25
     * Merges two or more arrays into one recursively.
26
     * If each array has an element with the same string key value, the latter
27
     * will overwrite the former (different from array_merge_recursive).
28
     * Recursive merging will be conducted if both arrays have an element of array
29
     * type and are having the same key.
30
     * For integer-keyed elements, the elements from the latter array will
31
     * be appended to the former array.
32
     *
33
     * @param array $a array to be merged to
34
     * @param array $b array to be merged from
35
     * @return array the merged array
36
     */
37
    public static function merge($a, $b)
38
    {
39
        $args = func_get_args();
40
        $res = array_shift($args);
41
        foreach ($args as $items) {
42
            if (!is_array($items)) {
43
                continue;
44
            }
45
            foreach ($items as $k => $v) {
46
                if (is_int($k)) {
47
                    if (isset($res[$k])) {
48
                        $res[] = $v;
49
                    } else {
50
                        $res[$k] = $v;
51
                    }
52
                } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
53
                    $res[$k] = self::merge($res[$k], $v);
54
                } else {
55
                    $res[$k] = $v;
56
                }
57
            }
58
        }
59
60
        return $res;
61
    }
62
63
    /**
64
     * Get specified items as array.
65
     * @param mixed $keys specification
66
     * @return array
67
     */
68 2
    public static function getItems($array, $keys = null)
69
    {
70
        if (is_null($keys)) {
71 2
            return $array;
72
        } elseif (is_scalar($keys)) {
73
            $keys = [$keys => $array[$keys]];
74
        }
75
        $res = [];
76
        foreach ($keys as $k) {
77
            if (array_key_exists($k, $array)) {
78
                $res[$k] = $array[$k];
79
            }
80
        }
81
        return $res;
82
    }
83
    /**
84
     * Inserts items in front of array.
85
     * rough method: unset and then set, think of better.
86
     */
87 2
    public static function insertLast(array $array, array $items)
88
    {
89
        foreach ($items as $k => $v) {
90
            unset($array[$k]);
91
        }
92
        foreach ($items as $k => $v) {
93 2
            $array[$k] = $v;
94
        }
95
96 2
        return $array;
97 2
    }
98
99
    /**
100
     * Inserts items in front of array.
101
     * rough method: unset and then set, think of better.
102
     */
103 2
    public static function insertFirst(array $array, array $items)
104
    {
105 2
        foreach (array_keys($items) as $k) {
106
            unset($array[$k]);
107
        }
108
        $array = array_merge($items, $array);
109
110 2
        return $array;
111 2
    }
112
113
    /**
114
     * Inserts items inside of array.
115
     * rough method: unset and then set, think of better.
116
     *
117
     * @param array        $array source array
118
     * @param array        $items array of items.
119
     * @param string|array $where where to insert
120
     * @return array new items list
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, isset($where['before']) ? $where['before'] : null);
129
        $after  = self::prepareWhere($array, isset($where['after'])  ? $where['after']  : null);
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) {
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) {
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
     * @return array
163
     */
164
    protected static function prepareWhere(array $array, $list)
165
    {
166
        if (!is_array($list)) {
167
            $list = [$list];
168
        }
169
        foreach ($list as $v) {
170
            if (array_key_exists($v, $array)) {
171
                return $v;
172
            }
173
        }
174
175
        return null;
176
    }
177
178
    /**
179
     * Recursively removes duplicate values from non-associative arrays.
180
     */
181
    public static function unique($array)
182
    {
183
        $suitable = true;
184
        foreach ($array as $k => &$v) {
185
            if (is_array($v)) {
186
                $v = self::unique($v);
187
            } elseif (!is_int($k)) {
188
                $suitable = false;
189
            }
190
        }
191
192
        return $suitable ? self::uniqueFlat($array) : $array;
193
    }
194
195
    /**
196
     * Non-recursively removes duplicate values from non-associative arrays.
197
     */
198
    public static function uniqueFlat($array)
199
    {
200
        $uniqs = [];
201
        $res = [];
202
        foreach ($array as $k => $v) {
203
            $uv = var_export($v, true);
204
            if (array_key_exists($uv, $uniqs)) {
205
                continue;
206
            }
207
            $uniqs[$uv] = 1;
208
            $res[$k] = $v;
209
        }
210
211
        return $res;
212
    }
213
214
    public static function toArray($object)
215
    {
216
        $res = (array) $object;
217
        foreach ($res as &$v) {
218
            if (is_object($v)) {
219
                $v = self::toArray($v);
220
            }
221
        }
222
223
        return $res;
224
    }
225
}
226