| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Utility class for shuffling information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace CryptoManana\Utilities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CryptoManana\Core\Abstractions\Containers\AbstractRandomnessInjectable as RandomnessContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use CryptoManana\Core\Interfaces\Containers\DataShufflingInterface as Shuffling; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use CryptoManana\Core\StringBuilder as StringBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class DataShuffler - Utility class for data shuffling. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package CryptoManana\Utilities | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property \CryptoManana\Core\Abstractions\Randomness\AbstractGenerator $randomnessSource The randomness generator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class DataShuffler extends RandomnessContainer implements Shuffling | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Shuffle a string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $string The string for shuffling. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return string The output shuffled/scrambled string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @throws \Exception Validation errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 8 |  |     public function shuffleString($string = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 8 |  |         if (!is_string($string)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |             throw new \InvalidArgumentException('The supplied argument is not of type string.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 6 |  |         if (empty($string)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |             return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // Convert the string to an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |         $array = StringBuilder::stringSplit($string, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // Reuse the code for array shuffling and convert result to string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 5 |  |         return implode('', $this->shuffleArray($array)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Shuffle an array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param array $array The array for shuffling. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return array The output shuffled/scrambled array. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 8 |  |     public function shuffleArray(array $array = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 8 |  |         if (empty($array)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 2 |  |             return $array; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 7 |  |         $shuffled = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 7 |  |         $iterator = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         // Reset pointer to the begging | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 7 |  |         reset($array); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         // Iterate through array elements | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 7 |  |         foreach ($array as $keyName => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             // Choose random index | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 7 |  |             $newIndex = ($iterator === 0) ? 0 : $this->randomnessSource->getInt(0, $iterator); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             // Shuffling logic | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 7 |  |             if ($newIndex === $iterator) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 7 |  |                 $shuffled[] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 6 |  |                 $shuffled[] = $shuffled[$newIndex]; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 6 |  |                 $shuffled[$newIndex] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             // Update the index | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 7 |  |             $iterator++; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         // Return the shuffled array | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 7 |  |         return $shuffled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  |