|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MakinaCorpus\DbToolsBundle\Helper; |
| 6 | + |
| 7 | +use MakinaCorpus\DbToolsBundle\Error\ConfigurationException; |
| 8 | +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; |
| 9 | + |
| 10 | +class FileReader |
| 11 | +{ |
| 12 | + public static function getFileExtension(string $filename): ?string |
| 13 | + { |
| 14 | + $ext = null; |
| 15 | + if ($pos = \strrpos($filename, '.')) { |
| 16 | + $ext = \substr($filename, $pos + 1); |
| 17 | + } |
| 18 | + |
| 19 | + return $ext; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Iterator on file contents. |
| 24 | + * |
| 25 | + * @return iterable<string> |
| 26 | + */ |
| 27 | + public static function readEnumFile(string $filename, ?Options $options = null, ?string $anonymizerId = null): iterable |
| 28 | + { |
| 29 | + $ext = self::getFileExtension($filename); |
| 30 | + |
| 31 | + // no match() usage here because CSV cannot expressed as a single expression. |
| 32 | + if (null === $ext || 'txt' === $ext) { |
| 33 | + yield from self::readTxtFile($filename, $options, $anonymizerId); |
| 34 | + } elseif ('csv' === $ext || 'tsv' === $ext) { |
| 35 | + foreach (self::readCsvFile($filename, $options, $anonymizerId) as $line) { |
| 36 | + \assert(\is_array($line)); |
| 37 | + if ($line) { |
| 38 | + yield $line[0]; |
| 39 | + } |
| 40 | + } |
| 41 | + } elseif ($anonymizerId) { |
| 42 | + throw new ConfigurationException(\sprintf("Anonymizer '%s': unsupported enum data file type: '%s'.", $anonymizerId, $ext)); |
| 43 | + } else { |
| 44 | + throw new ConfigurationException(\sprintf("Unsupported enum data file type: '%s'.", $ext)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Iterator on column file contents. |
| 50 | + * |
| 51 | + * @return iterable<array<string>> |
| 52 | + */ |
| 53 | + public static function readColumnFile(string $filename, ?Options $options = null, ?string $anonymizerId = null): iterable |
| 54 | + { |
| 55 | + $ext = self::getFileExtension($filename); |
| 56 | + |
| 57 | + // no match() usage here because CSV cannot expressed as a single expression. |
| 58 | + if ('csv' === $ext || 'tsv' === $ext) { |
| 59 | + yield from self::readCsvFile($filename, $options, $anonymizerId); |
| 60 | + } else { |
| 61 | + throw new ConfigurationException("Unsupported column data file type."); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Iterator on plain text file lines. |
| 67 | + * |
| 68 | + * @return iterable<string> |
| 69 | + */ |
| 70 | + public static function readTxtFile(string $filename, ?Options $options = null, ?string $anonymizerId = null): iterable |
| 71 | + { |
| 72 | + self::ensureFile($filename, $anonymizerId); |
| 73 | + |
| 74 | + $options ??= new Options(); |
| 75 | + |
| 76 | + $handle = null; |
| 77 | + try { |
| 78 | + $handle = \fopen($filename, 'r'); |
| 79 | + |
| 80 | + if (false === $handle) { |
| 81 | + if ($anonymizerId) { |
| 82 | + throw new ConfigurationException(\sprintf("Anonymizer '%s' could not open file: %s", $anonymizerId, $filename)); |
| 83 | + } else { |
| 84 | + throw new ConfigurationException(\sprintf("Could not open file: %s", $filename)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + $first = true; |
| 89 | + while ($line = \fgets($handle)) { |
| 90 | + $line = \trim($line); // Trim whitespaces (including end of line). |
| 91 | + |
| 92 | + if ($first) { |
| 93 | + $first = false; |
| 94 | + if ($options->getBool('file_skip_header', false)) { |
| 95 | + continue; // Skip header. |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (empty($line)) { |
| 100 | + continue; // Empty line, ignore. |
| 101 | + } |
| 102 | + |
| 103 | + yield $line; |
| 104 | + } |
| 105 | + } finally { |
| 106 | + if ($handle) { |
| 107 | + @\fclose($handle); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Iterator on CSV file contents. |
| 114 | + * |
| 115 | + * @return iterable<array<string>> |
| 116 | + */ |
| 117 | + public static function readCsvFile(string $filename, ?Options $options = null, ?string $anonymizerId = null): iterable |
| 118 | + { |
| 119 | + self::ensureFile($filename, $anonymizerId); |
| 120 | + |
| 121 | + $options ??= new Options(); |
| 122 | + |
| 123 | + $handle = null; |
| 124 | + try { |
| 125 | + $handle = \fopen($filename, 'r'); |
| 126 | + |
| 127 | + if (false === $handle) { |
| 128 | + if ($anonymizerId) { |
| 129 | + throw new ConfigurationException(\sprintf("Anonymizer '%s' could not open file: %s", $anonymizerId, $filename)); |
| 130 | + } else { |
| 131 | + throw new ConfigurationException(\sprintf("Could not open file: %s", $filename)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + $separator = $options->getString('file_csv_separator', ','); |
| 136 | + $enclosure = $options->getString('file_csv_enclosure', '"'); |
| 137 | + $escape = $options->getString('file_csv_escape', '\\'); |
| 138 | + |
| 139 | + $first = true; |
| 140 | + while ($line = \fgetcsv($handle, null, $separator, $enclosure, $escape)) { |
| 141 | + if ($first) { |
| 142 | + $first = false; |
| 143 | + if ($options->getBool('file_skip_header', false)) { |
| 144 | + continue; // Skip header. |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (!\array_filter($line)) { |
| 149 | + continue; // Empty line, ignore. |
| 150 | + } |
| 151 | + |
| 152 | + yield $line; |
| 153 | + } |
| 154 | + } finally { |
| 155 | + if ($handle) { |
| 156 | + @\fclose($handle); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public static function ensureFile(string $filename, ?string $anonymizerId = null): void |
| 162 | + { |
| 163 | + if (!\file_exists($filename)) { |
| 164 | + if ($anonymizerId) { |
| 165 | + throw new ConfigurationException(\sprintf("Anonymizer '%s' uses a non existing file: %s", $anonymizerId, $filename)); |
| 166 | + } else { |
| 167 | + throw new ConfigurationException(\sprintf("Uses a non existing file: %s", $filename)); |
| 168 | + } |
| 169 | + } |
| 170 | + if (!\is_file($filename)) { |
| 171 | + if ($anonymizerId) { |
| 172 | + throw new ConfigurationException(\sprintf("Anonymizer '%s' is not a regular file: %s", $anonymizerId, $filename)); |
| 173 | + } else { |
| 174 | + throw new ConfigurationException(\sprintf("Is not a regular file: %s", $filename)); |
| 175 | + } |
| 176 | + } |
| 177 | + if (!\is_readable($filename)) { |
| 178 | + if ($anonymizerId) { |
| 179 | + throw new ConfigurationException(\sprintf("Anonymizer '%s' file cannot be read: %s", $anonymizerId, $filename)); |
| 180 | + } else { |
| 181 | + throw new ConfigurationException(\sprintf("File cannot be read: %s", $filename)); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments