From 3026b20245befd5edbf6918adfd1d60c39940ff4 Mon Sep 17 00:00:00 2001 From: Oshomo Oforomeh Date: Sat, 28 Oct 2023 00:18:02 +0200 Subject: [PATCH] chore: Fixed failing tests --- src/Helpers/FormatsMessages.php | 8 ++------ src/Rules/AsciiOnly.php | 2 +- src/Rules/Between.php | 14 +++++++++++--- src/Rules/ClosureValidationRule.php | 2 -- src/Validator/ValidationRuleParser.php | 3 ++- tests/src/CsvValidatorParserTest.php | 4 ++-- tests/src/CsvValidatorTest.php | 4 ++-- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Helpers/FormatsMessages.php b/src/Helpers/FormatsMessages.php index 99fd3fa..b66b243 100755 --- a/src/Helpers/FormatsMessages.php +++ b/src/Helpers/FormatsMessages.php @@ -60,9 +60,7 @@ protected function ruleToLower(string $rule): ?string $lowerRule = strtolower($lowerRule); - $lowerRule = ltrim($lowerRule, '_'); - - return $lowerRule; + return ltrim($lowerRule, '_'); } /** @@ -90,9 +88,7 @@ protected function makeReplacements( $message = $this->replaceValuePlaceholder($message, $value); - $message = $this->replaceErrorLinePlaceholder($message, $lineNumber); - - return $message; + return $this->replaceErrorLinePlaceholder($message, $lineNumber); } /** diff --git a/src/Rules/AsciiOnly.php b/src/Rules/AsciiOnly.php index a811a59..ed1bffc 100644 --- a/src/Rules/AsciiOnly.php +++ b/src/Rules/AsciiOnly.php @@ -15,7 +15,7 @@ class AsciiOnly implements ValidationRuleInterface */ public function passes($value, array $parameters): bool { - return (mb_detect_encoding($value, 'ASCII', true)) ? true : false; + return (bool)mb_detect_encoding($value, 'ASCII', true); } /** diff --git a/src/Rules/Between.php b/src/Rules/Between.php index df7151c..a194fd5 100644 --- a/src/Rules/Between.php +++ b/src/Rules/Between.php @@ -21,11 +21,19 @@ public function allowedParameters(): array */ public function passes($value, array $parameters): bool { - $size = (int) $value; - list($min, $max) = $parameters; - return $size >= (int) $min && $size <= (int) $max; + if (is_numeric($value)) { + $convertedValue = +$value; + return $convertedValue >= +$min && $convertedValue <= +$max; + } + + if (is_string($value)) { + $valueLength = strlen($value); + return $valueLength >= +$min && $valueLength <= +$max; + } + + return false; } /** diff --git a/src/Rules/ClosureValidationRule.php b/src/Rules/ClosureValidationRule.php index f54b9f7..5da4c52 100755 --- a/src/Rules/ClosureValidationRule.php +++ b/src/Rules/ClosureValidationRule.php @@ -44,8 +44,6 @@ public function __construct(\Closure $callback) */ public function passes($value, array $parameters): bool { - $this->failed = false; - $this->callback->__invoke($value, function ($message) { $this->failed = true; diff --git a/src/Validator/ValidationRuleParser.php b/src/Validator/ValidationRuleParser.php index 04c792f..4bdfae6 100755 --- a/src/Validator/ValidationRuleParser.php +++ b/src/Validator/ValidationRuleParser.php @@ -5,6 +5,7 @@ namespace Oshomo\CsvUtils\Validator; use Closure; +use Oshomo\CsvUtils\Contracts\ValidationRuleInterface; use Oshomo\CsvUtils\Contracts\ValidationRuleInterface as ValidationRule; use Oshomo\CsvUtils\Rules\ClosureValidationRule; @@ -13,7 +14,7 @@ class ValidationRuleParser /** * Extract the rule name and parameters from a rule. * - * @param string $rule|ValidationRuleInterface + * @param string|ValidationRuleInterface $rule */ public static function parse($rule): array { diff --git a/tests/src/CsvValidatorParserTest.php b/tests/src/CsvValidatorParserTest.php index 27086c6..8ca74df 100755 --- a/tests/src/CsvValidatorParserTest.php +++ b/tests/src/CsvValidatorParserTest.php @@ -1,8 +1,8 @@