Skip to content

Commit

Permalink
Refactor code to handle CSV Parser Config within Schema class (#145)
Browse files Browse the repository at this point in the history
The provided commit removes dependency on CsvParserConfig by
transferring its methods and properties to the Schema class. This move
centralizes configuration management, simplifying future maintenance.
The CsvParserConfig class was deleted as a consequence, and several
checks were updated to fetch data from the Schema instance directly, not
via CsvParserConfig. Tests were also updated to reflect these changes.
  • Loading branch information
SmetDenis committed Apr 4, 2024
1 parent a8087c2 commit a6c3545
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 199 deletions.
14 changes: 1 addition & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,11 @@ specifications, making it invaluable in scenarios where data quality and consist

<!-- auto-update:toc -->
- [Introduction](#introduction)
- [Why?](#why)
- [Features](#features)
- [Live demo](#live-demo)
- [Usage](#usage)
- [GitHub Action](#github-action)
- [Docker container](#docker-container)
- [Phar binary](#phar-binary)
- [Schema definition](#schema-definition)
- [Example Schema in YAML](#example-schema-in-yaml)
- [Full schema description](#full-schema-description)
- [Extra checks](#extra-checks)
- [Complete CLI help message](#complete-cli-help-message)
- [Report examples](#report-examples)
- [Benchmarks](#benchmarks)
- [Brief conclusions](#brief-conclusions)
- [Examples of CSV Files](#examples-of-csv-files)
- [Run benchmark locally](#run-benchmark-locally)
- [Disadvantages?](#disadvantages)
- [Coming soon](#coming-soon)
- [Contributing](#contributing)
Expand Down Expand Up @@ -246,7 +234,7 @@ The provided example illustrates a straightforward schema for a CSV file with a
column must not be empty and should only contain integer values. Additionally, the `name` column is required to have a
minimum length of 3 characters, ensuring basic data integrity and usefulness.

### Example Schema in YAML
### Example schema in YAML

<!-- auto-update:readme-sample-yml -->
```yml
Expand Down
19 changes: 6 additions & 13 deletions src/Csv/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
final class CsvFile
{
private string $csvFilename;
private CsvParserConfig $csvParserConfig;
private LeagueReader $reader;
private Schema $schema;
private bool $isEmpty;
Expand All @@ -43,7 +42,6 @@ public function __construct(string $csvFilename, null|array|string $csvSchemaFil
$this->csvFilename = $csvFilename;
$this->isEmpty = \filesize($this->csvFilename) <= 1;
$this->schema = new Schema($csvSchemaFilenameOrArray);
$this->csvParserConfig = $this->schema->getCsvParserConfig();
$this->reader = $this->prepareReader();
}

Expand All @@ -52,19 +50,14 @@ public function getCsvFilename(): string
return $this->csvFilename;
}

public function getCsvStructure(): CsvParserConfig
{
return $this->csvParserConfig;
}

/**
* @return string[]
*/
public function getHeader(): array
{
if ($this->header === null) {
$this->header = [];
if ($this->csvParserConfig->isHeader() && !$this->isEmpty) {
if ($this->schema->csvHasHeader() && !$this->isEmpty) {
// TODO: add handler for empty file
// League\Csv\SyntaxError : The header record does not exist or is empty at offset: `0
$this->header = $this->getRecordsChunk(0, 1)->first();
Expand Down Expand Up @@ -112,7 +105,7 @@ public function getSchema(): Schema
*/
public function getColumnsMappedByHeader(?ErrorSuite $errors = null): array
{
$isHeader = $this->schema->getCsvParserConfig()->isHeader();
$isHeader = $this->schema->csvHasHeader();

$map = [];
$errors ??= new ErrorSuite();
Expand Down Expand Up @@ -166,12 +159,12 @@ public function getColumnsMappedByHeaderNamesOnly(?ErrorSuite $errors = null): a
private function prepareReader(): LeagueReader
{
$reader = LeagueReader::createFromPath($this->csvFilename)
->setDelimiter($this->csvParserConfig->getDelimiter())
->setEnclosure($this->csvParserConfig->getEnclosure())
->setEscape($this->csvParserConfig->getQuoteChar())
->setDelimiter($this->schema->getCsvDelimiter())
->setEnclosure($this->schema->getCsvEnclosure())
->setEscape($this->schema->getCsvQuoteChar())
->setHeaderOffset(null); // It's important to set it to null to optimize memory usage!

if ($this->csvParserConfig->isBom()) {
if ($this->schema->csvHasBOM()) {
$reader->includeInputBOM();
} else {
$reader->skipInputBOM();
Expand Down
123 changes: 0 additions & 123 deletions src/Csv/CsvParserConfig.php

This file was deleted.

Loading

0 comments on commit a6c3545

Please sign in to comment.