Installation
Get started with malagasy-intl for PHP
One Composer package, mdgascar/malagasy-intl, with every module as value objects and enums. PHP 8.3+, zero dependencies, same results as the JavaScript packages.
Requirements
Section titled “Requirements”| PHP | 8.3, 8.4, 8.5 |
| Dependencies | None |
| Projects | Any Composer project: plain PHP, WordPress, Slim, Laminas… For Laravel and Symfony, add malagasy-intl-laravel or malagasy-intl-symfony for validation, templates and database types. |
Installing with Composer
Section titled “Installing with Composer”-
Require the package
Section titled “Require the package”Terminal composer require mdgascar/malagasy-intl -
Use a module
Section titled “Use a module”Each module is a class under
Mdgascar\MalagasyIntl. Read a number written in any common way and format it.signup.php <?phprequire __DIR__ . '/vendor/autoload.php';use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumber;use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumberFormat;$phone = PhoneNumber::parse('034 12 345 67');$phone->operator->displayName(); // 'Yas Madagascar'$phone->format(PhoneNumberFormat::International); // '+261 34 12 345 67' -
Handle bad input
Section titled “Handle bad input”parse()throwsMalagasyIntlException.safeParse()returns the error code without throwing,tryParse()returnsnull.signup.php use Mdgascar\MalagasyIntl\ErrorCode;use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumber;$result = PhoneNumber::safeParse('031 12 345 67');$result->success; // false$result->error?->errorCode; // ErrorCode::UnknownOperatorPhoneNumber::tryParse('not a number'); // null -
Catch errors from every module the same way
Section titled “Catch errors from every module the same way”Every module throws
MalagasyIntlException; itserrorCodeis the same code JavaScript reports.signup.php use Mdgascar\MalagasyIntl\MalagasyIntlException;try {PhoneNumber::parse($userInput);} catch (MalagasyIntlException $exception) {echo $exception->errorCode->value; // 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | …}