diff --git a/.gitignore b/.gitignore index 2f89fe5..f7a3e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build composer.lock vendor -run.php \ No newline at end of file +run.php +.DS_Store +.phpunit.result.cache diff --git a/composer.json b/composer.json index 077a9af..fc141b2 100644 --- a/composer.json +++ b/composer.json @@ -32,13 +32,13 @@ }, "minimum-stability": "stable", "require": { - "illuminate/contracts": "^5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", - "illuminate/support": "^5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", - "php": "^7.0|^8.0" + "illuminate/contracts": "^10.0|^11.0", + "illuminate/support": "^10.0|^11.0", + "php": "^8.0" }, "require-dev": { - "phpunit/phpunit": "^6.3|^9.0|^10.5", - "orchestra/testbench": "^3.5|^5.0|^7.0|^9.0" + "phpunit/phpunit": "^6.3|^9.0|^10.5|^11.0|^12.0", + "orchestra/testbench": "^3.5|^5.0|^7.0|^9.0|^10.0" }, - "suggest": [] + "suggest": {} } diff --git a/src/Digits.php b/src/Digits.php index 5b71a2f..4c96293 100644 --- a/src/Digits.php +++ b/src/Digits.php @@ -1,30 +1,27 @@ isDigits($value); - } + protected string $message = ':attribute must be in digits only phone format'; /** - * Get the validation error message. + * Run the validation rule. * - * @return string + * @param string $attribute + * @param mixed $value + * @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @return void */ - public function message() + public function validate(string $attribute, mixed $value, Closure $fail): void { - return ':attribute must be in digits only phone format'; + if (! $this->isDigits($value)) { + $fail($this->message); + } } } diff --git a/src/E123.php b/src/E123.php index 86ac1fa..bf57f2e 100644 --- a/src/E123.php +++ b/src/E123.php @@ -2,29 +2,27 @@ namespace LVR\Phone; +use Closure; class E123 extends Phone { /** - * Determine if the validation rule passes. - * - * @param string $attribute - * @param mixed $value - * - * @return bool + * Validation error message */ - public function passes($attribute, $value) - { - return $this->isE123($value); - } + protected string $message = ':attribute must be in E.123 phone format'; /** - * Get the validation error message. + * Run the validation rule. * - * @return string + * @param string $attribute + * @param mixed $value + * @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @return void */ - public function message() + public function validate(string $attribute, mixed $value, Closure $fail): void { - return ':attribute must be in E.123 phone format'; + if (! $this->isE123($value)) { + $fail($this->message); + } } -} \ No newline at end of file +} diff --git a/src/E164.php b/src/E164.php index a586843..99fc8de 100644 --- a/src/E164.php +++ b/src/E164.php @@ -1,30 +1,27 @@ isE164($value); - } + protected string $message = ':attribute must be in E.164 phone format'; /** - * Get the validation error message. + * Run the validation rule. * - * @return string + * @param string $attribute + * @param mixed $value + * @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @return void */ - public function message() + public function validate(string $attribute, mixed $value, Closure $fail): void { - return ':attribute must be in E.164 phone format'; + if (! $this->isE164($value)) { + $fail($this->message); + } } -} \ No newline at end of file +} diff --git a/src/NANP.php b/src/NANP.php index bdf79de..ac43b52 100644 --- a/src/NANP.php +++ b/src/NANP.php @@ -1,30 +1,27 @@ isNANP($value); - } + protected string $message = ':attribute must be in the NANP phone format'; /** - * Get the validation error message. + * Run the validation rule. * - * @return string + * @param string $attribute + * @param mixed $value + * @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @return void */ - public function message() + public function validate(string $attribute, mixed $value, Closure $fail): void { - return ':attribute must be in the NANP phone format'; + if (! $this->isNANP($value)) { + $fail($this->message); + } } -} \ No newline at end of file +} diff --git a/src/Phone.php b/src/Phone.php index 00a4179..e9e792f 100644 --- a/src/Phone.php +++ b/src/Phone.php @@ -1,31 +1,29 @@ isPhone($value); - } + protected string $message = 'Incorrect phone format for :attribute.'; /** - * Get the validation error message. + * Run the validation rule. * - * @return string + * @param string $attribute + * @param mixed $value + * @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @return void */ - public function message() + public function validate(string $attribute, mixed $value, Closure $fail): void { - return 'Incorrect phone format for :attribute.'; + if (! $this->isPhone($value)) { + $fail($this->message); + } } /** @@ -90,4 +88,4 @@ protected function isNANP($value) $conditions[] = preg_match("/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i", $value) > 0; return (bool) array_product($conditions); } -} \ No newline at end of file +}