Laravel validation rule and helpers for Ghana phone numbers
# Gh Phone Validator
A Laravel validation rule and helper package for validating and normalizing Ghana phone numbers.
## Installation
Install the package via Composer:
```bash
composer require nanayawkumi/gh-phone-validator
```
The package will automatically register its service provider if you're using Laravel's package auto-discovery.
## Requirements
- PHP ^8.2 (Laravel 13 requires PHP ^8.3)
- Laravel ^10.0|^11.0|^12.0|^13.0
## Usage
### Validation Rule
You can use the `gh_phone` validation rule in your form requests or controllers:
```php
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'phone' => 'required|gh_phone',
]);
```
Or use the rule class directly:
```php
use Nanayawkumi\GhPhoneValidator\Rules\GhPhone;
$validator = Validator::make($request->all(), [
'phone' => ['required', new GhPhone()],
]);
```
### Normalize Phone Numbers
The `normalize()` method converts various phone number formats to a standard 10-digit format (starting with 0):
```php
use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;
GhPhoneValidator::normalize('241234567'); // Returns: '0241234567'
GhPhoneValidator::normalize('+233241234567'); // Returns: '0241234567'
GhPhoneValidator::normalize('233241234567'); // Returns: '0241234567'
GhPhoneValidator::normalize('024-123-4567'); // Returns: '0241234567'
GhPhoneValidator::normalize('024 123 4567'); // Returns: '0241234567'
GhPhoneValidator::normalize('invalid'); // Returns: null
```
### Detect Network
The `network()` method identifies the network provider for a given phone number and returns a `Network` enum:
```php
use Nanayawkumi\GhPhoneValidator\GhPhoneValidator;
use Nanayawkumi\GhPhoneValidator\Enums\Network;
$network = GhPhoneValidator::network('0241234567');
// Returns: Network::MTN
$network = GhPhoneValidator::network('0201234567');
// Returns: Network::TELECEL
$network = GhPhoneValidator::network('0261234567');
// Returns: Network::AIRTELTIGO …