| 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 | 24 |  |                 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 36 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 18 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 36 |  |     public static function hashes(array $array) | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 36 |  |         $hashes = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         /* Process the provided array */ | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 36 |  |         foreach ($array as $key => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             /* Serialze and hash each value individually */ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 36 |  |             $hashes[$key] = md5(serialize($value)); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 6 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         /* Return array containing the hashes */ | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 36 |  |         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 | 36 |  |     public static function set(array &$array, array $path, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 36 |  |         $current = &$array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         /* Process the path until the last element */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 36 |  |         foreach ($path as $i => $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             /* Remove the element from the path */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 36 |  |             unset($path[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             /* Create missing key */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 36 |  |             if (! isset($current[$element])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 24 |  |                 $current[$element] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 4 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             /* Set current to a reference of next */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 36 |  |             $current = &$current[$element]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         /* Finally set the value using the last key */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 36 |  |         $current = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         /* Return the current, modified array (level) */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 36 |  |         return $array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  |