-
Couldn't load subscription status.
- Fork 8
Extended validators to allow array values, dependent field validations and added new rules #22
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
base: master
Are you sure you want to change the base?
Changes from 20 commits
70d3cdd
b8a850b
1eb7008
ad4a98e
f6b2274
993f57f
ec6fb50
08cdb6a
12dcb06
a283ae7
d09b6ac
5baaf6f
5465a2b
042a76c
3cad48f
4d1a497
e818396
c8d297f
aaf5e8b
6c96eaf
0045669
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
| 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); | ||
|
|
||
| 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.'; | ||
| } | ||
| } |
| 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.'; | ||
| } | ||
| } |
| 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)]; | ||
| } | ||
| } | ||
| 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.'; | ||
| } | ||
| } |
| 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.'; | ||
| } | ||
| } | ||
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.