apie/country-and-phone-number
Installation
composer require apie/country-and-phone-number
Verify the package loads by running:
php artisan package:discover
First Use Case: Validate and Parse a Phone Number
use Apie\CountryAndPhoneNumber\PhoneNumber;
$phoneNumber = new PhoneNumber('+14155552671'); // Google's US HQ
$isValid = $phoneNumber->isValid(); // true
$countryCode = $phoneNumber->getCountryCode(); // 'US'
First Use Case: Fetch Country Data
use Apie\CountryAndPhoneNumber\Country;
$country = Country::getByCode('DE');
$countryName = $country->getName(); // 'Germany'
$dialCode = $country->getDialCode(); // '+49'
Where to Look First
Apie\CountryAndPhoneNumberPhoneNumber, Country, CountryCollection$phone = new PhoneNumber('+44 20 7946 0958');
$phone->normalize(); // '+442079460958'
$phone = new PhoneNumber('0123456789');
$phone->setCountryCode('IN'); // India
$phone->isValid(); // true/false
use Apie\CountryAndPhoneNumber\CountryCollection;
$countries = CountryCollection::all()
->filterByDialCode('+1'); // US/Canada
$country = Country::getByName('France'); // Loads on demand
use Illuminate\Validation\Rule;
$rules = [
'phone' => ['required', function ($attribute, $value, $fail) {
$phone = new PhoneNumber($value);
if (!$phone->isValid()) {
$fail('Invalid phone number.');
}
}],
];
use Apie\CountryAndPhoneNumber\PhoneNumber;
Request::macro('parsePhone', function () {
return new PhoneNumber($this->phone);
});
$country = Country::getByCode('JP');
$name = __($country->getName()); // Use Laravel's translation
$countries = Cache::remember('apie.countries', now()->addDays(7), function () {
return CountryCollection::all();
});
class CustomPhoneRule extends \Illuminate\Validation\Rules\In {
public function validateStringAttribute($attribute, $value, $fail) {
$phone = new PhoneNumber($value);
if (!$phone->isValid() || !$phone->getCountryCode() === 'US') {
$fail('Must be a valid US number.');
}
}
}
$user = User::find(1);
$user->phone = (new PhoneNumber($user->phone))->normalize();
return response()->json($user);
No Official Documentation
$phone->getCountryCode() // Returns string, not Country object.
Monorepo Maintenance
Edge Cases in Validation
isValid() but lack a country code (e.g., short codes). Always check:
if ($phone->isValid() && $phone->getCountryCode()) {
// Safe to use
}
Performance with Large Datasets
CountryCollection::all() loads all countries (~250) into memory. Cache aggressively if used frequently.Inspect Phone Number Components
$phone = new PhoneNumber('+447900123456');
dd([
'raw' => $phone->getRawNumber(),
'national' => $phone->getNationalNumber(),
'country' => $phone->getCountryCode(),
]);
Check Country Data
$country = Country::getByCode('XK'); // Kosovo (disputed)
dd($country->toArray()); // Inspect all available fields
Enable Strict Typing
composer.json to catch type mismatches early:
"config": {
"platform-check": true
}
Add Custom Country Data
Country class or use a decorator pattern:
class ExtendedCountry extends Country {
public function getRegion() {
return $this->getSubregion(); // Custom logic
}
}
Override Validation Logic
PhoneNumber:
class BusinessPhoneNumber extends PhoneNumber {
public function isValid() {
$isValid = parent::isValid();
return $isValid && $this->startsWith('+1'); // US-only
}
}
Integrate with Laravel Scout
use Laravel\Scout\Searchable;
class Country extends \Apie\CountryAndPhoneNumber\Country implements Searchable {
public function toSearchableArray() {
return [
'name' => $this->getName(),
'code' => $this->getCode(),
];
}
}
__() for translations:
$countryName = __($country->getName());
How can I help you explore Laravel packages today?