| @@ -4,27 +4,27 @@ | ||
| 4 | 4 | |
| 5 | 5 | class InvalidParameterException extends \Exception | 
| 6 | 6 |  { | 
| 7 | - /** | |
| 8 | - * Ambiguous parameter static constructor. | |
| 9 | - * | |
| 10 | - * @param string $parameter | |
| 11 | - * @return static | |
| 12 | - */ | |
| 13 | - public static function ambiguous($parameter) | |
| 14 | -    { | |
| 15 | -        return new static('Ambiguous phone validation parameter: "' . $parameter . '". This parameter is recognized as an input field and as a phone type. Please rename the input field.'); | |
| 16 | - } | |
| 7 | + /** | |
| 8 | + * Ambiguous parameter static constructor. | |
| 9 | + * | |
| 10 | + * @param string $parameter | |
| 11 | + * @return static | |
| 12 | + */ | |
| 13 | + public static function ambiguous($parameter) | |
| 14 | +	{ | |
| 15 | +		return new static('Ambiguous phone validation parameter: "' . $parameter . '". This parameter is recognized as an input field and as a phone type. Please rename the input field.'); | |
| 16 | + } | |
| 17 | 17 | |
| 18 | - /** | |
| 19 | - * Invalid parameters static constructor. | |
| 20 | - * | |
| 21 | - * @param array|Collection $parameters | |
| 22 | - * @return static | |
| 23 | - */ | |
| 24 | - public static function parameters($parameters) | |
| 25 | -    { | |
| 26 | - $parameters = Collection::make($parameters); | |
| 18 | + /** | |
| 19 | + * Invalid parameters static constructor. | |
| 20 | + * | |
| 21 | + * @param array|Collection $parameters | |
| 22 | + * @return static | |
| 23 | + */ | |
| 24 | + public static function parameters($parameters) | |
| 25 | +	{ | |
| 26 | + $parameters = Collection::make($parameters); | |
| 27 | 27 | |
| 28 | -        return new static('Invalid phone validation parameters: "' . $parameters->implode(',') . '".'); | |
| 29 | - } | |
| 28 | +		return new static('Invalid phone validation parameters: "' . $parameters->implode(',') . '".'); | |
| 29 | + } | |
| 30 | 30 | } | 
| 31 | 31 | \ No newline at end of file | 
| @@ -12,7 +12,7 @@ discard block | ||
| 12 | 12 | */ | 
| 13 | 13 | public static function ambiguous($parameter) | 
| 14 | 14 |      { | 
| 15 | -        return new static('Ambiguous phone validation parameter: "' . $parameter . '". This parameter is recognized as an input field and as a phone type. Please rename the input field.'); | |
| 15 | +        return new static('Ambiguous phone validation parameter: "'.$parameter.'". This parameter is recognized as an input field and as a phone type. Please rename the input field.'); | |
| 16 | 16 | } | 
| 17 | 17 | |
| 18 | 18 | /** | 
| @@ -25,6 +25,6 @@ discard block | ||
| 25 | 25 |      { | 
| 26 | 26 | $parameters = Collection::make($parameters); | 
| 27 | 27 | |
| 28 | -        return new static('Invalid phone validation parameters: "' . $parameters->implode(',') . '".'); | |
| 28 | +        return new static('Invalid phone validation parameters: "'.$parameters->implode(',').'".'); | |
| 29 | 29 | } | 
| 30 | 30 | } | 
| 31 | 31 | \ No newline at end of file | 
| @@ -11,130 +11,130 @@ | ||
| 11 | 11 | |
| 12 | 12 | class Phone | 
| 13 | 13 |  { | 
| 14 | - use ParsesCountries, | |
| 15 | - ParsesTypes; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * @var \libphonenumber\PhoneNumberUtil | |
| 19 | - */ | |
| 20 | - protected $lib; | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * Phone constructor. | |
| 24 | - */ | |
| 25 | - public function __construct() | |
| 26 | -    { | |
| 27 | - $this->lib = PhoneNumberUtil::getInstance(); | |
| 28 | - } | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Validates a phone number. | |
| 32 | - * | |
| 33 | - * @param string $attribute | |
| 34 | - * @param mixed $value | |
| 35 | - * @param array $parameters | |
| 36 | - * @param object $validator | |
| 37 | - * @return bool | |
| 38 | - */ | |
| 39 | - public function validate($attribute, $value, array $parameters, $validator) | |
| 40 | -    { | |
| 41 | - $data = $validator->getData(); | |
| 42 | - | |
| 43 | - list( | |
| 44 | - $countries, | |
| 45 | - $types, | |
| 46 | - $detect, | |
| 47 | - $lenient) = $this->extractParameters($attribute, $parameters, $data); | |
| 48 | - | |
| 49 | - // A "null" country is prepended: | |
| 50 | - // 1. In case of auto-detection to have the validation run first without supplying a country. | |
| 51 | - // 2. In case of lenient validation without provided countries; we still might have some luck... | |
| 52 | -        if ($detect || ($lenient && empty($countries))) { | |
| 53 | - array_unshift($countries, null); | |
| 54 | - } | |
| 55 | - | |
| 56 | -        foreach ($countries as $country) { | |
| 57 | -            try { | |
| 58 | - // Parsing the phone number also validates the country, so no need to do this explicitly. | |
| 59 | - // It'll throw a PhoneCountryException upon failure. | |
| 60 | - $phoneNumber = PhoneNumber::make($value, $country); | |
| 61 | - | |
| 62 | - // Type validation. | |
| 63 | -                if (! empty($types) && ! $phoneNumber->isOfType($types)) { | |
| 64 | - continue; | |
| 65 | - } | |
| 66 | - | |
| 67 | - $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance(); | |
| 68 | - | |
| 69 | - // Lenient validation. | |
| 70 | -                if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) { | |
| 71 | - return true; | |
| 72 | - } | |
| 73 | - | |
| 74 | - $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance(); | |
| 75 | - | |
| 76 | - // Country detection. | |
| 77 | -                if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) { | |
| 78 | - return true; | |
| 79 | - } | |
| 80 | - | |
| 81 | - // Default number+country validation. | |
| 82 | -                if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) { | |
| 83 | - return true; | |
| 84 | - } | |
| 85 | -            } catch (NumberParseException $e) { | |
| 86 | - continue; | |
| 87 | - } | |
| 88 | - } | |
| 89 | - | |
| 90 | - return false; | |
| 91 | - } | |
| 92 | - | |
| 93 | - /** | |
| 94 | - * Parse and extract parameters in the appropriate validation arguments. | |
| 95 | - * | |
| 96 | - * @param string $attribute | |
| 97 | - * @param array $parameters | |
| 98 | - * @param array $data | |
| 99 | - * @return array | |
| 100 | - * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException | |
| 101 | - */ | |
| 102 | - protected function extractParameters($attribute, array $parameters, array $data) | |
| 103 | -    { | |
| 104 | - // Discover if an input field was provided. If not, guess the field's name. | |
| 105 | - $inputField = Collection::make($parameters) | |
| 106 | - ->intersect(array_keys(Arr::dot($data))) | |
| 107 | -                                ->first() ?: "${attribute}_country"; | |
| 108 | - | |
| 109 | - // Attempt to retrieve the field's value. | |
| 110 | -        if ($inputCountry = Arr::get($data, $inputField)) { | |
| 111 | - | |
| 112 | -            if (static::isValidType($inputField)) { | |
| 113 | - throw InvalidParameterException::ambiguous($inputField); | |
| 114 | - } | |
| 115 | - | |
| 116 | - $parameters[] = $inputCountry; | |
| 117 | - } | |
| 118 | - | |
| 119 | - $countries = static::parseCountries($parameters); | |
| 120 | - $types = static::parseTypes($parameters); | |
| 121 | - | |
| 122 | - // Force developers to write proper code. | |
| 123 | - // Since the static parsers return a validated array with preserved keys, we can safely diff against the keys. | |
| 124 | - // Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions. | |
| 125 | - $leftovers = array_diff_key($parameters, $types, $countries); | |
| 126 | - $leftovers = array_diff($leftovers, ['AUTO', 'LENIENT', $inputField]); | |
| 127 | - | |
| 128 | -        if (! empty($leftovers)) { | |
| 129 | - throw InvalidParameterException::parameters($leftovers); | |
| 130 | - } | |
| 131 | - | |
| 132 | - return [ | |
| 133 | - $countries, | |
| 134 | - $types, | |
| 135 | -            in_array('AUTO', $parameters), | |
| 136 | -            in_array('LENIENT', $parameters), | |
| 137 | - $inputField, | |
| 138 | - ]; | |
| 139 | - } | |
| 14 | + use ParsesCountries, | |
| 15 | + ParsesTypes; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * @var \libphonenumber\PhoneNumberUtil | |
| 19 | + */ | |
| 20 | + protected $lib; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Phone constructor. | |
| 24 | + */ | |
| 25 | + public function __construct() | |
| 26 | +	{ | |
| 27 | + $this->lib = PhoneNumberUtil::getInstance(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Validates a phone number. | |
| 32 | + * | |
| 33 | + * @param string $attribute | |
| 34 | + * @param mixed $value | |
| 35 | + * @param array $parameters | |
| 36 | + * @param object $validator | |
| 37 | + * @return bool | |
| 38 | + */ | |
| 39 | + public function validate($attribute, $value, array $parameters, $validator) | |
| 40 | +	{ | |
| 41 | + $data = $validator->getData(); | |
| 42 | + | |
| 43 | + list( | |
| 44 | + $countries, | |
| 45 | + $types, | |
| 46 | + $detect, | |
| 47 | + $lenient) = $this->extractParameters($attribute, $parameters, $data); | |
| 48 | + | |
| 49 | + // A "null" country is prepended: | |
| 50 | + // 1. In case of auto-detection to have the validation run first without supplying a country. | |
| 51 | + // 2. In case of lenient validation without provided countries; we still might have some luck... | |
| 52 | +		if ($detect || ($lenient && empty($countries))) { | |
| 53 | + array_unshift($countries, null); | |
| 54 | + } | |
| 55 | + | |
| 56 | +		foreach ($countries as $country) { | |
| 57 | +			try { | |
| 58 | + // Parsing the phone number also validates the country, so no need to do this explicitly. | |
| 59 | + // It'll throw a PhoneCountryException upon failure. | |
| 60 | + $phoneNumber = PhoneNumber::make($value, $country); | |
| 61 | + | |
| 62 | + // Type validation. | |
| 63 | +				if (! empty($types) && ! $phoneNumber->isOfType($types)) { | |
| 64 | + continue; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance(); | |
| 68 | + | |
| 69 | + // Lenient validation. | |
| 70 | +				if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) { | |
| 71 | + return true; | |
| 72 | + } | |
| 73 | + | |
| 74 | + $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance(); | |
| 75 | + | |
| 76 | + // Country detection. | |
| 77 | +				if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) { | |
| 78 | + return true; | |
| 79 | + } | |
| 80 | + | |
| 81 | + // Default number+country validation. | |
| 82 | +				if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) { | |
| 83 | + return true; | |
| 84 | + } | |
| 85 | +			} catch (NumberParseException $e) { | |
| 86 | + continue; | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + return false; | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Parse and extract parameters in the appropriate validation arguments. | |
| 95 | + * | |
| 96 | + * @param string $attribute | |
| 97 | + * @param array $parameters | |
| 98 | + * @param array $data | |
| 99 | + * @return array | |
| 100 | + * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException | |
| 101 | + */ | |
| 102 | + protected function extractParameters($attribute, array $parameters, array $data) | |
| 103 | +	{ | |
| 104 | + // Discover if an input field was provided. If not, guess the field's name. | |
| 105 | + $inputField = Collection::make($parameters) | |
| 106 | + ->intersect(array_keys(Arr::dot($data))) | |
| 107 | +								->first() ?: "${attribute}_country"; | |
| 108 | + | |
| 109 | + // Attempt to retrieve the field's value. | |
| 110 | +		if ($inputCountry = Arr::get($data, $inputField)) { | |
| 111 | + | |
| 112 | +			if (static::isValidType($inputField)) { | |
| 113 | + throw InvalidParameterException::ambiguous($inputField); | |
| 114 | + } | |
| 115 | + | |
| 116 | + $parameters[] = $inputCountry; | |
| 117 | + } | |
| 118 | + | |
| 119 | + $countries = static::parseCountries($parameters); | |
| 120 | + $types = static::parseTypes($parameters); | |
| 121 | + | |
| 122 | + // Force developers to write proper code. | |
| 123 | + // Since the static parsers return a validated array with preserved keys, we can safely diff against the keys. | |
| 124 | + // Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions. | |
| 125 | + $leftovers = array_diff_key($parameters, $types, $countries); | |
| 126 | + $leftovers = array_diff($leftovers, ['AUTO', 'LENIENT', $inputField]); | |
| 127 | + | |
| 128 | +		if (! empty($leftovers)) { | |
| 129 | + throw InvalidParameterException::parameters($leftovers); | |
| 130 | + } | |
| 131 | + | |
| 132 | + return [ | |
| 133 | + $countries, | |
| 134 | + $types, | |
| 135 | +			in_array('AUTO', $parameters), | |
| 136 | +			in_array('LENIENT', $parameters), | |
| 137 | + $inputField, | |
| 138 | + ]; | |
| 139 | + } | |
| 140 | 140 | } |