Skip to content
GitHub

Installation

Get started with mdg-utils for Symfony

mdgascar/mdg-utils-bundle adds validation constraints, Twig filters and Doctrine types on top of the PHP package. Symfony 7.4 LTS and 8, PHP 8.3+.

  1. Terminal
    composer require mdgascar/mdg-utils-bundle

    With Symfony Flex the bundle is enabled for you; otherwise add Mdgascar\MdgUtilsBundle\MdgUtilsBundle::class => ['all' => true] to config/bundles.php. It brings in mdgascar/mdg-utils, so every class from Using Composer works too.

  2. src/Dto/OrderInput.php
    use Mdgascar\MdgUtilsBundle\Validator\AriaryAmount;
    use Mdgascar\MdgUtilsBundle\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;
    }

    MalagasyPhone passes for 034 12 345 67 or +261 34 12 345 67 and fails for 031 12 345 67. AriaryAmount passes for Ar 1 500, 1 500,50 or 1500 from JSON. Messages come in English and French.

  3. src/Entity/Order.php
    use Doctrine\ORM\Mapping as ORM;
    use Mdgascar\MdgUtils\Ariary\Ariary;
    use Mdgascar\MdgUtils\Phone\PhoneNumber;
    #[ORM\Column(type: 'mg_phone')] // stored as +261341234567
    public ?PhoneNumber $phone = null;
    #[ORM\Column(type: 'mg_ariary', precision: 20, scale: 2)]
    public ?Ariary $total = null;
  4. templates/order/show.html.twig
    {{ order.total|ariary }} {# Ar 1 500 #}
    {{ order.phone|mg_phone }} {# 034 12 345 67 #}
    {{ 2026|mg_words }} {# enina amby roapolo sy roa arivo #}
    {{ order.createdAt|mg_date }} {# 27 Novambra 2026 #}

    mg_phone and mg_date take a format name: mg_phone('international'), mg_date('numeric'). Each filter prints nothing for null.

Only Ariary amounts have options.

config/packages/mdg_utils.yaml
mdg_utils:
ariary:
grouping: space # space, dot or comma
symbol_position: before # before or after
decimal: comma # comma or dot

A wrong value fails when the container is built, with the option named.