Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init #1

Merged
merged 25 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e0244cf
Init commit
Mar 10, 2024
163c9a9
Refactor UnitFacing rule to accept specific values. Add unit facing o…
Mar 10, 2024
871c935
Refactor CSV and schema file paths in the test cases.
Mar 10, 2024
a40d4cf
Refactor CSV structure definition and validation rules
Mar 10, 2024
14a7c6b
Refactor IsUuid4 class name and add test for IsUuid4 validator.
Mar 10, 2024
08c8596
Add CSV schema examples to README.md and update dependencies in compo…
Mar 10, 2024
a63e1d4
Add spoiler functionality to README.md
Mar 10, 2024
c3382b8
Update readme: Add more descriptive titles for examples and file form…
Mar 10, 2024
8dcaed9
Refactor CSV schema column rules' YAML and JSON representations
Mar 10, 2024
a7fc230
Refactor CSV validation methods
Mar 10, 2024
ae75c74
Refactor CsvFile constructor and add additional methods to ErrorSuite
Mar 10, 2024
bfcbcfa
Add support for rendering errors as a plain list.
Mar 10, 2024
e646c1b
Fix typo in file paths in MiscTest.php tests
Mar 10, 2024
8daa1fe
Validate CSV file by applying schema rules. If errors are found, disp…
Mar 10, 2024
94179dc
Refactor workflows to focus on demos
Mar 10, 2024
f0a9c13
Fix typo in Makefile command and update PHP version to 8.3 in workflo…
Mar 10, 2024
35001b8
Refactor workflow file for better readability and maintenance
Mar 10, 2024
c53f7bf
Refactor Makefile to add support for ANSI output in demo-github task
Mar 10, 2024
c57d003
Refactor makefile build command for consistent output
Mar 10, 2024
56edbc9
Update PHP array format in README and test case for PHP format in Blu…
Mar 10, 2024
ea82d7d
PHPStan fixed!
Mar 10, 2024
b20e4eb
PHPStan fixed!
Mar 10, 2024
8592314
Phan fixed!
Mar 10, 2024
36fa39e
Codestyle!
Mar 10, 2024
7e41368
Codestyle!
Mar 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor CSV and schema file paths in the test cases.
  • Loading branch information
Denis Smet committed Mar 10, 2024
commit 871c93572be1acd647b583644603d1d1a7461754
63 changes: 63 additions & 0 deletions src/Validators/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 Error
{
public function __construct(
private string $ruleCode,
private string $message,
private string $columnName,
private int $line = 0,
) {
}

public function __toString(): string
{
return "Error \"{$this->getRuleCode()}\" at line {$this->getLine()}, column \"{$this->getColumnName()}\". {$this->getMessage()}.";
}

public function getRuleCode(): string
{
return $this->ruleCode;
}

public function getMessage(): string
{
return \rtrim($this->message, '.');
}

public function getColumnName(): string
{
return $this->columnName;
}

public function getLine(): int
{
return $this->line;
}

public function toArray(): array
{
return [
'ruleCode' => $this->ruleCode,
'message' => $this->message,
'columnName' => $this->columnName,
'line' => $this->line,
];
}
}
7 changes: 3 additions & 4 deletions src/Validators/Rules/AbstarctRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace JBZoo\CsvBlueprint\Validators\Rules;

use JBZoo\CsvBlueprint\Utils;
use JBZoo\CsvBlueprint\Validators\Error;

use function JBZoo\Data\data;
use function JBZoo\Utils\bool;
Expand All @@ -39,12 +40,10 @@ public function __construct(?string $columnNameId, null|array|bool|float|int|str
$this->ruleCode = $this->getRuleCode();
}

public function validate(?string $cellValue, int $line = 0): ?string
public function validate(?string $cellValue, int $line = 0): ?Error
{
if ($error = $this->validateRule($cellValue)) {
$error = \rtrim($error, '.'); // Remove last dot to make the final message nice.

return "Error \"{$this->ruleCode}\" at line {$line}, column \"{$this->columnNameId}\". {$error}.";
return new Error($this->ruleCode, $error, $this->columnNameId, $line);
}

return null;
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsBool extends AllowValues
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

return parent::validateRule(\strtolower((string)$cellValue));
}

Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsDomain extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

$domainPattern = '/^(?!-)[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})$/';

