Skip to content

Commit

Permalink
Resolve number types in Rule class (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas authored Apr 10, 2019
1 parent c9cf86e commit 408da6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/Rules/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Phone
{
use ParsesTypes;

/**
* The provided phone countries.
*
Expand Down Expand Up @@ -141,12 +143,12 @@ public function __toString()
{
$parameters = implode(',', array_merge(
$this->countries,
$this->types,
static::parseTypes($this->types),
($this->countryField ? [$this->countryField]: []),
($this->detect ? ['AUTO'] : []),
($this->lenient ? ['LENIENT'] : [])
));

return 'phone' . (! empty($parameters) ? ":$parameters" : '');
}
}
}
14 changes: 7 additions & 7 deletions src/Traits/ParsesTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait ParsesTypes
*
* @var array
*/
protected static $types;
protected static $resolvedTypes;

/**
* Determine whether the given type is valid.
Expand All @@ -38,12 +38,12 @@ protected static function parseTypes($types)
return Collection::make(is_array($types) ? $types : func_get_args())
->map(function ($type) {
// If the type equals a constant's value, just return it.
if (is_numeric($type) && in_array($type, static::$types)) {
if (is_numeric($type) && in_array($type, static::$resolvedTypes)) {
return (int) $type;
}

// Otherwise we'll assume the type is the constant's name.
return Arr::get(static::$types, strtoupper($type));
return Arr::get(static::$resolvedTypes, strtoupper($type));
})
->reject(function ($value) {
return is_null($value) || $value === false;
Expand All @@ -62,7 +62,7 @@ protected static function parseTypesAsStrings($types)

return array_keys(
array_intersect(
static::$types,
static::$resolvedTypes,
static::parseTypes($types)
)
);
Expand All @@ -73,8 +73,8 @@ protected static function parseTypesAsStrings($types)
*/
private static function loadTypes()
{
if (! static::$types) {
static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants();
if (! static::$resolvedTypes) {
static::$resolvedTypes = with(new ReflectionClass(PhoneNumberType::class))->getConstants();
}
}
}
}
10 changes: 9 additions & 1 deletion tests/PhoneValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,18 @@ public function it_has_a_rule_class()
$actual = with(new Rule)->mobile();
$expected = 'phone:1';
$this->assertEquals($expected, (string) $actual);

$actual = with(new Rule)->type('mobile');
$expected = 'phone:1';
$this->assertEquals($expected, (string) $actual);

$actual = with(new Rule)->mobile()->fixedLine();
$expected = 'phone:1,0';
$this->assertEquals($expected, (string) $actual);

$actual = with(new Rule)->type('mobile')->type('fixed_line');
$expected = 'phone:1,0';
$this->assertEquals($expected, (string) $actual);

$actual = with(new Rule)->country('BE')->country('AU','US')->country(['CH','FR']);
$expected = 'phone:BE,AU,US,CH,FR';
Expand All @@ -502,7 +510,7 @@ public function it_has_a_rule_class()
$this->assertEquals($expected, (string) $actual);

$actual = with(new Rule)->detect()->lenient()->type('toll_free')->type(PhoneNumberType::VOIP)->country('BE')->countryField('my_field');
$expected = 'phone:BE,toll_free,6,my_field,AUTO,LENIENT';
$expected = 'phone:BE,3,6,my_field,AUTO,LENIENT';
$this->assertEquals($expected, (string) $actual);
}

Expand Down

0 comments on commit 408da6e

Please sign in to comment.