Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
34 / 34 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CsvSettings | |
100.00% |
34 / 34 |
|
100.00% |
2 / 2 |
13 | |
100.00% |
1 / 1 |
| getFileDelimiter | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
8 | |||
| getStringDelimiter | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\IO\Csv |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\IO\Csv; |
| 16 | |
| 17 | /** |
| 18 | * Csv settings. |
| 19 | * |
| 20 | * @package phpOMS\Utils\IO\Csv |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class CsvSettings |
| 26 | { |
| 27 | /** |
| 28 | * Get csv file delimiter based on file content. |
| 29 | * |
| 30 | * @param resource $file File resource |
| 31 | * @param int $checkLines Lines to check for evaluation |
| 32 | * @param string[] $delimiters Potential delimiters |
| 33 | * |
| 34 | * @return string |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public static function getFileDelimiter($file, int $checkLines = 2, array $delimiters = [',', "\t", ';', '|', ':']) : string |
| 39 | { |
| 40 | $results = []; |
| 41 | $i = 0; |
| 42 | $line = \fgets($file); |
| 43 | |
| 44 | if ($line === false) { |
| 45 | return ';'; // @codeCoverageIgnore |
| 46 | } |
| 47 | |
| 48 | while ($line !== false && $i < $checkLines) { |
| 49 | ++$i; |
| 50 | |
| 51 | foreach ($delimiters as $delimiter) { |
| 52 | $regExp = '/[' . $delimiter . ']/'; |
| 53 | $fields = \preg_split($regExp, $line); |
| 54 | |
| 55 | if ($fields === false) { |
| 56 | return ';'; // @codeCoverageIgnore |
| 57 | } |
| 58 | |
| 59 | if (\count($fields) > 1) { |
| 60 | if (!empty($results[$delimiter])) { |
| 61 | ++$results[$delimiter]; |
| 62 | } else { |
| 63 | $results[$delimiter] = 1; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | $line = \fgets($file); |
| 69 | } |
| 70 | |
| 71 | \rewind($file); |
| 72 | |
| 73 | $results = \array_keys($results, \max($results)); |
| 74 | |
| 75 | return $results[0]; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get csv string delimiter based on string content. |
| 80 | * |
| 81 | * @param string $content File content |
| 82 | * @param int $checkLines Lines to check for evaluation |
| 83 | * @param string[] $delimiters Potential delimiters |
| 84 | * |
| 85 | * @return string |
| 86 | * |
| 87 | * @since 1.0.0 |
| 88 | */ |
| 89 | public static function getStringDelimiter(string $content, int $checkLines = 2, array $delimiters = [',', "\t", ';', '|', ':']) : string |
| 90 | { |
| 91 | $results = []; |
| 92 | $lines = \explode("\n", $content); |
| 93 | $i = 0; |
| 94 | |
| 95 | do { |
| 96 | $line = $lines[$i]; |
| 97 | foreach ($delimiters as $delimiter) { |
| 98 | $regExp = '/[' . $delimiter . ']/'; |
| 99 | $fields = \preg_split($regExp, $line); |
| 100 | |
| 101 | if ($fields === false) { |
| 102 | return ';'; // @codeCoverageIgnore |
| 103 | } |
| 104 | |
| 105 | if (\count($fields) > 1) { |
| 106 | if (!empty($results[$delimiter])) { |
| 107 | ++$results[$delimiter]; |
| 108 | } else { |
| 109 | $results[$delimiter] = 1; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | ++$i; |
| 115 | } while ($i < $checkLines); |
| 116 | |
| 117 | $results = \array_keys($results, \max($results)); |
| 118 | |
| 119 | return $results[0]; |
| 120 | } |
| 121 | } |