Skip to content

Commit

Permalink
Implement dependent validation (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas authored May 26, 2017
1 parent a2bb726 commit 7705023
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/LaravelPhoneServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Propaganistas\LaravelPhone;

use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use libphonenumber\PhoneNumberUtil;

Expand All @@ -13,7 +14,11 @@ class LaravelPhoneServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
$extend = version_compare(Application::VERSION, '5.4.18', '>=')
? 'extendDependent'
: 'extend';

$this->app['validator']->{$extend}('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
}

/**
Expand Down
115 changes: 115 additions & 0 deletions tests/PhoneValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,121 @@ public function testValidatePhoneFaultyParameters()
)->passes();
}

public function testValidatePhoneWithArrayInput()
{
if (version_compare(Application::VERSION, '5.4.18', '>=')) {
// Validator with correct country value.
$this->assertTrue($this->validator->make(
[
'container' => [
['field' => '016123456'],
['field' => '0499123456']
]
],
['container.*.field' => 'phone:BE'])->passes()
);

// Validator with wrong country value.
$this->assertFalse($this->validator->make(
[
'container' => [
['field' => '016123456'],
['field' => '0499123456']
]
],
['container.*.field' => 'phone:NL'])->passes()
);

// Validator with correct country value, one wrong input.
$this->assertFalse($this->validator->make(
[
'container' => [
['field' => '01612'],
['field' => '0499123456']
]
],
['container.*.field' => 'phone:BE'])->passes()
);

// Validator with correct country value, one wrong input.
$this->assertFalse($this->validator->make(
[
'container' => [
['field' => '016123456'],
['field' => '049912']
]
],
['container.*.field' => 'phone:BE'])->passes()
);

// Validator with correct country value.
$this->assertTrue($this->validator->make(
[
'container' => [
['field' => '0477123456'],
['field' => '0499123456']
]
],
['container.*.field' => 'phone:BE,mobile'])->passes()
);

// Validator with correct country value, one input wrong type.
$this->assertFalse($this->validator->make(
[
'container' => [
['field' => '016123456'],
['field' => '0499123456']
]
],
['container.*.field' => 'phone:BE,mobile'])->passes()
);

// Validator with correct country fields.
$this->assertTrue($this->validator->make(
[
'container' => [
['field' => '016123456', 'field_country' => 'BE'],
['field' => '6502530000', 'field_country' => 'US']
]
],
['container.*.field' => 'phone'])->passes()
);

// Validator with correct country fields.
$this->assertFalse($this->validator->make(
[
'container' => [
['field' => '016123456', 'field_country' => 'BE'],
['field' => '6502530000', 'field_country' => 'BE']
]
],
['container.*.field' => 'phone'])->passes()
);

// Validator with correct custom country fields.
$this->assertTrue($this->validator->make(
[
'container' => [
['field' => '016123456', 'country_code' => 'BE'],
['field' => '6502530000', 'country_code' => 'US']
]
],
['container.*.field' => 'phone:container.*.country_code'])->passes()
);

// Validator with wrong custom country fields.
$this->assertFalse($this->validator->make(
[
'container' => [
['field' => '016123456', 'country_code' => 'BE'],
['field' => '6502530000', 'country_code' => 'BE']
]
],
['container.*.field' => 'phone:container.*.country_code'])->passes()
);
}
}

public function testHelperFunction()
{
// Test landline number without format parameter.
Expand Down

0 comments on commit 7705023

Please sign in to comment.