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

heimrichhannot / contao-utils-bundle / 15132588601

20 May 2025 08:23AM UTC coverage: 25.429% (+0.1%) from 25.315%
15132588601

Pull #99

github

koertho
add filterByPrefixes to StaticArrayUtils
Pull Request #99: Add filterByPrefixes to StaticArrayUtil

9 of 13 new or added lines in 2 files covered. (69.23%)

33 existing lines in 1 file now uncovered.

1498 of 5891 relevant lines covered (25.43%)

1.56 hits per line

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

3.03
/src/Arrays/ArrayUtil.php
1
<?php
2

3
/*
4
 * Copyright (c) 2022 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Arrays;
10

11
use Contao\CoreBundle\Framework\ContaoFramework;
12
use Contao\StringUtil;
13
use Contao\Validator;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15

16
class ArrayUtil
17
{
18
    /**
19
     * @var ContaoFramework
20
     */
21
    protected $framework;
22
    /**
23
     * @var ContainerInterface
24
     */
25
    private $container;
26

27
    public function __construct(ContainerInterface $container)
28
    {
29
        $this->framework = $container->get('contao.framework');
6✔
30
        $this->container = $container;
6✔
31
    }
32

33
    /**
34
     * Filter an Array by given prefixes.
35
     *
36
     * @param array $prefixes
37
     *
38
     * @return array the filtered array or $arrData if $prefix is empty
39
     */
40
    public function filterByPrefixes(array $data = [], $prefixes = [])
41
    {
UNCOV
42
        $extract = [];
×
43

UNCOV
44
        if (!\is_array($prefixes) || empty($prefixes)) {
×
NEW
45
            return $data;
×
46
        }
47

NEW
48
        foreach ($data as $key => $value) {
×
NEW
49
            foreach ($prefixes as $prefix) {
×
NEW
50
                if ($this->container->get('huh.utils.string')->startsWith($key, $prefix)) {
×
51
                    $extract[$key] = $value;
×
52
                }
53
            }
54
        }
55

UNCOV
56
        return $extract;
×
57
    }
58

59
    /**
60
     * sort an array alphabetically by some key in the second layer (x => array(key1, key2, key3)).
61
     */
62
    public function aasort(array &$array, $key)
63
    {
64
        $sorter = [];
×
65
        $ret = [];
×
UNCOV
66
        reset($array);
×
67

68
        foreach ($array as $ii => $va) {
×
UNCOV
69
            $sorter[$ii] = $va[$key];
×
70
        }
71

UNCOV
72
        asort($sorter);
×
73

74
        foreach ($sorter as $ii => $va) {
×
UNCOV
75
            $ret[$ii] = $array[$ii];
×
76
        }
77

UNCOV
78
        $array = $ret;
×
79
    }
80

81
    /**
82
     * Removes a value in an array.
83
     *
84
     * @param $value
85
     *
86
     * @return bool Returns true if the value has been found and removed, false in other cases
87
     *
88
     * @deprecated Use Utils service instead
89
     * @codeCoverageIgnore
90
     */
91
    public function removeValue($value, array &$array): bool
92
    {
93
        if (false !== ($intPosition = array_search($value, $array))) {
94
            unset($array[$intPosition]);
95

96
            return true;
97
        }
98

99
        return false;
100
    }
101

102
    public function removePrefix(string $prefix, array $array): array
103
    {
UNCOV
104
        $result = [];
×
105

106
        foreach ($array as $key => $value) {
×
UNCOV
107
            $result[$this->container->get('huh.utils.string')->removeLeadingString($prefix, $key)] = $value;
×
108
        }
109

UNCOV
110
        return $result;
×
111
    }
112

113
    /**
114
     * Insert a value into an existing array by key name.
115
     *
116
     * @param array  $current The target array
117
     * @param string $key     the existing target key in the array
118
     * @param mixed  $value   the new value to be inserted
119
     * @param int    $offset  offset for inserting the new value
120
     * @param bool   $strict  use strict behavior for array search
121
     *
122
     * @deprecated Use utils service instead
123
     * @codeCoverageIgnore
124
     */
125
    public function insertInArrayByName(array &$current, string $key, $value, int $offset = 0, bool $strict = false)
