-
Notifications
You must be signed in to change notification settings - Fork 7
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
Extended validators to allow array values, dependent field validations and added new rules #22
Open
xanish
wants to merge
21
commits into
hoshomoh:master
Choose a base branch
from
xanish:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
70d3cdd
Added support for array of values in rule parameters
xanish b8a850b
Added support for dependent field validations
xanish 1eb7008
Added required validation rule
xanish ad4a98e
Added integer validation rule
xanish f6b2274
Added numeric validation rule
xanish 993f57f
Added alpha_num validation rule
xanish ec6fb50
Updated url rule to pass for empty values
xanish 08cdb6a
Added in validation rule
xanish 12dcb06
Added required_if validation rule
xanish a283ae7
Added new and updated existing rule test cases
xanish d09b6ac
Added alpha validation rule
xanish 5baaf6f
Fixed lint
xanish 5465a2b
Fixed code-coverage for test cases
xanish 042a76c
Created min and max rules. Generalised min, max and between rules to …
xanish 3cad48f
Updated validation rule docs
xanish 4d1a497
Added optional trimming to csv cells
xanish e818396
Removed generalised min, max and between functionality for string and…
xanish c8d297f
Added min and max length validators
xanish aaf5e8b
Added typecasting for values in min, max and between rules
xanish 6c96eaf
Updated docs for min, max, min_length and max_length rules
xanish 0045669
Rename enableCellValueTrim to setShouldTrim
xanish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oshomo\CsvUtils\Contracts; | ||
|
||
interface ArrayParameterizedRuleInterface | ||
{ | ||
/** | ||
* Should return an array of the allowed parameters. | ||
* The allowed parameters should be tokenized string | ||
* e.g :min, :max, :first, :last etc. | ||
*/ | ||
public function allowedParameters(): array; | ||
|
||
/** | ||
* Should return an array of parameter values as strings | ||
* parsed in the same order and format as allowedParameters(). | ||
* This will aid in mapping our parameters to their placeholders. | ||
*/ | ||
public function parseParameterValues(array $parameters): array; | ||
} |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Oshomo\CsvUtils\Helpers; | ||
|
||
use Oshomo\CsvUtils\Contracts\ArrayParameterizedRuleInterface; | ||
use Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface; | ||
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; | ||
|
||
|
@@ -88,6 +89,14 @@ protected function makeReplacements( | |
); | ||
} | ||
|
||
if ($rule instanceof ArrayParameterizedRuleInterface) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment above |
||
$message = $this->replaceParameterPlaceholder( | ||
$message, | ||
$rule->allowedParameters(), | ||
$rule->parseParameterValues($parameters) | ||
); | ||
} | ||
|
||
$message = $this->replaceValuePlaceholder($message, $value); | ||
|
||
$message = $this->replaceErrorLinePlaceholder($message, $lineNumber); | ||
|
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oshomo\CsvUtils\Rules; | ||
|
||
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; | ||
|
||
class Alpha implements ValidationRuleInterface | ||
{ | ||
/** | ||
* Determines if the validation rule passes. This is where we do the | ||
* actual validation. If the validation passes return true else false. | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function passes($value, array $parameters, array $row): bool | ||
{ | ||
if (null === $value || '' === $value) { | ||
return true; | ||
} | ||
|
||
return ctype_alpha($value); | ||
} | ||
|
||
/** | ||
* Get the validation error message. Specify the message that should | ||
* be returned if the validation fails. You can make use of the | ||
* :attribute and :value placeholders in the message string. | ||
*/ | ||
public function message(): string | ||
{ | ||
return 'The :attribute value :value may only contain letters on line :line.'; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oshomo\CsvUtils\Rules; | ||
|
||
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; | ||
|
||
class AlphaNum implements ValidationRuleInterface | ||
{ | ||
/** | ||
* Determines if the validation rule passes. This is where we do the | ||
* actual validation. If the validation passes return true else false. | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function passes($value, array $parameters, array $row): bool | ||
{ | ||
if (null === $value || '' === $value) { | ||
return true; | ||
} | ||
|
||
return ctype_alnum($value); | ||
} | ||
|
||
/** | ||
* Get the validation error message. Specify the message that should | ||
* be returned if the validation fails. You can make use of the | ||
* :attribute and :value placeholders in the message string. | ||
*/ | ||
public function message(): string | ||
{ | ||
return 'The :attribute value :value may only contain letters and numbers on line :line.'; | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oshomo\CsvUtils\Rules; | ||
|
||
use Oshomo\CsvUtils\Contracts\ArrayParameterizedRuleInterface; | ||
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; | ||
|
||
class In implements ValidationRuleInterface, ArrayParameterizedRuleInterface | ||
{ | ||
/** | ||
* Determines if the validation rule passes. This is where we do the | ||
* actual validation. If the validation passes return true else false. | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function passes($value, array $parameters, array $row): bool | ||
{ | ||
if (null === $value || '' === $value) { | ||
return true; | ||
} | ||
|
||
return in_array($value, $parameters); | ||
} | ||
|
||
/** | ||
* Get the validation error message. Specify the message that should | ||
* be returned if the validation fails. You can make use of the | ||
* :attribute and :value placeholders in the message string. | ||
*/ | ||
public function message(): string | ||
{ | ||
return 'The :attribute value :value does not exist in :allowed_values on line :line.'; | ||
} | ||
|
||
/** | ||
* Should return an array of the allowed parameters. | ||
* The allowed parameters should be tokenized string | ||
* e.g :min, :max, :first, :last etc. | ||
*/ | ||
public function allowedParameters(): array | ||
{ | ||
// all parameter values passed to the in rule will | ||
// get mapped as allowed_values | ||
// example in:1,2,3,4 | ||
// the values [1, 2, 3, 4] will get mapped to :allowed_values | ||
// while checking for passes() and while writing message | ||
// using parseParameterValues() | ||
return [':allowed_values']; | ||
} | ||
|
||
/** | ||
* Should return an array of parameter values as strings | ||
* parsed in the same order and format as allowedParameters(). | ||
* This will aid in mapping our parameters to their placeholders. | ||
*/ | ||
public function parseParameterValues(array $parameters): array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment above |
||
{ | ||
// make sure to convert the values to an imploded string | ||
// this will then get mapped to array index 0 which has | ||
// the placeholder for :allowed_values | ||
return [implode(',', $parameters)]; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oshomo\CsvUtils\Rules; | ||
|
||
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; | ||
|
||
class Integer implements ValidationRuleInterface | ||
{ | ||
/** | ||
* Determines if the validation rule passes. This is where we do the | ||
* actual validation. If the validation passes return true else false. | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function passes($value, array $parameters, array $row): bool | ||
{ | ||
if (null === $value || '' === $value) { | ||
return true; | ||
} | ||
|
||
return ctype_digit((string) $value); | ||
} | ||
|
||
/** | ||
* Get the validation error message. Specify the message that should | ||
* be returned if the validation fails. You can make use of the | ||
* :attribute and :value placeholders in the message string. | ||
*/ | ||
public function message(): string | ||
{ | ||
return 'The :attribute value :value must be an integer on line :line.'; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Oshomo\CsvUtils\Rules; | ||
|
||
use Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface; | ||
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; | ||
|
||
class Max implements ValidationRuleInterface, ParameterizedRuleInterface | ||
{ | ||
public function allowedParameters(): array | ||
{ | ||
return [':max']; | ||
} | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param mixed $value | ||
*/ | ||
public function passes($value, array $parameters, array $row): bool | ||
{ | ||
list($max) = $parameters; | ||
|
||
return +$value <= +$max; | ||
hoshomoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
*/ | ||
public function message(): string | ||
{ | ||
return 'The :attribute value :value may not be greater than :max on line :line.'; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is only useful when there a single parameter is allowed. Instead of doing this, I would rather update
CSVUtils/src/Helpers/FormatsMessages.php
Line 101 in 34afb02
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this works as well. I just wanted to keep it open in case someone needed multiple parameters.