-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathInsertArray.php
More file actions
98 lines (87 loc) · 2.67 KB
/
InsertArray.php
File metadata and controls
98 lines (87 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace DrupalCodeBuilder\Utility;
// Verbatim copy of latest code from
// https://www.drupal.org/project/drupal/issues/66183
/**
* Provides helpers to insert values into arrays.
*
* @ingroup utility
*/
class InsertArray {
/**
* Inserts values into an associative array before a given key.
*
* Values from $insert_array are inserted into $array before $key.
*
* @param array $array
* The array to insert into, passed by reference and altered in place.
* @param mixed $key
* The key of $array to insert before.
* @param array $insert_array
* An array whose keys and values should be inserted.
*
* @throws \Exception
* Throws an exception if the array does not have the key $key.
*/
public static function insertBefore(&$array, $key, $insert_array) {
static::insert($array, $key, $insert_array, TRUE);
}
/**
* Inserts values into an associative array after a given key.
*
* Values from $insert_array are inserted into $array after $key.
*
* @param array $array
* The array to insert into, passed by reference and altered in place.
* @param mixed $key
* The key of $array to insert after.
* @param array $insert_array
* An array whose keys and values should be inserted.
*
* @throws \Exception
* Throws an exception if the array does not have the key $key.
*/
public static function insertAfter(&$array, $key, $insert_array) {
static::insert($array, $key, $insert_array, FALSE);
}
/**
* Inserts values into an array before or after a given key.
*
* Helper for insertBefore() and insertAfter().
*
* Values from $insert_array are inserted into $array either before or after
* $key.
*
* @param array $array
* The array to insert into, passed by reference and altered in place.
* @param mixed $key
* The key of $array to insert before or after.
* @param array $insert_array
* An array whose values should be inserted.
* @param bool $before
* If TRUE, insert before the given key; if FALSE, insert after it.
*
* @throws \Exception
* Throws an exception if the array does not have the key $key.
*/
protected static function insert(&$array, $key, $insert_array, $before) {
if (!isset($array[$key])) {
throw new \Exception("The array does not have the key $key.");
}
if ($before) {
$offset = 0;
}
else {
$offset = 1;
}
$pos = array_search($key, array_keys($array));
$pos += $offset;
$array_before = array_slice($array, 0, $pos);
$array_after = array_slice($array, $pos);
$array = array_merge(
$array_before,
$insert_array,
$array_after
);
}
}