Search by

mdgascar / malagasy-address

johan-mickael

The regions, districts, communes and fokontany of Madagascar: find, list and search by code or name, from one shared spec. Zero dependencies.

v0.1.0 2026-12-04 00:08 UTC

This package is auto-updated.

Last update: 2026-12-04 00:37:23 UTC


README

md.gascar

malagasy-address-php

The regions, districts, communes and fokontany of Madagascar for PHP: find a place by its code, list what is below it, search by name. Zero dependencies, PHP 8.3+.

CI Packagist

Implements spec 0.1.0.

Data and test vectors come from malagasy-address-spec, included as a git submodule at spec/. The JavaScript port is malagasy-address-js.

composer require mdgascar/malagasy-address
use Mdgascar\MalagasyAddress\Places\PlaceLevel;
use Mdgascar\MalagasyAddress\Places\Places;

Places::find('MG11102430')->name;                 // 'Ankadinandriana' (a commune)
Places::find('MG11102430')->parents[0]->name;     // 'Analamanga' (its region)
Places::list('MG11');                             // the districts of Analamanga
Places::search('avaradrano', PlaceLevel::District)[0]->code; // 'MG11102'
Places::search('ambohi', within: 'MG11102')[0]->name; // a place below that district
Places::format('MG11102'); // 'Antananarivo Avaradrano, Analamanga'
Places::locate('MG11')->areaKm2;                  // the area of Analamanga in km2
Places::nearest(-18.9, 47.52, PlaceLevel::Region)->name; // 'Analamanga'
Places::distance('MG11', 'MG12');                 // the km between the two regions
Places::tryFind('MG99');                          // null

find has two forms that never throw: tryFind() returns the place or null, safeFind() a SafeParseResult. Errors throw Mdgascar\MalagasyAddress\MalagasyAddressException; check $exception->errorCode (ErrorCode::UnknownPlace, ErrorCode::InvalidFormat, …), the same code in every language.

locate() gives the point (latitude and longitude, 5 decimals) and the area of a place. nearest() finds the place of a level whose point is nearest to a point in Madagascar: it answers "nearest point", not "contains". distance() is the great circle distance in km between two places, rounded to metres. A point out of range or outside Madagascar is ErrorCode::OutOfRange.

Rules, sources and recorded decisions: places.

Data and licence

The code is MIT. The place data (names, points and areas) comes from the OCHA COD-AB for Madagascar, published on the Humanitarian Data Exchange (CC BY-IGO): see NOTICE.

Requirements

PHP 8.3, 8.4 or 8.5, with no extension beyond the defaults and no dependencies. For Laravel and Symfony see malagasy-address-laravel and malagasy-address-symfony.

Contributing

Data or vector changes go to malagasy-address-spec first. Code changes: see CONTRIBUTING.md.

License

MIT