126
    {
127
        if (false !== ($intIndex = array_search($key, array_keys($current), $strict))) {
128
            array_insert($current, $intIndex + $offset, $value);
129
        }
130
    }
131

132
    /**
133
     * Creates a stdClass from array.
134
     *
135
     * @param $array
136
     */
137
    public function arrayToObject(array $array): \stdClass
138
    {
UNCOV
139
        $objResult = new \stdClass();
×
140

141
        foreach ($array as $varKey => $varValue) {
×
UNCOV
142
            $objResult->{$varKey} = $varValue;
×
143
        }
144

UNCOV
145
        return $objResult;
×
146
    }
147

148
    /**
149
     * Returns a row of an multidimensional array by field value. Returns false, if no row found.
150
     *
151
     * @param string|int $key        The array key (fieldname)
152
     * @param mixed      $value
153
     * @param array      $haystack   a multidimensional array
154
     * @param bool       $strictType Specifiy if type comparison should be strict (type-safe)
155
     *
156
     * @return mixed
157
     */
158
    public function getArrayRowByFieldValue($key, $value, array $haystack, bool $strictType = false)
159
    {
UNCOV
160
        foreach ($haystack as $row) {
×
161
            if (!\is_array($row)) {
×
162
                continue;
×
163
            }
164

165
            if (!isset($row[$key])) {
×
166
                continue;
×
167
            }
168

UNCOV
169
            if (true === $strictType) {
×
170
                if ($value === $row[$key]) {
×
171
                    return $row;
×
172
                }
173
            } else {
UNCOV
174
                if ($row[$key] == $value) {
×
UNCOV
175
                    return $row;
×
176
                }
177
            }
178
        }
179

UNCOV
180
        return false;
×
181
    }
182

183
    /**
184
     * Flattens an multidimensional array to one dimension. Keys are not preserved.
185
     *
186
     * @return array
187
     */
188
    public function flattenArray(array $array)
189
    {
190
        $return = [];
×
191
        array_walk_recursive(
×
192
            $array,
×
UNCOV
193
            function ($a) use (&$return) {
×
194
                $return[] = $a;
×
UNCOV
195
            }
×
UNCOV
196
        );
×
197

UNCOV
198
        return $return;
×
199
    }
200

201
    /**
202
     * Insert a new entry before an specific key in array.
203
     * If key not exist, the new entry is added to the end of the array.
204
     * Array is passed as reference.
205
     *
206
     * Usage example: contao config.php to make your hook entry run before another.
207
     *
208
     * @param array  $array    Array the new entry should inserted to
209
     * @param string $key      The key where the new entry should be added before
210
     * @param string $newKey   The key of the entry that should be added
211
     * @param mixed  $newValue The value of the entry that should be added
212
     *
213
     * @deprecated Use Utils::insertBeforeKey() instead
214
     */
215
    public static function insertBeforeKey(array &$array, string $key, string $newKey, $newValue)
216
    {
217
        if (\array_key_exists($key, $array)) {
×
218
            $new = [];
×
219

220
            foreach ($array as $k => $value) {
×
UNCOV
221
                if ($k === $key) {
×
222
                    $new[$newKey] = $newValue;
×
223
                }
224
                $new[$k] = $value;
×
225
            }
UNCOV
226
            $array = $new;
×
227
        } else {
UNCOV
228
            $array[$newKey] = $newValue;
×
229
        }
230
    }
231

232
    public function implodeRecursive($var, $binary = false)
233
    {
234
        if (!\is_array($var)) {
×
235
            return $binary ? StringUtil::binToUuid($var) : $var;
×
236
        }
237

238
        if (!\is_array(current($var))) {
×
UNCOV
239
            if ($binary) {
×
UNCOV
240
                $var = array_map(function ($v) {
×
241
                    return $v ? (Validator::isBinaryUuid($v) ? StringUtil::binToUuid($v) : $v) : '';
×
UNCOV
242
                }, $var);
×
243
            }
244

UNCOV
245
            return implode(', ', $var);
×
246
        }
247

UNCOV
248
        $buffer = '';
×
249

250
        foreach ($var as $k => $v) {
×
UNCOV
251
            $buffer .= $k.': '.$this->implodeRecursive($v)."\n";
×
252
        }
253

UNCOV
254
        return trim($buffer);
×
255
    }
256
}
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