From da8521068a57b9e74b838c0ca25183571399b112 Mon Sep 17 00:00:00 2001 From: Oforomeh Oshomo Date: Thu, 26 Mar 2020 22:05:45 +0100 Subject: [PATCH] Rule parameter clean up (#17) * Created a new interface for parameterized rules * Removed getParameterCount from validatorInterface * Removed getParameterCount from all rules that don't need to implement it. * Added documentation for ParameterizedInterface --- .travis.yml | 1 + README.md | 49 ++++---------------- src/Contracts/ParameterizedRuleInterface.php | 15 ++++++ src/Contracts/ValidationRuleInterface.php | 20 -------- src/Helpers/FormatsMessages.php | 22 ++++++++- src/Rules/AsciiOnly.php | 16 ------- src/Rules/Between.php | 24 +++------- src/Rules/ClosureValidationRule.php | 16 ------- src/Rules/Url.php | 16 ------- src/Validator/Validator.php | 14 ++++-- tests/src/Rules/ClosureValidationTest.php | 20 -------- tests/src/UppercaseRule.php | 10 ---- 12 files changed, 62 insertions(+), 161 deletions(-) create mode 100644 src/Contracts/ParameterizedRuleInterface.php delete mode 100644 tests/src/Rules/ClosureValidationTest.php diff --git a/.travis.yml b/.travis.yml index 00caa32..1d11767 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: php php: - 7.1 - 7.2 + - 7.3 - nightly matrix: diff --git a/README.md b/README.md index d22573b..9fbcc9b 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ $validator = new Validator("some/valid/file_path", ",", [ if ($validator->fails()) { // Do something when validation fails - $errors = $validator->errors() + $errors = $validator->errors(); } ``` @@ -182,19 +182,6 @@ use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; class UppercaseRule implements ValidationRuleInterface { - - /** - * Get the number of parameters that should be supplied. - * If no parameter should be supplied return 0 else - * return the number of parameters that should be returned - * - * @return integer - */ - public function parameterCount() - { - return 0; - } - /** * Determines if the validation rule passes. This is where we do the * actual validation. If the validation passes return true else false @@ -203,7 +190,7 @@ class UppercaseRule implements ValidationRuleInterface * @param $parameters * @return bool */ - public function passes($value, $parameters) + public function passes($value, array $parameters): bool { return strtoupper($value) === $value; } @@ -215,34 +202,16 @@ class UppercaseRule implements ValidationRuleInterface * * @return string */ - public function message() + public function message(): string { return "The :attribute value :value must be uppercase on line :line."; } - - /** - * Replace error messages parameter with right values. If you want - * to allow user pass custom placeholders in the inline message - * specify and replace them here. If not just return $message - * i.e return $message. But if you want to allow custom placeholder - * return str_replace( - * [':custom_a', ':custom_b'], - * [$parameters[0], $parameters[1]], - * $message - * ); - * - * @param string $message - * @param array $parameters - * @return string - */ - public function parameterReplacer($message, $parameters) - { - return $message; - } } ``` +If the CustomRule accepts parameters like the `between` rule, then your CustomRule class must implement both `Oshomo\CsvUtils\Contracts\ValidationRuleInterface` and `Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface`. See `Oshomo\CsvUtils\Rules\Between` as an example. + ##### Passing Custom Rules to Validator Using Closure If you only need the functionality of a custom rule once throughout your application, you may use a Closure instead of a rule object. The Closure receives the attribute's value, and a `$fail` callback that should be called if validation fails: @@ -280,16 +249,16 @@ class JsonConverter implements ConverterHandlerInterface /** * @return string */ - public function getExtension() + public function getExtension(): string { - return self::FILE_EXTENSION; + return JsonConverter::FILE_EXTENSION; } /** * @param array $data * @return $this|mixed */ - public function convert($data) + public function convert(array $data): ConverterHandlerInterface { $this->data = json_encode($data, JSON_PRETTY_PRINT | @@ -305,7 +274,7 @@ class JsonConverter implements ConverterHandlerInterface * @param string $filename * @return bool */ - public function write($filename) + public function write(string $filename): bool { return (file_put_contents($filename, $this->data)) ? true : false; } diff --git a/src/Contracts/ParameterizedRuleInterface.php b/src/Contracts/ParameterizedRuleInterface.php new file mode 100644 index 0000000..73bb58c --- /dev/null +++ b/src/Contracts/ParameterizedRuleInterface.php @@ -0,0 +1,15 @@ +replaceAttributePlaceholder($message, $attribute); + if ($rule instanceof ParameterizedRuleInterface) { + $message = $this->replaceParameterPlaceholder( + $message, + $rule->allowedParameters(), + $parameters + ); + } + $message = $this->replaceValuePlaceholder($message, $value); $message = $this->replaceErrorLinePlaceholder($message, $lineNumber); - $message = $rule->parameterReplacer($message, $parameters); - return $message; } + /** + * Replace the rule parameters placeholder in the given message. + */ + protected function replaceParameterPlaceholder( + string $message, + array $allowedParameters, + array $parameters + ): string { + return str_replace($allowedParameters, $parameters, $message); + } + /** * Replace the :attribute placeholder in the given message. */ diff --git a/src/Rules/AsciiOnly.php b/src/Rules/AsciiOnly.php index 1c9d83f..a811a59 100644 --- a/src/Rules/AsciiOnly.php +++ b/src/Rules/AsciiOnly.php @@ -8,14 +8,6 @@ class AsciiOnly implements ValidationRuleInterface { - /** - * Get the number of parameters that should be supplied. - */ - public function parameterCount(): int - { - return 0; - } - /** * Determine if the validation rule passes. * @@ -33,12 +25,4 @@ public function message(): string { return 'The :attribute value :value contains a non-ascii character on line :line.'; } - - /** - * Replace error messages parameter with right values. - */ - public function parameterReplacer(string $message, array $parameters): string - { - return $message; - } } diff --git a/src/Rules/Between.php b/src/Rules/Between.php index 3bb5baa..df7151c 100644 --- a/src/Rules/Between.php +++ b/src/Rules/Between.php @@ -4,18 +4,14 @@ namespace Oshomo\CsvUtils\Rules; +use Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface; use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; -class Between implements ValidationRuleInterface +class Between implements ValidationRuleInterface, ParameterizedRuleInterface { - const PARAMETER_COUNT = 2; - - /** - * Get the number of parameters that should be supplied. - */ - public function parameterCount(): int + public function allowedParameters(): array { - return self::PARAMETER_COUNT; + return [':min', ':max']; } /** @@ -27,7 +23,9 @@ public function passes($value, array $parameters): bool { $size = (int) $value; - return $size >= (int) $parameters[0] && $size <= (int) $parameters[1]; + list($min, $max) = $parameters; + + return $size >= (int) $min && $size <= (int) $max; } /** @@ -37,12 +35,4 @@ public function message(): string { return 'The :attribute value :value is not between :min - :max on line :line.'; } - - /** - * Replace error messages parameter with right values. - */ - public function parameterReplacer(string $message, array $parameters): string - { - return str_replace([':min', ':max'], [$parameters[0], $parameters[1]], $message); - } } diff --git a/src/Rules/ClosureValidationRule.php b/src/Rules/ClosureValidationRule.php index 801d2fe..f54b9f7 100755 --- a/src/Rules/ClosureValidationRule.php +++ b/src/Rules/ClosureValidationRule.php @@ -37,14 +37,6 @@ public function __construct(\Closure $callback) $this->callback = $callback; } - /** - * Get the number of parameters that should be supplied. - */ - public function parameterCount(): int - { - return 0; - } - /** * Determine if the validation rule passes. * @@ -70,12 +62,4 @@ public function message(): string { return $this->message; } - - /** - * Replace error messages parameter with right values. - */ - public function parameterReplacer(string $message, array $parameters): string - { - return $message; - } } diff --git a/src/Rules/Url.php b/src/Rules/Url.php index fbffe97..6ba2196 100644 --- a/src/Rules/Url.php +++ b/src/Rules/Url.php @@ -26,14 +26,6 @@ class Url implements ValidationRuleInterface (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional) $~ixu'; - /** - * Get the number of parameters that should be supplied. - */ - public function parameterCount(): int - { - return 0; - } - /** * Determine if the validation rule passes. * @@ -64,12 +56,4 @@ public function message(): string { return 'The :attribute value :value is not a valid url on line :line.'; } - - /** - * Replace error messages parameter with right values. - */ - public function parameterReplacer(string $message, array $parameters): string - { - return $message; - } } diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index f1feabe..5f8dbe2 100755 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -5,6 +5,7 @@ namespace Oshomo\CsvUtils\Validator; use Oshomo\CsvUtils\Contracts\ConverterHandlerInterface; +use Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface; use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; use Oshomo\CsvUtils\Contracts\ValidationRuleInterface as ValidationRule; use Oshomo\CsvUtils\Helpers\FormatsMessages; @@ -349,10 +350,14 @@ protected function passesParameterCheck($rule, array $parameters): bool $rule = $this->getRuleClass($rule); } - $ruleParameterCount = $rule->parameterCount(); - $parameterCount = count($parameters); + if ($rule instanceof ParameterizedRuleInterface) { + $ruleParameterCount = count($rule->allowedParameters()); + $parameterCount = count($parameters); - return (0 === $ruleParameterCount) ? true : ($parameterCount === $ruleParameterCount); + return $parameterCount === $ruleParameterCount; + } + + return true; } /** @@ -379,7 +384,8 @@ protected function validateUsingCustomRule( protected function addFailure( string $message, string $attribute, - $value, ValidationRuleInterface $rule, + $value, + ValidationRuleInterface $rule, array $parameters = [] ): void { $this->currentRowMessages[] = $this->makeReplacements( diff --git a/tests/src/Rules/ClosureValidationTest.php b/tests/src/Rules/ClosureValidationTest.php deleted file mode 100644 index e399133..0000000 --- a/tests/src/Rules/ClosureValidationTest.php +++ /dev/null @@ -1,20 +0,0 @@ -assertEquals(0, $closureValidationRule->parameterCount()); - } -} diff --git a/tests/src/UppercaseRule.php b/tests/src/UppercaseRule.php index 02928e5..47a20b9 100644 --- a/tests/src/UppercaseRule.php +++ b/tests/src/UppercaseRule.php @@ -6,11 +6,6 @@ class UppercaseRule implements ValidationRuleInterface { - public function parameterCount(): int - { - return 0; - } - /** * @param mixed $value * @param $parameters @@ -24,9 +19,4 @@ public function message(): string { return 'The :attribute value :value must be uppercase on line :line.'; } - - public function parameterReplacer(string $message, array $parameters): string - { - return $message; - } }