43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||
use Illuminate\Routing\Controller as BaseController;
|
||
|
||
class Controller extends BaseController
|
||
{
|
||
use AuthorizesRequests, ValidatesRequests;
|
||
|
||
public function validateGhanaPhone($phone) {
|
||
// Remove spaces, dashes, etc.
|
||
$phone = preg_replace('/\D+/', '', $phone);
|
||
|
||
// If it starts with 0 (e.g. 0241234567), strip the leading 0
|
||
if (strpos($phone, '0') === 0) {
|
||
$phone = substr($phone, 1);
|
||
}
|
||
|
||
// If it starts with Ghana country code without + (233...), add +
|
||
if (strpos($phone, '233') === 0) {
|
||
$phone = '+'.$phone;
|
||
}
|
||
|
||
// If it doesn’t start with +233, prepend it
|
||
if (strpos($phone, '+233') !== 0) {
|
||
$phone = '+233'.$phone;
|
||
}
|
||
|
||
// Now validate length: Ghana mobile numbers are 9 digits after +233
|
||
$pattern = '/^\+233\d{9}$/';
|
||
|
||
if (preg_match($pattern, $phone)) {
|
||
return $phone; // valid, normalized
|
||
} else {
|
||
return false; // invalid
|
||
}
|
||
}
|
||
|
||
}
|