Skip to content

Commit

Permalink
#5 (#10)
Browse files Browse the repository at this point in the history
* Added :line placeholder replacer, to replace the row/line number which the error occurred in the CSV.
* Updated Validator to set row/line number and passed it to addFailure for setting the row/line number.
* Updated all supported rules to display line number in their error message.
* Updated test for checking if the line number is replaced properly.
* Updated the README so, developers know they can pass :line number as a placeholder when overwriting the Rules error message.
  • Loading branch information
hoshomoh authored Nov 18, 2018
1 parent 2b9dd52 commit 88d32a4
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ use Oshomo\CsvUtils\Validator\Validator;
$validator = new Validator("some/valid/file_path", ",", [
'title' => ["ascii_only", "url"]
], [
'ascii_only' => 'The :value supplied for :attribute attribute is invalid',
'ascii_only' => 'The :value supplied for :attribute attribute is invalid on line :line of the CSV.',
// This specifies a custom message for a given attribute.
'hotel_link:url' => 'The :attribute must be a valid link',
'hotel_link:url' => 'The :attribute must be a valid link. This error occured on line :line of the CSV.',
]);
```

In this above example, the `:attribute` place-holder will be replaced by the actual name of the field under validation. The `:value` place-holder will also be replaced with value being validated. You may also utilize other place-holders in validation messages. For example the `between` rule exposes two other placeholder `min` and `max`. Find more about this in the available rules section
In this above example, the `:attribute` place-holder will be replaced by the actual name of the field under validation. The `:value` place-holder will be replaced with value being validated. The `:line` place-holder will also be replaced with the row/line number in the CSV in which the error happened. You may also utilize other place-holders in validation messages. For example the `between` rule exposes two other placeholder `min` and `max`. Find more about this in the available rules section

##### Available rules

Expand Down Expand Up @@ -206,7 +206,7 @@ class UppercaseRule implements ValidationRuleInterface
*/
public function message()
{
return "The :attribute value :value must be uppercase.";
return "The :attribute value :value must be uppercase on line :line.";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$file_path = realpath(dirname(__FILE__));
$file = $file_path . '/sample/sample.csv';
$validator = new Validator($file, ',', [
'stars' => ['between:0,5'],
'stars' => ['between:7,10'],
'name' => ['ascii_only'],
'uri' => ['url', function ($value, $fail) {
if (0 !== strpos($value, 'https://')) {
Expand Down
20 changes: 18 additions & 2 deletions src/Helpers/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function getFromLocalArray($attribute, $lowerRule)
/**
* @param $rule
*
* @return null|string|string[]
* @return string|string[]|null
*/
protected function ruleToLower($rule)
{
Expand All @@ -86,15 +86,18 @@ protected function ruleToLower($rule)
* @param mixed $value
* @param ValidationRuleInterface $rule
* @param array $parameters
* @param $lineNumber
*
* @return string
*/
protected function makeReplacements($message, $attribute, $value, $rule, $parameters)
protected function makeReplacements($message, $attribute, $value, $rule, $parameters, $lineNumber)
{
$message = $this->replaceAttributePlaceholder($message, $attribute);

$message = $this->replaceValuePlaceholder($message, $value);

$message = $this->replaceErrorLinePlaceholder($message, $lineNumber);

$message = $rule->parameterReplacer($message, $parameters);

return $message;
Expand Down Expand Up @@ -125,4 +128,17 @@ protected function replaceValuePlaceholder($message, $value)
{
return str_replace([':value'], [$value], $message);
}

/**
* Replace the :line placeholder in the given message.
*
* @param $message
* @param $lineNUmber
*
* @return mixed
*/
protected function replaceErrorLinePlaceholder($message, $lineNUmber)
{
return str_replace([':line'], [$lineNUmber], $message);
}
}
2 changes: 1 addition & 1 deletion src/Rules/AsciiOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function passes($value, $parameters)
*/
public function message()
{
return 'The :attribute value :value contains a non-ascii character';
return 'The :attribute value :value contains a non-ascii character on line :line.';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function passes($value, $parameters)
*/
public function message()
{
return 'The :attribute value :value is not between :min - :max.';
return 'The :attribute value :value is not between :min - :max on line :line.';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function passes($value, $parameters)
*/
public function message()
{
return 'The :attribute value :value is not a valid url';
return 'The :attribute value :value is not a valid url on line :line.';
}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Validator
*/
protected $message;

/**
* The line number of the row under validation.
*
* @var int
*/
protected $currentRowLineNumber = 0;

/**
* The row under validation.
*
Expand Down Expand Up @@ -79,7 +86,7 @@ class Validator
*
* @var string
*/
protected $delimiter;
protected $delimiter = ',';

/**
* The rules to be applied to the data.
Expand Down Expand Up @@ -110,7 +117,7 @@ class Validator
* @param array $rules
* @param array $messages
*/
public function __construct($filePath, $delimiter = ',', array $rules, array $messages = [])
public function __construct($filePath, $delimiter, array $rules, array $messages = [])
{
$this->filePath = $filePath;
$this->delimiter = $delimiter;
Expand Down Expand Up @@ -175,6 +182,7 @@ protected function passes()
if ($this->doesFileExistAndReadable($this->filePath)) {
if (false !== ($handle = fopen($this->filePath, 'r'))) {
while (false !== ($row = fgetcsv($handle, 0, $this->delimiter))) {
++$this->currentRowLineNumber;
if (empty($this->headers)) {
$this->setHeaders($row);
continue;
Expand Down Expand Up @@ -269,7 +277,7 @@ protected function validateRow($row)
* @param string $attribute
* @param string $rule
*
* @return null|void
* @return void|null
*/
protected function validateAttribute($attribute, $rule)
{
Expand Down Expand Up @@ -423,7 +431,8 @@ protected function addFailure($message, $attribute, $value, $rule, $parameters =
$attribute,
$value,
$rule,
$parameters
$parameters,
$this->currentRowLineNumber
);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/src/CsvValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testAsciiOnlyValidationRule()
);

$this->assertContains(
'The name value Well Health Hotels¡ contains a non-ascii character',
'The name value Well Health Hotels¡ contains a non-ascii character on line 2.',
$validator->errors()['data'][0]['errors']
);
}
Expand All @@ -83,7 +83,7 @@ public function testBetweenValidationRule()
);

$this->assertContains(
'The stars value 3 is not between 4 - 10.',
'The stars value 3 is not between 4 - 10 on line 2.',
$validator->errors()['data'][0]['errors']
);
}
Expand All @@ -109,7 +109,7 @@ public function testUrlValidationRule()
);

$this->assertContains(
'The uri value http//:well.org is not a valid url',
'The uri value http//:well.org is not a valid url on line 2.',
$validator->errors()['data'][0]['errors']
);
}
Expand All @@ -135,7 +135,7 @@ public function testValidatorWithCustomRuleObject()
);

$this->assertContains(
'The name value Well Health Hotels¡ must be uppercase.',
'The name value Well Health Hotels¡ must be uppercase on line 2.',
$validator->errors()['data'][0]['errors']
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/UppercaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function passes($value, $parameters)
*/
public function message()
{
return 'The :attribute value :value must be uppercase.';
return 'The :attribute value :value must be uppercase on line :line.';
}

/**
Expand Down

0 comments on commit 88d32a4

Please sign in to comment.