if (!\preg_match($domainPattern, $cellValue)) {
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsEmail extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if (!\filter_var($cellValue, \FILTER_VALIDATE_EMAIL)) {
return "Value \"{$cellValue}\" is not a valid email";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class IsFloat extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if (!\preg_match('/^-?\d+(\.\d+)?$/', $cellValue)) {
return "Value \"{$cellValue}\" is not a float number";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsInt extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if (!\preg_match('/^-?\d+$/', $cellValue)) {
return "Value \"{$cellValue}\" is not an integer";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsIp.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsIp extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if (!\filter_var($cellValue, \FILTER_VALIDATE_IP)) {
return "Value \"{$cellValue}\" is not a valid IP";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsLatitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsLatitude extends IsFloat
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

$result = parent::validateRule($cellValue);
if ($result) {
return $result;
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsLongitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsLongitude extends IsFloat
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

$result = parent::validateRule($cellValue);
if ($result) {
return $result;
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/IsUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class IsUrl extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if (!\filter_var($cellValue, \FILTER_VALIDATE_URL)) {
return "Value \"{$cellValue}\" is not a valid URL";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class NotEmpty extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if ($cellValue === null || $cellValue === '') {
return 'Value is empty';
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/OnlyCapitalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class OnlyCapitalize extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if ($cellValue !== null && $cellValue !== \ucfirst($cellValue)) {
return "Value \"{$cellValue}\" should be in capitalize";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/OnlyLowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class OnlyLowercase extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if ($cellValue !== null && $cellValue !== \mb_strtolower($cellValue)) {
return "Value \"{$cellValue}\" should be in lowercase";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/OnlyTrimed.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class OnlyTrimed extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if (\trim($cellValue) !== $cellValue) {
return "Value \"{$cellValue}\" is not trimmed";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/OnlyUppercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class OnlyUppercase extends AbstarctRule
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if ($cellValue !== null && \mb_strtoupper($cellValue, 'UTF-8') !== $cellValue) {
return "Value \"{$cellValue}\" is not uppercase";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validators/Rules/UnitFacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class UnitFacing extends AllowValues
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

return parent::validateRule((string)$cellValue);
}

Expand Down
34 changes: 34 additions & 0 deletions src/Validators/Rules/UsaMarketName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Rules;

class UsaMarketName extends AllowValues
{
public function validateRule(?string $cellValue): ?string
{
if (!$this->getOptionAsBool()) {
return null;
}

if (!\preg_match('/^[A-Za-z0-9\s-]+, [A-Z]{2}$/u', $cellValue)) {
return 'Invalid market name format for value "' . $cellValue . '". ' .
'Market name must have format "New York, NY"';
}

return null;
}
}
3 changes: 3 additions & 0 deletions src/Validators/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function __construct(Column $column)
$this->ruleset = new Ruleset($column->getRules(), $column->getHumanName());
}

/**
* @return Error[]
*/
public function validate(?string $cellValue, int $line): array
{
return $this->ruleset->validate($cellValue, $line);
Expand Down
13 changes: 8 additions & 5 deletions tests/CsvReaderTest.php → tests/Blueprint/CsvReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@

declare(strict_types=1);

namespace JBZoo\PHPUnit;
namespace JBZoo\PHPUnit\Blueprint;

use JBZoo\CsvBlueprint\Csv\CsvFile;
use JBZoo\PHPUnit\PHPUnit;

use function JBZoo\PHPUnit\isSame;

final class CsvReaderTest extends PHPUnit
{
private const CSV_SIMPLE_HEADER = __DIR__ . '/fixtures/simple_header.csv';
private const CSV_SIMPLE_NO_HEADER = __DIR__ . '/fixtures/simple_no_header.csv';
private const CSV_SIMPLE_HEADER = PROJECT_TESTS . '/fixtures/simple_header.csv';
private const CSV_SIMPLE_NO_HEADER = PROJECT_TESTS . '/fixtures/simple_no_header.csv';

private const SCHEMA_SIMPLE_HEADER = __DIR__ . '/schemas/simple_header.yml';
private const SCHEMA_SIMPLE_NO_HEADER = __DIR__ . '/schemas/simple_no_header.yml';
private const SCHEMA_SIMPLE_HEADER = PROJECT_TESTS . '/schemas/simple_header.yml';
private const SCHEMA_SIMPLE_NO_HEADER = PROJECT_TESTS . '/schemas/simple_no_header.yml';

public function testReadCsvFileWithoutHeader(): void
{
Expand Down
Loading