Skip to content
GitHub

Installation

Get started with mdg-utils for PHP

One Composer package, mdgascar/mdg-utils, with every module as value objects and enums. PHP 8.3+, zero dependencies, same results as the JavaScript packages.

  1. Terminal
    composer require mdgascar/mdg-utils
  2. Each module is a class under Mdgascar\MdgUtils. Read a number written in any common way and format it.

    signup.php
    <?php
    require __DIR__ . '/vendor/autoload.php';
    use Mdgascar\MdgUtils\Phone\PhoneNumber;
    use Mdgascar\MdgUtils\Phone\PhoneNumberFormat;
    $phone = PhoneNumber::parse('034 12 345 67');
    $phone->operator->displayName(); // 'Yas Madagascar'
    $phone->format(PhoneNumberFormat::International); // '+261 34 12 345 67'
  3. parse() throws MdgUtilsException. safeParse() returns the error code without throwing, tryParse() returns null.

    signup.php
    use Mdgascar\MdgUtils\ErrorCode;
    use Mdgascar\MdgUtils\Phone\PhoneNumber;
    $result = PhoneNumber::safeParse('031 12 345 67');
    $result->success; // false
    $result->error?->errorCode; // ErrorCode::UnknownOperator
    PhoneNumber::tryParse('not a number'); // null
  4. Catch errors from every module the same way

    Section titled “Catch errors from every module the same way”

    Every module throws MdgUtilsException; its errorCode is the same code JavaScript reports.

    signup.php
    use Mdgascar\MdgUtils\MdgUtilsException;
    try {
    PhoneNumber::parse($userInput);
    } catch (MdgUtilsException $exception) {
    echo $exception->errorCode->value; // 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | …
    }