/** * Validate that the file is correctly encoded in the provided charset. * * @throws CharsetException * @throws \Exception */ protected function validateEncoding() { $handle = fopen($this->filePath, 'r'); if (false === $handle) { throw new \Exception(sprintf('Unable to read the file "%s".', $this->filePath)); } $errors = []; $lineNo = 0; while (false !== ($line = fgets($handle)) && count($errors) < $this->maxErrors) { $lineNo++; if (false === iconv($this->charset, $this->charset, $line)) { $errors[] = $lineNo; } } fclose($handle); if (count($errors) > 0) { $message = count($errors) === $this->maxErrors ? sprintf('The first %s erroneous lines are %s.', $this->maxErrors, implode(', ', $errors)) : sprintf('The lines %s are erroneous.', implode(', ', $errors)); throw new CharsetException(sprintf('The file "%s" is not correctly encoded in %s. ', $this->filePath, $this->charset) . $message); } $this->stepExecution->addSummaryInfo('charset_validator.title', $this->charset . ' OK'); }