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

heimrichhannot / contao-utils-bundle / 9402045860

06 Jun 2024 01:45PM UTC coverage: 23.379% (-0.009%) from 23.388%
9402045860

push

github

koertho
fix #84

0 of 3 new or added lines in 1 file covered. (0.0%)

1320 of 5646 relevant lines covered (23.38%)

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, $keys, string $newKey, $newValue): void
18
    {
NEW
19
        if (is_string($keys)) {
×
20
            $keys = [$keys];
×
21
        }
22

NEW
23
        if (!is_array($keys)) {
×
NEW
24
            throw new \InvalidArgumentException('Parameter $keys must be of type array or string.');
×
25
        }
26

27
        if (array_intersect($keys, array_keys($array))) {
×
28
            $new = [];
×
29

30
            foreach ($array as $k => $value) {
×
31
                if (\in_array($k, $keys)) {
×
32
                    $new[$newKey] = $newValue;
×
33
                }
34
                $new[$k] = $value;
×
35
            }
36
            $array = $new;
×
37
        } else {
38
            $array[$newKey] = $newValue;
×
39
        }
40
    }
41

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

67
        $keys = array_keys($array);
×
68
        $index = array_search($key, $keys, $options['strict']);
×
69

70
        if (false === $index && false === $options['attachMissingKey']) {
×
71
            return;
×
72
        }
73
        $pos = false === $index ? count($array) : $index + 1;
×
74
        $pos = $pos + $options['offset'];
×
75

76
        if ($newKey) {
×
77
            $value = [$newKey => $value];
×
78
        } else {
79
            $value = [$value];
×
80
        }
81

82
        $array = array_combine(
×
83
            array_merge(array_slice($keys, 0, $pos), array_keys($value), array_slice($keys, $pos)),
×
84
            array_merge(array_slice($array, 0, $pos), $value, array_slice($array, $pos))
×
85
        );
×
86
    }
87

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

97
        if (false !== $position) {
×
98
            unset($array[$position]);
×
99
            return true;
×
100
        }
101

102
        return false;
×
103
    }
104
}
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

© 2025 Coveralls, Inc