Last active
August 27, 2018 05:13
-
-
Save VictorFursa/b7397d91686e5a5388ca7f907cd9b4a2 to your computer and use it in GitHub Desktop.
Laravel Phone Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'phone_number' => "required|unique:users,phone_number,$id|min:10|max:20", | |
protected function prepareForValidation() | |
{ | |
$this->offsetSet('phone_number', PhoneHelper::normalize($this->phone_number)); | |
} | |
<?php | |
namespace App\Modules\Core\Helpers; | |
use Exception; | |
use Propaganistas\LaravelPhone\PhoneNumber; | |
class PhoneHelper | |
{ | |
/** | |
* @return string | |
*/ | |
public static function normalize($phone) | |
{ | |
$phone = preg_replace('/[^\d\+]/', '', $phone); | |
try { | |
return PhoneNumber::make($phone, 'UA')->formatE164(); | |
} catch (Exception $exception) { | |
return $phone; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment