Passed
Push — v3 ( dc00fa...37cdd6 )
by
unknown
02:41
created

Arr::hashes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Helpers;
20
21
use Closure;
22
use RecursiveArrayIterator;
23
use RecursiveIteratorIterator;
24
25
/**
26
 * This helper contains functions to work with arrays.
27
 *
28
 * @package GameQ\Helpers
29
 */
30
class Arr
31
{
32
    use Arr\Recursively;
33
34
    /**
35
     * This helper does process each element of the provided array recursively.
36
     * It does so allowing for modifications to the provided array and without
37
     * using actual recursive calls.
38
     *
39
     * @param array $data
40
     * @param Closure $callback
41
     *
42
     * @return array
43
     */
44 36
    public static function recursively(array $data, Closure $callback)
45
    {
46
        /* Initialize the RecursiveArrayIterator for the provided data */
47 36
        $arrayIterator = new RecursiveArrayIterator($data);
48
49
        /* Configure the Iterator for the RecursiveIterator */
50 36
        $recursiveIterator = new RecursiveIteratorIterator($arrayIterator);
51
52
        /* Traverse the provided data */
53 36
        foreach ($recursiveIterator as $key => $value) {
54
            /* Get the current sub iterator with Type hinting */
55
            /** @var RecursiveArrayIterator */
56 36
            $subIterator = $recursiveIterator->getSubIterator();
57
58
            /* Wrap the implementation to handle PHP < 8.1 behaviour */
59 36
            static::handleArrayIteratorCopyOrReference(
60 30
                $data,
61 30
                $recursiveIterator,
62 30
                $subIterator,
63 30
                function () use ($callback, &$value, $key, $subIterator) {
64
                    /* Execute the callback */
65 36
                    $callback($value, $key, $subIterator);
66
67
                    /* Update the modified value */
68 36
                    $subIterator->offsetSet($key, $value);
0 ignored issues
show
Bug introduced by
The method offsetSet() does not exist on RecursiveIterator. It seems like you code against a sub-type of RecursiveIterator such as Phar or RecursiveCachingIterator or SimpleXMLElement or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                    $subIterator->/** @scrutinizer ignore-call */ 
69
                                  offsetSet($key, $value);
Loading history...
69 36
                }
70 30
            );
71
        }
72
73
        /* Return the processed data */
74 36
        return static::getArrayIteratorCopyOrReference($data, $arrayIterator);
75
    }
76
77
    /**
78
     * This helper is intended to hash the provided array's values
79
     * and return it back as key => hash.
80
     *
81
     * @param array $array
82
     * @return array<string|int, string>
83
     */
84 18
    public static function hashes(array $array)
85
    {
86 18
        $hashes = [];
87
88
        /* Process the provided array */
89 18
        foreach ($array as $key => $value) {
90
            /* Serialze and hash each value individually */
91 18
            $hashes[$key] = md5(serialize($value));
92
        }
93
94
        /* Return array containing the hashes */
95 18
        return $hashes;
96
    }
97
98
    /**
99
     * This helper is intended to set a value inside the provided array.
100
     *
101
     * @param array &$array
102
     * @param array $path
103
     * @param mixed $value
104
     * @return array
105
     */
106 18
    public static function set(array &$array, array $path, $value)
107
    {
108 18
        $current = &$array;
109
110
        /* Process the path until the last element */
111 18
        foreach ($path as $i => $element) {
112
            /* Remove the element from the path */
113 18
            unset($path[$i]);
114
115
            /* Create missing key */
116 18
            if (! isset($current[$element])) {
117 12
                $current[$element] = [];
118
            }
119
120
            /* Set current to a reference of next */
121 18
            $current = &$current[$element];
122
        }
123
124
        /* Finally set the value using the last key */
125 18
        $current = $value;
126
127
        /* Return the current, modified array (level) */
128 18
        return $array;
129
    }
130
}
131