Skip to content

Commit

Permalink
ADD: functionality to validate arrays with dot notation (data.*.field)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelpeter committed Mar 15, 2019
1 parent 8381cb6 commit 5b292a5
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 428 deletions.
1 change: 1 addition & 0 deletions src/IsoCodesValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function boot()
// set the validation error messages
foreach (get_class_methods('Pixelpeter\IsoCodesValidation\IsoCodesValidator') as $method) {
$key = $this->getTranslationKeyFromMethodName($method);

$messages[$key] = $this->getErrorMessage($translator, $messages, $key);
}

Expand Down
36 changes: 28 additions & 8 deletions src/IsoCodesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function validateIpaddress($attribute, $value, $parameters)
public function validateIsbn($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'isbn');
$type = Arr::get($this->data, $parameters[0]);
$type = $this->prepareReference($attribute, $parameters);

return $this->runIsoCodesValidator(\IsoCodes\Isbn::class, $value, $type);
}
Expand Down Expand Up @@ -322,7 +322,7 @@ public function validateNif($attribute, $value, $parameters)
public function validateOrganismeType12NormeB2($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'organisme_type12_norme_b2');
$clef = Arr::get($this->data, $parameters[0]);
$clef = $this->prepareReference($attribute, $parameters);

return $this->runIsoCodesValidator(\IsoCodes\OrganismeType12NormeB2::class, $value, $clef);
}
Expand All @@ -338,7 +338,7 @@ public function validateOrganismeType12NormeB2($attribute, $value, $parameters)
public function validatePhonenumber($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'phonenumber');
$country = Arr::get($this->data, $parameters[0]);
$country = $this->prepareReference($attribute, $parameters);

return $this->runIsoCodesValidator(\IsoCodes\PhoneNumber::class, $value, $country);
}
Expand Down Expand Up @@ -497,11 +497,27 @@ public function validateVat($attribute, $value, $parameters)
public function validateZipcode($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'zipcode');
$country = Arr::get($this->data, $parameters[0]);
$country = $this->prepareReference($attribute, $parameters);

return $this->runIsoCodesValidator(\IsoCodes\ZipCode::class, $value, $country);
}

/**
* Prepare/get the reference when defined in dot notation
*
* @param $attribute
* @param $parameters
* @return mixed
*/
protected function prepareReference($attribute, $parameters)
{
if ($keys = $this->getExplicitKeys($attribute)) {
$parameters = $this->replaceAsterisksInParameters($parameters, $keys);
}

return Arr::get($this->data, $parameters[0], $parameters[0]);
}

/**
* Execute the validation function
* and catch every other Exception from underlying libraries
Expand Down Expand Up @@ -532,9 +548,9 @@ protected function runIsoCodesValidator($validator, $value, $reference = '')
* @param $parameter
* @return mixed
*/
protected function countryReplacer($message, $parameter)
protected function countryReplacer($message, $reference)
{
return str_replace(':country', Arr::get($this->data, $parameter[0]), $message);
return str_replace(':country', $reference, $message);
}

/**
Expand Down Expand Up @@ -896,8 +912,10 @@ public function replaceOrganismeType12NormeB2($message, $attribute, $rule, $para
*/
protected function replacePhonenumber($message, $attribute, $rule, $parameter)
{
$reference = $this->prepareReference($attribute, $parameter);

$message = $this->valueReplacer($message, $attribute);
$message = $this->countryReplacer($message, $parameter);
$message = $this->countryReplacer($message, $reference);

return $message;
}
Expand Down Expand Up @@ -1067,8 +1085,10 @@ public function replaceVat($message, $attribute, $rule, $parameter)
*/
public function replaceZipcode($message, $attribute, $rule, $parameter)
{
$reference = $this->prepareReference($attribute, $parameter);

$message = $this->valueReplacer($message, $attribute);
$message = $this->countryReplacer($message, $parameter);
$message = $this->countryReplacer($message, $reference);

return $message;
}
Expand Down
Loading

0 comments on commit 5b292a5

Please sign in to comment.