From 408da6e0477977cfb8be57491bbc23d79dac5eab Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Wed, 10 Apr 2019 10:25:19 +0200 Subject: [PATCH] Resolve number types in Rule class (#112) --- src/Rules/Phone.php | 6 ++++-- src/Traits/ParsesTypes.php | 14 +++++++------- tests/PhoneValidatorTest.php | 10 +++++++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Rules/Phone.php b/src/Rules/Phone.php index d8aafc1..5541b59 100644 --- a/src/Rules/Phone.php +++ b/src/Rules/Phone.php @@ -6,6 +6,8 @@ class Phone { + use ParsesTypes; + /** * The provided phone countries. * @@ -141,7 +143,7 @@ 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'] : []) @@ -149,4 +151,4 @@ public function __toString() return 'phone' . (! empty($parameters) ? ":$parameters" : ''); } -} \ No newline at end of file +} diff --git a/src/Traits/ParsesTypes.php b/src/Traits/ParsesTypes.php index 072cb2e..25264de 100644 --- a/src/Traits/ParsesTypes.php +++ b/src/Traits/ParsesTypes.php @@ -12,7 +12,7 @@ trait ParsesTypes * * @var array */ - protected static $types; + protected static $resolvedTypes; /** * Determine whether the given type is valid. @@ -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; @@ -62,7 +62,7 @@ protected static function parseTypesAsStrings($types) return array_keys( array_intersect( - static::$types, + static::$resolvedTypes, static::parseTypes($types) ) ); @@ -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(); } } -} \ No newline at end of file +} diff --git a/tests/PhoneValidatorTest.php b/tests/PhoneValidatorTest.php index 4ecbf39..ddd2132 100644 --- a/tests/PhoneValidatorTest.php +++ b/tests/PhoneValidatorTest.php @@ -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'; @@ -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); }