Skip to content

Commit

Permalink
Throw exception for an ambiguous parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas committed Aug 22, 2017
1 parent 179fe35 commit a404e65
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

class InvalidParameterException extends \Exception
{
/**
* Ambiguous parameter static constructor.
*
* @param string $parameter
* @return static
*/
public static function ambiguous($parameter)
{
return new static('Ambiguous phone validation parameter: "' . $parameter . '". This parameter is recognized as an input field and as a phone type. Please rename the input field.');
}

/**
* Invalid parameters static constructor.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Validation/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ protected function extractParameters($attribute, array $parameters, array $data)

// Attempt to retrieve the field's value.
if ($inputCountry = Arr::get($data, $inputField)) {

if (static::isValidType($inputField)) {
throw InvalidParameterException::ambiguous($inputField);
}

$parameters[] = $inputCountry;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/PhoneValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,17 @@ public function it_throws_an_exception_for_invalid_parameters()
['field' => 'phone:BE,xyz,mobile,abc']
)->passes();
}

/** @test */
public function it_throws_an_exception_for_ambiguous_parameters()
{
$this->expectException(InvalidParameterException::class);
$this->expectExceptionMessage('mobile');

$this->validator->make(
['mobile' => '0470123456', 'mobile_country' => 'BE'],
['mobile' => 'phone:mobile']
)->passes();
}

/** @test */
Expand Down

0 comments on commit a404e65

Please sign in to comment.