Skip to content

Commit

Permalink
Refactor Validators and Rules structure
Browse files Browse the repository at this point in the history
Reorganized and refactored the architecture of Validators and Rules within the codebase. The Rules were moved under the base namespace rather than under Validators. Additionally, implemented the ColumnValidator and ScvValidator, and refactored the abstract base Validator class into AbstractValidator. Adjustments were also applied to unit tests receiving the impact of these changes, ensuring comprehensive test coverage over the refactored structure.
  • Loading branch information
Denis Smet committed Mar 13, 2024
1 parent 42a5778 commit a5fb85f
Show file tree
Hide file tree
Showing 39 changed files with 113 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/Csv/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

final class Column
Expand Down Expand Up @@ -111,7 +111,7 @@ public function getInherit(): string

public function validate(string $cellValue, int $line): ErrorSuite
{
return (new Validator($this))->validate($cellValue, $line);
return (new ColumnValidator($this))->validate($cellValue, $line);
}

private function prepareRuleSet(string $schemaKey): array
Expand Down
2 changes: 2 additions & 0 deletions src/Csv/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function getRecordsChunk(int $offset = 0, int $limit = -1): TabularDataRe

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

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

$errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

use JBZoo\CsvBlueprint\Utils;
use JBZoo\CsvBlueprint\Validators\Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

class AllowValues extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

class CardinalDirection extends AllowValues
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class DateFormat extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class ExactValue extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsBool.php → src/Rules/IsBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsBool extends AllowValues
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsDomain extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsEmail.php → src/Rules/IsEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsEmail extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsFloat.php → src/Rules/IsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

class IsFloat extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsInt.php → src/Rules/IsInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsInt extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsIp.php → src/Rules/IsIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsIp extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsLatitude extends IsFloat
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsLongitude extends IsFloat
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsUrl.php → src/Rules/IsUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsUrl extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/IsUuid4.php → src/Rules/IsUuid4.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class IsUuid4 extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/Max.php → src/Rules/Max.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class Max extends IsFloat
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/MaxDate.php → src/Rules/MaxDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class MaxDate extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class MaxLength extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/Min.php → src/Rules/Min.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class Min extends IsFloat
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/MinDate.php → src/Rules/MinDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class MinDate extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class MinLength extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class NotEmpty extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class OnlyCapitalize extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class OnlyLowercase extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class OnlyTrimed extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class OnlyUppercase extends AbstarctRule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

final class Precision extends AbstarctRule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/Rules/Regex.php → src/Rules/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

use JBZoo\CsvBlueprint\Utils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

class RuleException extends \JBZoo\CsvBlueprint\Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

declare(strict_types=1);

namespace JBZoo\CsvBlueprint\Validators\Rules;
namespace JBZoo\CsvBlueprint\Rules;

class UsaMarketName extends AllowValues
{
Expand Down
22 changes: 22 additions & 0 deletions src/Validators/AbstractValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;

abstract class AbstractValidator
{
abstract public function validate(?string $cellValue, int $line): ErrorSuite;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use JBZoo\CsvBlueprint\Csv\Column;

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

Expand Down
4 changes: 2 additions & 2 deletions src/Validators/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

namespace JBZoo\CsvBlueprint\Validators;

use JBZoo\CsvBlueprint\Rules\AbstarctRule;
use JBZoo\CsvBlueprint\Utils;
use JBZoo\CsvBlueprint\Validators\Rules\AbstarctRule;

final class Ruleset
{
Expand All @@ -41,7 +41,7 @@ public function __construct(array $rules, string $columnNameId)
*/
public function createRule(string $ruleName, null|array|bool|float|int|string $options = null): AbstarctRule
{
$classname = __NAMESPACE__ . '\\Rules\\' . Utils::kebabToCamelCase($ruleName);
$classname = '\\JBZoo\\CsvBlueprint\\Rules\\' . Utils::kebabToCamelCase($ruleName);
if (\class_exists($classname)) {
// @phpstan-ignore-next-line
return new $classname($this->columnNameId, $options);
Expand Down
25 changes: 25 additions & 0 deletions src/Validators/ScvValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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;

final class ScvValidator extends AbstractValidator
{
public function validate(?string $cellValue, int $line): ErrorSuite
{
return new ErrorSuite();
}
}
2 changes: 1 addition & 1 deletion tests/Blueprint/MiscTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testFullListOfRules(): void

$finder = (new Finder())
->files()
->in(PROJECT_ROOT . '/src/Validators/Rules')
->in(PROJECT_ROOT . '/src/Rules')
->ignoreDotFiles(false)
->ignoreVCS(true)
->name('/\\.php$/');
Expand Down
Loading

0 comments on commit a5fb85f

Please sign in to comment.