| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * League.Csv (https://csv.thephpleague.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Ignace Nyamagana Butera <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace League\Csv; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use ReflectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function array_fill_keys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use function array_filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use function array_reduce; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use function array_unique; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use function count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use function iterator_to_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use function strpos; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use const COUNT_RECURSIVE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Returns the BOM sequence found at the start of the string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * If no valid BOM sequence is found an empty string is returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | function bom_match(string $str): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     static $list; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 33 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     $list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 33 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     foreach ($list as $sequence) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 33 |  |         if (0 === strpos($str, $sequence)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 33 |  |             return $sequence; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 15 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 27 |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * Detect Delimiters usage in a {@link Reader} object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * Returns a associative array where each key represents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * a submitted delimiter and each value the number CSV fields found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * when processing at most $limit CSV records with the given delimiter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  * @param string[] $delimiters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  * @return int[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     $found = array_unique(array_filter($delimiters, static function (string $value): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return 1 == strlen($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 18 |  |     })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 18 |  |     $stmt = (new Statement())->limit($limit)->where(static function (array $record): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return count($record) > 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 9 |  |     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 12 |  |     $reducer = static function (array $result, string $delimiter) use ($csv, $stmt): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $result[$delimiter] = count(iterator_to_array($stmt->process($csv->setDelimiter($delimiter)), false), COUNT_RECURSIVE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 9 |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 12 |  |     $delimiter = $csv->getDelimiter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 12 |  |     $header_offset = $csv->getHeaderOffset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 12 |  |     $csv->setHeaderOffset(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 12 |  |     $stats = array_reduce($found, $reducer, array_fill_keys($delimiters, 0)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 12 |  |     $csv->setHeaderOffset($header_offset)->setDelimiter($delimiter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     return $stats; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  |