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

aplus-framework / helpers / 6092639362

28 Jul 2023 09:51PM UTC coverage: 100.0%. Remained the same
6092639362

push

github

natanfelles
Update Security Policy

92 of 92 relevant lines covered (100.0%)

2.14 hits per line

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

100.0
/src/ArraySimple.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Helpers Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Helpers;
11

12
use JetBrains\PhpStorm\Pure;
13

14
/**
15
 * Class ArraySimple.
16
 *
17
 * Contains methods to work with PHP arrays using "simple keys" (strings with
18
 * square brackets).
19
 *
20
 * Simple key format example: `parent[child1][child2]`.
21
 *
22
 * `user[country][state]` gets 'rs' in
23
 * `array('user' => ['country' => ['state' => 'rs']])`
24
 *
25
 * @see https://www.php.net/manual/en/language.types.array.php
26
 *
27
 * @package helpers
28
 */
29
class ArraySimple
30
{
31
    /**
32
     * @param string $simpleKey
33
     *
34
     * @return array<int,string>
35
     */
36
    protected static function extractKeys(string $simpleKey) : array
37
    {
38
        \preg_match_all('#\[(.*?)\]#', $simpleKey, $matches);
3✔
39
        return $matches[1] ?? [];
3✔
40
    }
41

42
    /**
43
     * Reverts an associative array of simple keys to an native array.
44
     *
45
     * @param array<mixed> $arraySimple An array with simple keys
46
     *
47
     * @return array<string,mixed> An array with their corresponding values
48
     */
49
    public static function revert(array $arraySimple) : array
50
    {
51
        $array = [];
3✔
52
        foreach ($arraySimple as $simpleKey => $value) {
3✔
53
            $simpleKey = (string) $simpleKey;
3✔
54
            $parentKey = static::getParentKey($simpleKey);
3✔
55
            if ($parentKey === null) {
3✔
56
                $array[$simpleKey] = \is_array($value)
3✔
57
                    ? static::revert($value)
3✔
58
                    : $value;
3✔
59
                continue;
3✔
60
            }
61
            $parent = [];
3✔
62
            static::addChild(
3✔
63
                $parent,
3✔
64
                \array_merge([$parentKey], static::extractKeys($simpleKey)),
3✔
65
                $value
3✔
66
            );
3✔
67
            $array = \array_replace_recursive($array, $parent);
3✔
68
        }
69
        return $array;
3✔
70
    }
71

72
    /**
73
     * Converts an array to an associative array with simple keys.
74
     *
75
     * @param array<mixed> $array Array to be converted
76
     *
77
     * @return array<string,mixed> An associative array with the simple keys as
78
     * keys and their corresponding values
79
     */
80
    public static function convert(array $array) : array
81
    {
82
        $array = static::revert($array);
1✔
83
        $data = [];
1✔
84
        foreach (static::keys($array) as $key) {
1✔
85
            $data[$key] = static::value($key, $array);
1✔
86
        }
87
        return $data;
1✔
88
    }
89

90
    /**
91
     * Gets the value of an array item through a simple key.
92
     *
93
     * @param string $simpleKey A string in the simple key format
94
     * @param array<mixed> $array The array to search in
95
     *
96
     * @return mixed The item value or null if not found
97
     */
98
    public static function value(string $simpleKey, array $array) : mixed
99
    {
100
        $array = static::revert($array);
2✔
101
        $parentKey = static::getParentKey($simpleKey);
2✔
102
        if ($parentKey === null) {
2✔
103
            return $array[$simpleKey] ?? null;
2✔
104
        }
105
        $value = $array[$parentKey] ?? null;
2✔
106
        foreach (static::extractKeys($simpleKey) as $key) {
2✔
107
            if ( ! (\is_array($value) && \array_key_exists($key, $value))) {
2✔
108
                return null;
1✔
109
            }
110
            $value = $value[$key];
2✔
111
        }
112
        return $value;
2✔
113
    }
114

115
    /**
116
     * Gets the keys of an array in the simple keys format.
117
     *
118
     * @param array<mixed> $array The array to get the simple keys
119
     *
120
     * @return array<int,string> An indexed array containing the simple keys as
121
     * values
122
     */
123
    #[Pure]
124
    public static function keys(array $array) : array
125
    {
126
        return static::getKeys($array);
2✔
127
    }
128

129
    /**
130
     * @param array<mixed> $array
131
     * @param string $childKey
132
     *
133
     * @return array<int,string>
134
     */
135
    #[Pure]
136
    protected static function getKeys(array $array, string $childKey = '') : array
137
    {
138
        $allKeys = [];
2✔
139
        foreach ($array as $key => $value) {
2✔
140
            $key = (string) $key;
2✔
141
            if (\is_array($value)) {
2✔
142
                $allKeys = $childKey === ''
2✔
143
                    ? \array_merge($allKeys, static::getKeys($value, $key))
2✔
144
                    : \array_merge(
2✔
145
                        $allKeys,
2✔
146
                        static::getKeys($value, $childKey . static::getChildKey($key))
2✔
147
                    );
2✔
148
                continue;
2✔
149
            }
150
            $allKeys[] = $childKey === ''
2✔
151
                ? $key
2✔
152
                : $childKey . static::getChildKey($key);
2✔
153
        }
154
        return $allKeys;
2✔
155
    }
156

157
    /**
158
     * @param array<int,string> $parent
159
     * @param array<int,string> $childs
160
     * @param mixed $value
161
     */
162
    protected static function addChild(array &$parent, array $childs, mixed $value) : void
163
    {
164
        $key = \array_shift($childs);
3✔
165
        $key = (string) $key;
3✔
166
        $parent[$key] = [];
3✔
167
        if ($childs === []) {
3✔
168
            $parent[$key] = $value;
3✔
169
            return;
3✔
170
        }
171
        static::addChild($parent[$key], $childs, $value);
3✔
172
    }
173

174
    #[Pure]
175
    protected static function getParentKey(string $key) : ?string
176
    {
177
        $posOpen = \strpos($key, '[');
4✔
178
        $posClose = $posOpen ? \strpos($key, ']', $posOpen) : false;
4✔
179
        if ($posClose === false) {
4✔
180
            return null;
4✔
181
        }
182
        return \substr($key, 0, $posOpen); // @phpstan-ignore-line
4✔
183
    }
184

185
    #[Pure]
186
    protected static function getChildKey(string $key) : string
187
    {
188
        $parentKey = static::getParentKey($key);
2✔
189
        if ($parentKey === null) {
2✔
190
            return '[' . $key . ']';
2✔
191
        }
192
        $key = \explode('[', $key, 2);
1✔
193
        $key = '[' . $key[0] . '][' . $key[1];
1✔
194
        return $key;
1✔
195
    }
196

197
    /**
198
     * Get $_FILES in a re-organized way.
199
     *
200
     * NOTE: Do not use file input names as `name`, `type`, `tmp_name`, `error`,
201
     * `full_path` and `size` to avoid overwrite of arrays.
202
     *
203
     * @return array<string,mixed> An array ready to be used with
204
     * {@see ArraySimple::value()}
205
     */
206
    #[Pure]
207
    public static function files() : array
208
    {
209
        $files = [];
1✔
210
        foreach ($_FILES as $name => $values) {
1✔
211
            if ( ! isset($files[$name])) {
1✔
212
                $files[$name] = [];
1✔
213
            }
214
            if ( ! \is_array($values['error'])) {
1✔
215
                $files[$name] = $values;
1✔
216
                continue;
1✔
217
            }
218
            foreach ($values as $infoKey => $subArray) {
1✔
219
                $files[$name] = \array_replace_recursive(
1✔
220
                    $files[$name],
1✔
221
                    static::filesWalker($subArray, $infoKey)
1✔
222
                );
1✔
223
            }
224
        }
225
        return $files;
1✔
226
    }
227

228
    /**
229
     * @see https://stackoverflow.com/a/33261775/6027968
230
     *
231
     * @param array<mixed> $array
232
     * @param string $infoKey
233
     *
234
     * @return array<string,mixed>
235
     */
236
    #[Pure]
237
    protected static function filesWalker(array $array, string $infoKey) : array
238
    {
239
        $return = [];
1✔
240
        foreach ($array as $key => $value) {
1✔
241
            $key = (string) $key;
1✔
242
            if (\is_array($value)) {
1✔
243
                $return[$key] = static::filesWalker($value, $infoKey);
1✔
244
                continue;
1✔
245
            }
246
            $return[$key][$infoKey] = $value;
1✔
247
        }
248
        return $return;
1✔
249
    }
250
}
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