Search by

mdgascar / malagasy-intl-symfony

johan-mickael

Symfony bundle: validation constraints, Twig filters and Doctrine types for Malagasy phone numbers, Ariary amounts, number words and dates.

Package info

github.com/mdgascar/malagasy-intl-symfony

Documentation

Type:symfony-bundle

pkg:composer/mdgascar/malagasy-intl-symfony

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 5

Open Issues: 0

v0.7.0 2026-11-29 23:58 UTC

This package is auto-updated.

Last update: 2026-11-30 00:36:05 UTC


README

CI Packagist

Symfony validation constraints, Twig filters and Doctrine types for Malagasy phone numbers, Ariary amounts, number words and dates. Symfony 7.4 LTS and 8, PHP 8.3+ (Symfony 8 needs PHP 8.4).

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

composer require mdgascar/malagasy-intl-symfony

With Symfony Flex the bundle is enabled for you. Otherwise add it to config/bundles.php:

Mdgascar\MalagasyIntlBundle\MalagasyIntlBundle::class => ['all' => true],

Each part needs its library and is skipped without it: the constraints need symfony/validator, the filters twig/twig, and the types DoctrineBundle (DBAL 4).

Requirements

PHP 8.3, 8.4 or 8.5 with Symfony 7.4 LTS, or Symfony 8.x on PHP 8.4 and later. Full list: Supported versions.

Validation

use Mdgascar\MalagasyIntlBundle\Validator\AriaryAmount;
use Mdgascar\MalagasyIntlBundle\Validator\MalagasyPhone;
use Symfony\Component\Validator\Constraints as Assert;

final class OrderInput
{
    #[Assert\NotBlank, MalagasyPhone]
    public ?string $phone = null;

    #[Assert\NotBlank, AriaryAmount]
    public ?string $total = null;
}
Constraint Passes for Fails for
MalagasyPhone 034 12 345 67, +261 34 12 345 67, 020 22 421 19 (fixed line), a PhoneNumber 031 12 345 67 (no operator), anything not text
MalagasyPhone(type: PhoneLineType::Mobile) 034 12 345 67 020 22 421 19 (fixed line)
AriaryAmount Ar 1 500, 1.500 Ar, 1 500,50, 1500 from JSON, an Ariary -1, 1.5 (a dot is not a decimal point by default), abc

null and '' pass, as with Symfony's own constraints: add NotBlank to require a value. Messages come in English and French (domain validators) and can be changed per use with message:, e.g. #[MalagasyPhone(message: 'Use a Malagasy mobile number.')]. The violation codes are MalagasyPhone::INVALID_ERROR, MalagasyPhone::WRONG_TYPE_ERROR (a number of the other kind) and AriaryAmount::INVALID_ERROR.

Doctrine types

The bundle registers two column types with DoctrineBundle.

use Doctrine\ORM\Mapping as ORM;
use Mdgascar\MalagasyIntl\Ariary\Ariary;
use Mdgascar\MalagasyIntl\PhoneNumber\PhoneNumber;

#[ORM\Entity]
class Order
{
    #[ORM\Column(type: 'mg_phone')] // VARCHAR(16), stored as E.164: +261341234567
    public ?PhoneNumber $phone = null;

    #[ORM\Column(type: 'mg_ariary', precision: 20, scale: 2)] // DECIMAL
    public ?Ariary $total = null;
}

$order->phone = PhoneNumber::parse('034 12 345 67');
$order->total = Ariary::fromInt(1500);

The entity holds the value objects, so parse input once (or validate it first, then parse). A stored value that is not a valid number or amount fails with Doctrine's ValueNotConvertible.

Twig

{{ order.total|ariary }}                    {# Ar 1 500 #}
{{ order.phone|mg_phone }}                  {# 034 12 345 67 #}
{{ order.phone|mg_phone('international') }} {# +261 34 12 345 67 #}
{{ 2026|mg_spell }}                         {# enina amby roapolo sy roa arivo #}
{{ 3|mg_ordinal }}                          {# fahatelo #}
{{ '14:30'|mg_time }}                       {# amin'ny roa sy telopolo minitra tolakandro #}
{{ order.createdAt|mg_datetime }}           {# 23 Novambra 2026 amin'ny roa sy telopolo minitra tolakandro #}
{{ 30000000|mg_spell('traditional') }}      {# telo safatsiroa #}
{{ order.createdAt|mg_date }}               {# 27 Novambra 2026 #}
{{ order.createdAt|mg_date('numeric') }}    {# 27/11/2026 #}

Each filter takes the typed value (Ariary, PhoneNumber, any DateTimeInterface) or the raw one (an int, phone text, an ISO date such as 2026-11-27), and prints nothing for null. The format names are the PhoneNumberFormat, DateStyle and NumberSystem values (e164, international, national; short, medium, long, full, numeric; standard, traditional); an unknown name throws ValueError. Invalid input throws MalagasyIntlException.

Configuration

Only Ariary amounts have options:

# config/packages/malagasy_intl.yaml
malagasy_intl:
    ariary:
        grouping: space        # space (Ar 1 500), dot (Ar 1.500), comma (Ar 1,500) or none (Ar 1500)
        symbol_position: before # before (Ar 1 500) or after (1 500 Ar)
        decimal: comma         # comma (Ar 1 500,50) or dot
        currency_display: symbol # symbol (Ar 1 500) or code (MGA 1 500)

The ariary filter writes with these options. AriaryAmount reads every Malagasy separator (space or dot) with the configured decimal separator; with grouping: comma it reads English-style text such as 1,500.50 instead, and with grouping: none only plain numbers such as 1500,50. An unknown value, or the same character for grouping and decimal, fails when the container is built.

Versioning

This bundle 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