Skip to content

Commit

Permalink
Rule parameter clean up (#17)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hoshomoh authored Mar 26, 2020
1 parent 77389a4 commit da85210
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 161 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: php
php:
- 7.1
- 7.2
- 7.3
- nightly

matrix:
Expand Down
49 changes: 9 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
```

Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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:
Expand Down Expand Up @@ -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 |
Expand All @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions src/Contracts/ParameterizedRuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Oshomo\CsvUtils\Contracts;

interface ParameterizedRuleInterface
{
/**
* Should return an array of the allowed parameters.
* See the between rule. Tha allowed parameters should be
* tokenized string e.g :min, :max, :first, :last etc.
*/
public function allowedParameters(): array;
}
20 changes: 0 additions & 20 deletions src/Contracts/ValidationRuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@

interface 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.
*/
public function parameterCount(): int;

/**
* Determines if the validation rule passes. This is where we do the
* actual validation. If the validation passes return true else false.
Expand All @@ -27,17 +20,4 @@ public function passes($value, array $parameters): bool;
* :attribute and :value placeholders in the message string.
*/
public function message(): string;

/**
* 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
* );.
*/
public function parameterReplacer(string $message, array $parameters): string;
}
22 changes: 20 additions & 2 deletions src/Helpers/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Oshomo\CsvUtils\Helpers;

use Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface;
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface;

trait FormatsMessages
Expand Down Expand Up @@ -79,15 +80,32 @@ protected function makeReplacements(
): string {
$message = $this->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.
*/
Expand Down
16 changes: 0 additions & 16 deletions src/Rules/AsciiOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
24 changes: 7 additions & 17 deletions src/Rules/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}
16 changes: 0 additions & 16 deletions src/Rules/ClosureValidationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
16 changes: 0 additions & 16 deletions src/Rules/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
14 changes: 10 additions & 4 deletions src/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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(
Expand Down
20 changes: 0 additions & 20 deletions tests/src/Rules/ClosureValidationTest.php

This file was deleted.

Loading

0 comments on commit da85210

Please sign in to comment.