Looking to hire Laravel developers? Try LaraJobs

malagasy-intl-laravel maintained by mdgascar

Description
Laravel validation rules, Eloquent casts and Blade directives for Malagasy phone numbers, Ariary amounts, number words and dates.
Last update
2026/11/30 01:28 (dev-feat/ariary-defaults)
License
Downloads
4

Comments
comments powered by Disqus

malagasy-intl-laravel

CI Packagist

Laravel validation rules, Eloquent casts and Blade directives for Malagasy phone numbers, Ariary amounts, number words and dates. Laravel 12 and 13, PHP 8.3+.

All the parsing and formatting comes from mdgascar/malagasy-intl, which follows the shared malagasy-intl spec. This package only wires it into Laravel. Docs and playground: mdgascar.github.io/malagasy-intl-docs.

composer require mdgascar/malagasy-intl-laravel

Laravel discovers the service provider on its own.

Requirements

PHP 8.3, 8.4 or 8.5 with Laravel 12 or 13. Full list: Supported versions.

Validation

use Mdgascar\MalagasyIntl\Laravel\Rules\AriaryAmount;
use Mdgascar\MalagasyIntl\Laravel\Rules\MalagasyPhone;

$request->validate([
    'phone' => ['required', 'mg_phone'],   // or new MalagasyPhone()
    'mobile' => ['required', 'mg_phone:mobile'], // mobile only, e.g. for SMS
    'total' => ['required', 'mg_ariary'],  // or new AriaryAmount()
]);
Rule Passes for Fails for
mg_phone / MalagasyPhone 034 12 345 67, +261 34 12 345 67, 020 22 421 19 (fixed line) 031 12 345 67 (no operator), anything not text
mg_phone:mobile / MalagasyPhone(PhoneLineType::Mobile) 034 12 345 67 020 22 421 19 (fixed line)
mg_ariary / AriaryAmount Ar 1 500, 1.500 Ar, 1 500,50, 1500 from JSON -1, 1.5 (a dot is not a decimal point by default), abc

Outside a rules array, (new MalagasyPhone())->passes($value) returns the same answer as a bool. new AriaryAmount($options) reads with other AriaryFormatSettings than the app's config.

Messages come in English and French and follow the app locale. To change them or add a language, run php artisan vendor:publish --tag=malagasy-intl-lang and edit or add lang/vendor/malagasy-intl/{locale}/validation.php, or use Laravel's usual custom messages. In another locale without its own file, Laravel falls back to the English message.

Eloquent casts

use Mdgascar\MalagasyIntl\Laravel\Casts\AsAriary;
use Mdgascar\MalagasyIntl\Laravel\Casts\AsPhoneNumber;

final class Order extends Model
{
    protected function casts(): array
    {
        return [
            'phone' => AsPhoneNumber::class, // string column, stored as E.164: +261341234567
            'total' => AsAriary::class,      // integer or decimal column, exact digits
        ];
    }
}

$order = Order::create(['phone' => '034 12 345 67', 'total' => 'Ar 1 500']);

$order->phone->operator;  // Operator::Yas
$order->phone->e164;      // '+261341234567'
$order->total->amount();  // 1500
$order->total->digits;    // '1500' (stored as is, never rounded)
$order->total->spell();   // 'dimanjato sy arivo ariary'

Assigning parses the value, so an invalid phone number or amount throws MalagasyIntlException before it reaches the database. Validate input first.

Blade

@ariary($order->total)     {{-- Ar 1 500 --}}
@mgPhone($order->phone)    {{-- 034 12 345 67 --}}
@mgSpell(2026)             {{-- enina amby roapolo sy roa arivo --}}
@mgOrdinal(3)              {{-- fahatelo --}}
@mgTime('14:30')           {{-- amin'ny roa sy telopolo minitra tolakandro --}}
@mgDateTime($order->created_at) {{-- 23 Novambra 2026 amin'ny roa sy telopolo minitra tolakandro --}}
@mgDate($order->created_at) {{-- 27 Novambra 2026 --}}

@mgPhone($order->phone, \Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumberFormat::International)
@mgSpell(30000000, \Mdgascar\MalagasyIntl\NumberWords\NumberSystem::Traditional) {{-- telo safatsiroa --}}
@mgDate($order->created_at, \Mdgascar\MalagasyIntl\DateTime\DateStyle::Numeric)

Each directive takes the typed value (Ariary, PhoneNumber, any DateTimeInterface such as Carbon) or the raw one (an int, phone text, an ISO date such as 2026-11-27). It prints nothing for null, and escapes its output.

Configuration

Only Ariary amounts have options. Publish the config with php artisan vendor:publish --tag=malagasy-intl, or set the environment variables:

Key Env Values Default
ariary.grouping MALAGASY_INTL_ARIARY_GROUPING space, dot, comma, none space
ariary.symbol_position MALAGASY_INTL_ARIARY_SYMBOL_POSITION before, after before
ariary.currency_display MALAGASY_INTL_ARIARY_CURRENCY_DISPLAY symbol, code (MGA) symbol
ariary.decimal MALAGASY_INTL_ARIARY_DECIMAL comma, dot comma

@ariary writes with these options. mg_ariary and AsAriary read every Malagasy separator (space or dot) with the configured decimal separator. With grouping set to comma they read English-style text such as 1,500.50 instead; with none they read only plain numbers such as 1500,50. An unknown value, or the same character for grouping and decimal, throws InvalidArgumentException naming the key.

Versioning

This package has its own version number, following Semantic Versioning, and it says which mdgascar/malagasy-intl versions it works with. Changes are listed in CHANGELOG.md.

Contributing

See CONTRIBUTING.md and the organisation contributing guide.

License

MIT