Skip to content

Commit

Permalink
Merge pull request #4 from microdel/master
Browse files Browse the repository at this point in the history
Add rules for validation phone number.
  • Loading branch information
populov committed Jan 3, 2018
2 parents bdea4d9 + 8db4d95 commit 53b7aa2
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes History

1.0.7
-----
Add rules for validation phone number.

1.0.6
-----
Resolve "Presence verifier has not been set." issue for **exists** and **in** rules
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"illuminate/database": "^5.4",
"illuminate/support": "^5.4",
"illuminate/validation": "^5.4",
"saritasa/php-common": "^1.0"
"saritasa/php-common": "^1.0",
"propaganistas/laravel-phone": "^3.0.4"
},
"require-dev": {
"mockery/mockery": "^0.9",
Expand Down
11 changes: 11 additions & 0 deletions src/FluentValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Validation\ValidationServiceProvider;
use Illuminate\Contracts\Validation\Factory as IValidatorFactory;
use Propaganistas\LaravelPhone\PhoneServiceProvider;

/**
* Service provider substitutes default Laravel's validation factory
Expand All @@ -26,4 +27,14 @@ protected function registerValidationFactory()
return $validator;
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->register(PhoneServiceProvider::class);
}
}
60 changes: 60 additions & 0 deletions src/PhoneRuleSet.php
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);
}
}
29 changes: 23 additions & 6 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* @method static ImageRuleSet dimensions(array $constraints) Get a dimensions constraint builder instance.
* @method static GenericRuleSet accepted() The field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.
* @method static GenericRuleSet array() The field under validation must be a PHP array.
* @method static GenericRuleSet boolean() The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".
* @method static GenericRuleSet confirmed() The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
* @method static GenericRuleSet distinct() When working with arrays, the field under validation must not have any duplicate values.
Expand Down Expand Up @@ -46,6 +47,7 @@
* @method static StringRuleSet timezone() The field under validation must be a valid timezone identifier according to the timezone_identifiers_list PHP function
* @method static StringRuleSet url() The field under validation must be a valid URL.
* @method static StringRuleSet regex(string $pattern, bool $ignoreCase = false) The field under validation must match the given regular expression.
* @method static StringRuleSet phoneRegex() Shortcut method for validating phone with use regex.
* @method static FileRuleSet mimetypes(string ...$types) The file under validation must match one of the given MIME types. To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client provided MIME type.
* @method static FileRuleSet mimes(string ...$extensions) The file under validation must have a MIME type corresponding to one of the listed extensions.
Expand All @@ -65,12 +67,11 @@
class Rule
{
/**
The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:
The value is null.
The value is an empty string.
The value is an empty array or empty Countable object.
The value is an uploaded file with no path.
* The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:
* The value is null.
* The value is an empty string.
* The value is an empty array or empty Countable object.
* The value is an uploaded file with no path.
*/
static function required(): GenericRuleSet
{
Expand Down Expand Up @@ -132,6 +133,22 @@ static function image($constraints = []): ImageRuleSet
return new ImageRuleSet([], $constraints);
}

/**
* The field under validation must be a phone.
*
* If difficult validation is not required, it is recommended use
* {@see \Saritasa\Laravel\Validation\StringRuleSet::phoneRegex} phoneRegex() method.
*
* @see https://github.com/Propaganistas/Laravel-Phone Documentation of Laravel Phone library.
* @see \Propaganistas\LaravelPhone\Rules\Phone Rule of phone validation.
*
* @return PhoneRuleSet
*/
static function phone()
{
return new PhoneRuleSet();
}

public static function __callStatic($name, $arguments)
{
$ruleSet = null;
Expand Down
18 changes: 18 additions & 0 deletions src/Rules/Phone.php
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();
}
}
17 changes: 16 additions & 1 deletion src/StringRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class StringRuleSet extends RuleSet
{
const EXPOSED_RULES = ['email', 'regex', 'timezone'];
const EXPOSED_RULES = ['email', 'regex', 'timezone', 'phoneRegex'];

const TRIVIAL_STRING_RULES = [
'activeUrl',
Expand Down Expand Up @@ -59,6 +59,21 @@ public function regex(string $pattern, bool $ignoreCase = false): StringRuleSet
return $this->appendIfNotExists("regex:$pattern");
}

/**
* Shortcut method for validating phone with use regex.
* The method uses E.164 format for validation. (ex: +12345678901)
*
* For more difficult validation needs use \Saritasa\Laravel\Validation\Rule::phone()
*
* @see \Saritasa\Laravel\Validation\Rule::phone()
*
* @return StringRuleSet
*/
public function phoneRegex()
{
return $this->regex('/^\+(?:[0-9]?){6,14}[0-9]$/');
}

function __call($name, $arguments)
{
if (in_array($name, static::TRIVIAL_STRING_RULES)) {
Expand Down
81 changes: 81 additions & 0 deletions tests/PhoneRulesTest.php
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)
);
}
}
6 changes: 6 additions & 0 deletions tests/StringRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public function testRegex()
$this->assertEquals('string|regex:/^\w+[\w-\.]*\w+$/', $rules);
}

public function testPhoneRegex()
{
$rules = Rule::string()->phoneRegex();
$this->assertEquals('string|regex:/^\+(?:[0-9]?){6,14}[0-9]$/', $rules);
}

public function testTimezone()
{
$this->assertEquals('string|timezone', Rule::timezone());
Expand Down

0 comments on commit 53b7aa2

Please sign in to comment.