| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Maxim Sokolovsky | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace WS\Utils\Collections\Functions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WS\Utils\Collections\ArrayList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WS\Utils\Collections\Collection; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class Reorganizers | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 19 |  |     private static function collectionConstructor(? array $elements = null): Collection  { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 | 19 |  |         return new ArrayList($elements); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * Returns <Fn($c: Collection): Collection> that shuffles elements | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @return Closure | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 5 |  |     public static function shuffle(): Closure | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         return static function (Collection $collection): Collection { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 5 |  |             $array = $collection->toArray(); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 5 |  |             shuffle($array); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 5 |  |             return self::collectionConstructor($array); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 5 |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Returns Closure <Fn($c: Collection): Collection> that gets $count random elements from collection | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param int $count | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @return Closure | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 10 |  |     public static function random(int $count = 1): Closure | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         return static function (Collection $collection) use ($count): Collection { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |              * Collection | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |              */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 10 |  |             $resultCollection = self::collectionConstructor(); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 10 |  |             if ($count === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 2 |  |                 return $resultCollection; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 8 |  |             $collectionSize = $collection->size(); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 8 |  |             $expectedCount = $count; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 8 |  |             if ($collectionSize < $expectedCount) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 2 |  |                 return $resultCollection; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 6 |  |             $rest = $collectionSize; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 6 |  |             $generated = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 6 |  |             $multiplicity = (int)round($collectionSize / $expectedCount); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $rangeRandomizer = static function () use ($multiplicity): int { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 4 |  |                 return random_int(0, $multiplicity - 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 6 |  |             }; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $trier = static function () use (& $generated, & $rest, $expectedCount, $rangeRandomizer): bool { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 6 |  |                 $rest--; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 6 |  |                 if ($generated === $expectedCount) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 3 |  |                     return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 6 |  |                 if ($generated + $rest + 1 <= $expectedCount) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 3 |  |                     return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 4 |  |                 if ($rangeRandomizer() !== 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 2 |  |                     return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 3 |  |                 $generated++; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 3 |  |                 return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 6 |  |             }; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             return $collection | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 6 |  |                 ->stream() | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 6 |  |                 ->filter($trier) | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 6 |  |                 ->getCollection(); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 10 |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * Returns Closure <Fn($c: Collection): Collection> that split collection into sub collections with $size | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @param int $size | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @return Closure | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |     public static function chunk(int $size): Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return static function (Collection $collection) use ($size): Collection { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |             $chunkCollection = self::collectionConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |             $currentChunk = self::collectionConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             $pointer = $size; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |                 ->stream() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 ->each(static function ($el) use ($size, $chunkCollection, & $currentChunk, & $pointer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |                     $pointer--; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |                     $currentChunk->add($el); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |                     if ($pointer === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |                         $chunkCollection->add($currentChunk); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |                         $currentChunk = self::collectionConstructor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |                         $pointer = $size; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |                 }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 | 1 |  |             return $chunkCollection; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 110 | 1 |  |         }; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * Returns Closure <Fn($c: Collection): Collection> that collapses a collection of arrays into a single, flat collection | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * @param int $depth Depth of  collapses. The 0 value is without | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @return Closure | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |     public static function collapse(int $depth = 0): Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 3 |  |         if ($depth === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |             $depth = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return static function (Collection $collection) use ($depth): Collection { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $flatIterable = static function (iterable $collection, $depth) use (& $flatIterable): array  { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 3 |  |                 $goToDepth = $depth > 0 || $depth === null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  |                 if ($depth !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  |                     $depth--; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 3 |  |                 $res = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 3 |  |                 foreach ($collection as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 3 |  |                     if (is_iterable($item) && $goToDepth) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 3 |  |                         $toPush = $flatIterable($item, $depth); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 3 |  |                         array_unshift($toPush, $res); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 3 |  |                         array_push(...$toPush); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 3 |  |                         $res = array_shift($toPush); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 3 |  |                         $res[] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |                 return $res; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 3 |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 | 3 |  |             return self::collectionConstructor($flatIterable($collection, $depth)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 3 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |