| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Get a random value from an array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @param int   $numReq The amount of values to return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | function array_rand_value(array $array, $numReq = 1) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     if (!count($array)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         return; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     $keys = array_rand($array, $numReq); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     if ($numReq === 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         return $array[$keys]; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     return array_intersect_key($array, array_flip($keys)); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * Get a random value from an array, with the ability to skew the results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | function array_rand_weighted(array $array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     $array = array_filter($array, function ($item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $item >= 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     if (!count($array)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     $totalWeight = array_sum($array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     foreach ($array as $value => $weight) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (rand(1, $totalWeight) <= $weight) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $totalWeight -= $weight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  * Determine if all given needles are present in the haystack. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  * @param array|string $needles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  * @param array        $haystack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | function values_in_array($needles, array $haystack) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     if (!is_array($needles)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $needles = [$needles]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     return count(array_intersect($needles, $haystack)) === count($needles); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  * Determine if all given needles are present in the haystack as array keys. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  * @param array|string $needles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  * @param array        $haystack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | function array_keys_exist($needles, array $haystack) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     if (!is_array($needles)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return array_key_exists($needles, $haystack); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     return values_in_array($needles, array_keys($haystack)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  * Returns an array with two elements. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  * Iterates over each value in the array passing them to the callback function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  * If the callback function returns true, the current value from array is returned in the first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  * element of result array. If not, it is return in the second element of result array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  * Array keys are preserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  * @param array    $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  * @param callable $callback | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | function array_split_filter(array $array, callable $callback) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     $passesFilter = array_filter($array, $callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     $negatedCallback = static function ($item) use ($callback) { return !$callback($item); }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     $doesNotPassFilter = array_filter($array, $negatedCallback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     return [$passesFilter, $doesNotPassFilter]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  * Split an array in the given amount of pieces. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  * @param int   $numberOfPieces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  * @param bool  $preserveKeys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  * @throws \InvalidArgumentException if the provided argument $numberOfPieces is lower than 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     if ($numberOfPieces <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         throw new \InvalidArgumentException('Number of pieces parameter expected to be greater than 0'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     if (count($array) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     $splitSize = ceil(count($array) / $numberOfPieces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     return array_chunk($array, $splitSize, $preserveKeys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  * Returns an array with the unique values from all the given arrays. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  * @param \array[] $arrays | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | function array_merge_values(array ...$arrays) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     $allValues = array_reduce($arrays, static function ($carry, $array) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |          return array_merge($carry, $array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     }, []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     return array_values(array_unique($allValues)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  * Flatten an array of arrays. The `$levels` parameter specifies how deep you want to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  * recurse in the array. If `$levels` is -1, the function will recurse infinitely. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  * @param int   $levels | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | function array_flatten(array $array, $levels = -1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     if ($levels === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         return $array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     $flattened = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     if ($levels !== -1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         --$levels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     foreach ($array as $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $flattened[] = is_array($element) ? array_flatten($element, $levels) : [$element]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     return array_merge([], ...$flattened); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |  |