mdgascar / malagasy-intl-symfony
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
Type:symfony-bundle
pkg:composer/mdgascar/malagasy-intl-symfony
Requires
- php: >=8.3
- mdgascar/malagasy-intl: ^0.15
- symfony/config: ^7.4 || ^8.0
- symfony/dependency-injection: ^7.4 || ^8.0
- symfony/http-kernel: ^7.4 || ^8.0
Requires (Dev)
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13 || ^3.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^12.5 || ^13.4
- povils/phpmnd: ^3.6
- symfony/filesystem: ^7.4 || ^8.0
- symfony/framework-bundle: ^7.4 || ^8.0
- symfony/translation: ^7.4 || ^8.0
- symfony/twig-bundle: ^7.4 || ^8.0
- symfony/validator: ^7.4 || ^8.0
- twig/twig: ^3.15
Suggests
- doctrine/doctrine-bundle: For the mg_phone and mg_ariary Doctrine types (doctrine/dbal 4)
- symfony/validator: For the MalagasyPhone and AriaryAmount constraints
- twig/twig: For the ariary, mg_phone, mg_spell and mg_date filters
Provides
None
Conflicts
- doctrine/dbal: <4.0
Replaces
None
README
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.