-
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.
Add new string and word count validation rules
Introduced new validation rules related to string content and word count. The changes include rules for word counting, string beginning and ending content, and required content presence in strings. These modifications expand the ability of the library to perform more intricate validations, aiding in ensuring data consistency and accuracy in CSV files.
- Loading branch information
Denis Smet
committed
Mar 13, 2024
1 parent
1ec2368
commit 5f4c4ac
Showing
13 changed files
with
431 additions
and
140 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 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 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,37 @@ | ||
<?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\Rules; | ||
|
||
final class AllMustContain extends AbstarctRule | ||
{ | ||
public function validateRule(?string $cellValue): ?string | ||
{ | ||
$inclusions = $this->getOptionAsArray(); | ||
if (\count($inclusions) === 0) { | ||
return null; | ||
} | ||
|
||
foreach ($inclusions as $inclusion) { | ||
if (\strpos((string)$cellValue, (string)$inclusion) === false) { | ||
return "Value \"<c>{$cellValue}</c>\" must contain all of the following:" . | ||
' "<green>["' . \implode('", "', $inclusions) . '"]</green>"'; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,37 @@ | ||
<?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\Rules; | ||
|
||
final class AtLeastContains extends AbstarctRule | ||
{ | ||
public function validateRule(?string $cellValue): ?string | ||
{ | ||
$inclusions = $this->getOptionAsArray(); | ||
if (\count($inclusions) === 0) { | ||
return null; | ||
} | ||
|
||
foreach ($inclusions as $inclusion) { | ||
if (\strpos((string)$cellValue, (string)$inclusion) !== false) { | ||
return null; | ||
} | ||
} | ||
|
||
return "Value \"<c>{$cellValue}</c>\" must contain one of the following:" . | ||
' "<green>["' . \implode('", "', $inclusions) . '"]</green>"'; | ||
} | ||
} |
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,33 @@ | ||
<?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\Rules; | ||
|
||
final class MaxWordCount extends AbstarctRule | ||
{ | ||
public function validateRule(?string $cellValue): ?string | ||
{ | ||
$wordCount = $this->getOptionAsInt(); | ||
$count = \str_word_count((string)$cellValue); | ||
|
||
if ($count > $wordCount) { | ||
return "Value \"<c>{$cellValue}</c>\" has {$count} words, " . | ||
"but must have no more than <green>{$wordCount}</green> words"; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,33 @@ | ||
<?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\Rules; | ||
|
||
final class MinWordCount extends AbstarctRule | ||
{ | ||
public function validateRule(?string $cellValue): ?string | ||
{ | ||
$wordCount = $this->getOptionAsInt(); | ||
$count = \str_word_count((string)$cellValue); | ||
|
||
if ($count < $wordCount) { | ||
return "Value \"<c>{$cellValue}</c>\" has {$count} words, " . | ||
"but must have at least <green>{$wordCount}</green> words"; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,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\Rules; | ||
|
||
final class StrEndsWith extends AbstarctRule | ||
{ | ||
public function validateRule(?string $cellValue): ?string | ||
{ | ||
$prefix = $this->getOptionAsString(); | ||
if ($prefix === '') { | ||
return null; | ||
} | ||
|
||
if (!\str_ends_with((string)$cellValue, $prefix)) { | ||
return "Value \"<c>{$cellValue}</c>\" must end with \"<green>{$prefix}</green>\""; | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.