• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 9254340622

27 May 2024 11:53AM UTC coverage: 23.388% (-0.2%) from 23.563%
9254340622

push

github

ericges
backport static utils

0 of 42 new or added lines in 3 files covered. (0.0%)

1320 of 5644 relevant lines covered (23.39%)

1.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/StaticUtil/StaticArrayUtil.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\StaticUtil;
4

5
class StaticArrayUtil extends AbstractStaticUtil
6
{
7
    /**
8
     * Insert a new entry before a specific or multiple keys in array.
9
     * If the keys not exist, the new entry is added to the end of the array.
10
     * Array is passed as reference.
11
     *
12
     * @param array        $array    Array the new entry should inserted to
13
     * @param array|string $keys     The key or keys where the new entry should be added before
14
     * @param string       $newKey   The key of the entry that should be added
15
     * @param mixed        $newValue The value of the entry that should be added
16
     */
17
    public static function insertBeforeKey(array &$array, array|string $keys, string $newKey, mixed $newValue): void
18
    {
NEW
19
        if (!is_array($keys)) {
×
NEW
20
            $keys = [$keys];
×
21
        }
22

NEW
23
        if (array_intersect($keys, array_keys($array))) {
×
NEW
24
            $new = [];
×
25

NEW
26
            foreach ($array as $k => $value) {
×
NEW
27
                if (\in_array($k, $keys)) {
×
NEW
28
                    $new[$newKey] = $newValue;
×
29
                }
NEW
30
                $new[$k] = $value;
×
31
            }
NEW
32
            $array = $new;
×
33
        } else {
NEW
34
            $array[$newKey] = $newValue;
×
35
        }
36
    }
37

38
    /**
39
     * Insert a value into an existing array by key name.
40
     *
41
     * Additional options:
42
     * - (bool) strict: Strict behavior for array search. Default false
43
     * - (bool) attachMissingKey: Attach value to the end of the array if the key does not exist. Default: true
44
     * - (int) offset: Add additional offset.
45
     *
46
     * @param array  $array   The target array
47
     * @param string $key     the existing target key in the array
48
     * @param mixed  $value   the new value to be inserted
49
     * @param array{
50
     *     strict?: bool,
51
     *     attachMissingKey?: bool,
52
     *     offset?: int
53
     * } $options Additional options
54
     */
55
    public static function insertAfterKey(array &$array, string $key, mixed $value, string $newKey = null, array $options = []): void
56
    {
NEW
57
        $options = array_merge([
×
NEW
58
            'strict' => false,
×
NEW
59
            'attachMissingKey' => true,
×
NEW
60
            'offset' => 0,
×
NEW
61
        ], $options);
×
62

NEW
63
        $keys = array_keys($array);
×
NEW
64
        $index = array_search($key, $keys, $options['strict']);
×
65

NEW
66
        if (false === $index && false === $options['attachMissingKey']) {
×
NEW
67
            return;
×
68
        }
NEW
69
        $pos = false === $index ? count($array) : $index + 1;
×
NEW
70
        $pos = $pos + $options['offset'];
×
71

NEW
72
        if ($newKey) {
×
NEW
73
            $value = [$newKey => $value];
×
74
        } else {
NEW
75
            $value = [$value];
×
76
        }
77

NEW
78
        $array = array_combine(
×
NEW
79
            array_merge(array_slice($keys, 0, $pos), array_keys($value), array_slice($keys, $pos)),
×
NEW
80
            array_merge(array_slice($array, 0, $pos), $value, array_slice($array, $pos))
×
NEW
81
        );
×
82
    }
83

84
    /**
85
     * Removes a value from an array.
86
     *
87
     * @return bool Returns true if the value has been found and removed, false in other cases
88
     */
89
    public static function removeValue(mixed $value, array &$array): bool
90
    {
NEW
91
        $position = array_search($value, $array);
×
92

NEW
93
        if (false !== $position) {
×
NEW
94
            unset($array[$position]);
×
NEW
95
            return true;
×
96
        }
97

NEW
98
        return false;
×
99
    }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc