-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from microdel/master
Add rules for validation phone number.
- Loading branch information
Showing
9 changed files
with
221 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Saritasa\Laravel\Validation; | ||
|
||
use Saritasa\Laravel\Validation\Rules\Phone; | ||
|
||
/** | ||
* Phone validation rule. | ||
* The rule has options: "country", "type", "detect", "lenient"; | ||
* | ||
* @method PhoneRuleSet country($country) Set the country input field. | ||
* @method PhoneRuleSet countryField($name) Set the country input field. | ||
* @method PhoneRuleSet type($type) Set the phone types. | ||
* @method PhoneRuleSet mobile() Shortcut method for mobile type restriction. | ||
* @method PhoneRuleSet fixedLine() Shortcut method for fixed line type restriction. | ||
* @method PhoneRuleSet detect() Enable automatic country detection. | ||
* @method PhoneRuleSet lenient() Enable lenient number checking. | ||
* | ||
* @see https://github.com/Propaganistas/Laravel-Phone Documentation of Laravel Phone library. | ||
* @see \Propaganistas\LaravelPhone\Rules\Phone Rule of phone validation. | ||
* | ||
* @package Saritasa\Laravel\Validation | ||
*/ | ||
class PhoneRuleSet extends StringRuleSet | ||
{ | ||
/** | ||
* @var Phone | ||
*/ | ||
protected $rule; | ||
|
||
/** | ||
* PhoneRuleSet constructor. | ||
* | ||
* @param array $rules | ||
*/ | ||
public function __construct(array $rules = []) | ||
{ | ||
parent::__construct($rules); | ||
|
||
foreach ($this->rules as $rule) { | ||
if ($rule instanceof Phone) { | ||
$this->rule = $rule; | ||
} | ||
} | ||
|
||
if (!$this->rule) { | ||
$this->rule = new Phone(); | ||
$this->rules[] = $this->rule; | ||
} | ||
} | ||
|
||
function __call($name, $arguments) | ||
{ | ||
if (method_exists($this->rule, $name)) { | ||
call_user_func_array([$this->rule, $name], $arguments); | ||
return $this; | ||
} | ||
return parent::__call($name, $arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Saritasa\Laravel\Validation\Rules; | ||
|
||
use Saritasa\Laravel\Validation\IRule; | ||
|
||
class Phone extends \Propaganistas\LaravelPhone\Rules\Phone implements IRule | ||
{ | ||
/** | ||
* Convert the rule to a validation string. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return parent::__toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Saritasa\Laravel\Validation\Tests; | ||
|
||
use libphonenumber\PhoneNumberType; | ||
use PHPUnit\Framework\TestCase; | ||
use Saritasa\Laravel\Validation\Rule; | ||
|
||
class PhoneRulesTest extends TestCase | ||
{ | ||
|
||
public function testBaseRule() | ||
{ | ||
$this->assertEquals('string|phone', Rule::phone()); | ||
} | ||
|
||
public function testAutoDetectNumber() | ||
{ | ||
$this->assertEquals('string|phone:AUTO', Rule::phone()->detect()); | ||
} | ||
|
||
public function testCountry() | ||
{ | ||
$this->assertEquals('string|phone:US', Rule::phone()->country('US')); | ||
$this->assertEquals('string|phone:BE,US', Rule::phone()->country('BE', 'US')); | ||
$this->assertEquals('string|phone:BE,US', Rule::phone()->country(['BE', 'US'])); | ||
} | ||
|
||
public function testType() | ||
{ | ||
$this->assertEquals('string|phone:' . PhoneNumberType::MOBILE, Rule::phone()->type(PhoneNumberType::MOBILE)); | ||
|
||
$expected = 'string|phone:' . PhoneNumberType::MOBILE . ',' . PhoneNumberType::FIXED_LINE; | ||
$this->assertEquals($expected, Rule::phone()->type(PhoneNumberType::MOBILE, PhoneNumberType::FIXED_LINE)); | ||
$this->assertEquals($expected, Rule::phone()->type([PhoneNumberType::MOBILE, PhoneNumberType::FIXED_LINE])); | ||
$this->assertEquals($expected, Rule::phone()->mobile()->fixedLine()); | ||
|
||
$expected = 'string|phone:' . PhoneNumberType::FIXED_LINE . ',' . PhoneNumberType::MOBILE; | ||
$this->assertEquals($expected, Rule::phone()->fixedLine()->mobile()); | ||
} | ||
|
||
public function testLenient() | ||
{ | ||
$this->assertEquals('string|phone:LENIENT', Rule::phone()->lenient()); | ||
} | ||
|
||
public function testOptions() | ||
{ | ||
$this->assertEquals( | ||
'string|phone:US,' . PhoneNumberType::MOBILE . ',AUTO,LENIENT', | ||
Rule::phone()->country('US')->mobile()->detect()->lenient() | ||
); | ||
|
||
$this->assertEquals( | ||
'string|phone:US,BE,' . PhoneNumberType::MOBILE . ',AUTO,LENIENT', | ||
Rule::phone()->country(['US', 'BE'])->mobile()->detect()->lenient() | ||
); | ||
|
||
$this->assertEquals( | ||
'string|phone:US,' . PhoneNumberType::MOBILE . ',' . PhoneNumberType::FIXED_LINE . ',AUTO,LENIENT', | ||
Rule::phone()->country('US')->mobile()->fixedLine()->detect()->lenient() | ||
); | ||
|
||
$this->assertEquals( | ||
'string|phone:AUTO,LENIENT', | ||
Rule::phone()->detect()->lenient() | ||
); | ||
} | ||
|
||
public function testWithOtherRules() | ||
{ | ||
$this->assertEquals( | ||
'string|phone:AUTO,LENIENT|required', | ||
Rule::phone()->detect()->lenient()->required() | ||
); | ||
$this->assertEquals( | ||
'string|phone:AUTO|required|size:14', | ||
Rule::phone()->detect()->required()->size(14) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters