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

heimrichhannot / contao-utils-bundle / 15132848912

20 May 2025 08:36AM UTC coverage: 25.433% (+0.1%) from 25.315%
15132848912

push

github

web-flow
Add filterByPrefixes to StaticArrayUtil (#99)

* add filterByPrefixes to StaticArrayUtils

* deprecate method, update changelog

9 of 16 new or added lines in 2 files covered. (56.25%)

1498 of 5890 relevant lines covered (25.43%)

1.56 hits per line

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

3.08
/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 HeimrichHannot\UtilsBundle\StaticUtil\SUtils;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16

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

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

34
    /**
35
     * Filter an Array by given prefixes.
36
     *
37
     * @param array $prefixes
38
     *
39
     * @return array the filtered array or $arrData if $prefix is empty
40
     *
41
     * @deprecated Use StaticUtils instead
42
     */
43
    public function filterByPrefixes(array $data = [], $prefixes = [])
44
    {
NEW
45
        trigger_deprecation(
×
NEW
46
            'heimrichhannot/contao-utils-bundle',
×
NEW
47
            '2.243.0',
×
NEW
48
            'The "%s" method is deprecated and will be removed in the next major version. Use SUtils::array::filterByPrefixes() instead.',
×
NEW
49
            __METHOD__
×
NEW
50
        );
×
51

NEW
52
        return SUtils::array()::filterByPrefixes($data, $prefixes);
×
53
    }
54

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

64
        foreach ($array as $ii => $va) {
×
65
            $sorter[$ii] = $va[$key];
×
66
        }
67

68
        asort($sorter);
×
69

70
        foreach ($sorter as $ii => $va) {
×
71
            $ret[$ii] = $array[$ii];
×
72
        }
73

74
        $array = $ret;
×
75
    }
76

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

92
            return true;
93
        }
94

95
        return false;
96
    }
97

98
    public function removePrefix(string $prefix, array $array): array
99
    {
100
        $result = [];
×
101

102
        foreach ($array as $key => $value) {
×
103
            $result[$this->container->get('huh.utils.string')->removeLeadingString($prefix, $key)] = $value;
×
104
        }
105

106
        return $result;
×
107
    }
108

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

128
    /**
129
     * Creates a stdClass from array.
130
     *
131
     * @param $array
132
     */
133
    public function arrayToObject(array $array): \stdClass
134
    {
135
        $objResult = new \stdClass();
×
136

137
        foreach ($array as $varKey => $varValue) {
×
138
            $objResult->{$varKey} = $varValue;
×
139
        }
140

141
        return $objResult;
×
142
    }
143

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

161
            if (!isset($row[$key])) {
×
162
                continue;
×
163
            }
164

165
            if (true === $strictType) {
×
166
                if ($value === $row[$key]) {
×
167
                    return $row;
×
168
                }
169
            } else {
170
                if ($row[$key] == $value) {
×
171
                    return $row;
×
172
                }
173
            }
174
        }
175

176
        return false;
×
177
    }
178

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

194
        return $return;
×
195
    }
196

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

216
            foreach ($array as $k => $value) {
×
217
                if ($k === $key) {
×
218
                    $new[$newKey] = $newValue;
×
219
                }
220
                $new[$k] = $value;
×
221
            }
222
            $array = $new;
×
223
        } else {
224
            $array[$newKey] = $newValue;
×
225
        }
226
    }
227

228
    public function implodeRecursive($var, $binary = false)
229
    {
230
        if (!\is_array($var)) {
×
231
            return $binary ? StringUtil::binToUuid($var) : $var;
×
232
        }
233

234
        if (!\is_array(current($var))) {
×
235
            if ($binary) {
×
236
                $var = array_map(function ($v) {
×
237
                    return $v ? (Validator::isBinaryUuid($v) ? StringUtil::binToUuid($v) : $v) : '';
×
238
                }, $var);
×
239
            }
240

241
            return implode(', ', $var);
×
242
        }
243

244
        $buffer = '';
×
245

246
        foreach ($var as $k => $v) {
×
247
            $buffer .= $k.': '.$this->implodeRecursive($v)."\n";
×
248
        }
249

250
        return trim($buffer);
×
251
    }
252
}
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