Skip to content

Commit

Permalink
Refactor major parts of the validation and rules structure for easier…
Browse files Browse the repository at this point in the history
… maintainability and clarity.

The validation code in `CsvFile` has been refactored to a new class, `CsvValidator`, for better encapsulation and readability. The use statements in `CsvFile` and `Column` classes have been updated to reflect these changes. The `AbstractValidator` has been removed for simplicity, hence, `ColumnValidator` and `CsvValidator` classes no longer extend it. Furthermore, a typo in the `CsvValidator` filename was corrected. The commit aims to make the validation process easier to maintain and understand.
  • Loading branch information
Denis Smet committed Mar 13, 2024
1 parent a5fb85f commit e35d8a5
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/Csv/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
namespace JBZoo\CsvBlueprint\Csv;

use JBZoo\CsvBlueprint\Utils;
use JBZoo\CsvBlueprint\Validators\ErrorSuite;
use JBZoo\CsvBlueprint\Validators\ColumnValidator;
use JBZoo\CsvBlueprint\Validators\ErrorSuite;
use JBZoo\Data\Data;

final class Column
Expand Down
104 changes: 2 additions & 102 deletions src/Csv/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
namespace JBZoo\CsvBlueprint\Csv;

use JBZoo\CsvBlueprint\Schema;
use JBZoo\CsvBlueprint\Utils;
use JBZoo\CsvBlueprint\Validators\Error;
use JBZoo\CsvBlueprint\Validators\CsvValidator;
use JBZoo\CsvBlueprint\Validators\ErrorSuite;
use League\Csv\Reader as LeagueReader;
use League\Csv\Statement;
Expand Down Expand Up @@ -81,17 +80,7 @@ public function getRecordsChunk(int $offset = 0, int $limit = -1): TabularDataRe

public function validate(bool $quickStop = false): ErrorSuite
{
// $fileValidator

$errors = new ErrorSuite($this->getCsvFilename());

$errors
->addErrorSuit($this->validateFile($quickStop))
->addErrorSuit($this->validateHeader($quickStop))
->addErrorSuit($this->validateEachCell($quickStop))
->addErrorSuit(self::validateAggregateRules($quickStop));

return $errors;
return (new CsvValidator($this, $this->schema))->validate($quickStop);
}

private function prepareReader(): LeagueReader
Expand All @@ -110,93 +99,4 @@ private function prepareReader(): LeagueReader

return $reader;
}

private function validateHeader(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

if (!$this->getCsvStructure()->isHeader()) {
return $errors;
}

foreach ($this->schema->getColumns() as $column) {
if ($column->getName() === '') {
$error = new Error(
'csv.header',
"Property \"<c>name</c>\" is not defined in schema: \"<c>{$this->schema->getFilename()}</c>\"",
$column->getHumanName(),
1,
);

$errors->addError($error);
}

if ($quickStop && $errors->count() > 0) {
return $errors;
}
}

return $errors;
}

private function validateEachCell(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

foreach ($this->getRecords() as $line => $record) {
$columns = $this->schema->getColumnsMappedByHeader($this->getHeader());

foreach ($columns as $column) {
if ($column === null) {
continue;
}

$errors->addErrorSuit($column->validate($record[$column->getKey()], (int)$line + 1));
if ($quickStop && $errors->count() > 0) {
return $errors;
}
}
}

return $errors;
}

private function validateFile(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

$filenamePattern = $this->schema->getFilenamePattern();
if (
$filenamePattern !== null
&& $filenamePattern !== ''
&& \preg_match($filenamePattern, $this->csvFilename) === 0
) {
$error = new Error(
'filename_pattern',
'Filename "<c>' . Utils::cutPath($this->csvFilename) .
"</c>\" does not match pattern: \"<c>{$filenamePattern}</c>\"",
'',
0,
);

$errors->addError($error);

if ($quickStop && $errors->count() > 0) {
return $errors;
}
}

return $errors;
}

private static function validateAggregateRules(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

if ($quickStop && $errors->count() > 0) {
return $errors;
}

return new ErrorSuite();
}
}
22 changes: 0 additions & 22 deletions src/Validators/AbstractValidator.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Validators/ColumnValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use JBZoo\CsvBlueprint\Csv\Column;

final class ColumnValidator extends AbstractValidator
final class ColumnValidator
{
private Ruleset $ruleset;

Expand Down
133 changes: 133 additions & 0 deletions src/Validators/CsvValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators;

use JBZoo\CsvBlueprint\Csv\CsvFile;
use JBZoo\CsvBlueprint\Schema;
use JBZoo\CsvBlueprint\Utils;

final class CsvValidator
{
private CsvFile $csv;
private ErrorSuite $errors;
private Schema $schema;

public function __construct(CsvFile $csv, Schema $schema)
{
$this->csv = $csv;
$this->schema = $schema;
$this->errors = new ErrorSuite($this->csv->getCsvFilename());
}

public function validate(bool $quickStop = false): ErrorSuite
{
return $this->errors
->addErrorSuit($this->validateFile($quickStop))
->addErrorSuit($this->validateHeader($quickStop))
->addErrorSuit($this->validateEachCell($quickStop))
->addErrorSuit(self::validateAggregateRules($quickStop));
}

private function validateHeader(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

if (!$this->schema->getCsvStructure()->isHeader()) {
return $errors;
}

foreach ($this->schema->getColumns() as $column) {
if ($column->getName() === '') {
$error = new Error(
'csv.header',
"Property \"<c>name</c>\" is not defined in schema: \"<c>{$this->schema->getFilename()}</c>\"",
$column->getHumanName(),
1,
);

$errors->addError($error);
}

if ($quickStop && $errors->count() > 0) {
return $errors;
}
}

return $errors;
}

private function validateEachCell(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

foreach ($this->csv->getRecords() as $line => $record) {
$columns = $this->schema->getColumnsMappedByHeader($this->csv->getHeader());

foreach ($columns as $column) {
if ($column === null) {
continue;
}

$errors->addErrorSuit($column->validate($record[$column->getKey()], (int)$line + 1));
if ($quickStop && $errors->count() > 0) {
return $errors;
}
}
}

return $errors;
}

private function validateFile(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

$filenamePattern = $this->schema->getFilenamePattern();
if (
$filenamePattern !== null
&& $filenamePattern !== ''
&& \preg_match($filenamePattern, $this->csv->getCsvFilename()) === 0
) {
$error = new Error(
'filename_pattern',
'Filename "<c>' . Utils::cutPath($this->csv->getCsvFilename()) .
"</c>\" does not match pattern: \"<c>{$filenamePattern}</c>\"",
'',
0,
);

$errors->addError($error);

if ($quickStop && $errors->count() > 0) {
return $errors;
}
}

return $errors;
}

private static function validateAggregateRules(bool $quickStop = false): ErrorSuite
{
$errors = new ErrorSuite();

if ($quickStop && $errors->count() > 0) {
return $errors;
}

return new ErrorSuite();
}
}
25 changes: 0 additions & 25 deletions src/Validators/ScvValidator.php

This file was deleted.

0 comments on commit e35d8a5

Please sign in to comment.