-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor major parts of the validation and rules structure for easier…
… 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
Showing
6 changed files
with
137 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.