PHP SDK for parsing, validating, building, serializing, and CSV-converting MSCONS EDIFACT messages, per EANCOM® 2002 S3 (GS1 Edition 2016 Upd. 2021, Reference Directory D.01B, EANCOM Subset Version 004).
composer require malakhov/mscons-sdkuse Malakhov\Mscons\Parser\MsconsParser;
$interchange = (new MsconsParser())->parse(file_get_contents('input.edi'));
foreach ($interchange->messages as $message) {
foreach ($message->premises as $premise) {
foreach ($premise->meters as $meter) {
// ...
}
}
}use Malakhov\Mscons\Builder\MessageBuilder;
use Malakhov\Mscons\Model\{Interchange, Party, Premise, Meter, LineItem, Quantity};
use Malakhov\Mscons\Model\Enum\{PartyFunctionQualifier, QuantityQualifier};
use Malakhov\Mscons\Writer\MsconsWriter;
$message = (new MessageBuilder())
->reference('8552')
->messageDate('20020102')
->addParty(new Party(PartyFunctionQualifier::Supplier, id: '5098765111111'))
->addParty(new Party(PartyFunctionQualifier::Buyer, id: '5471615111118'))
->addPremise(new Premise('5071615222229', [
new Meter('5098765222220', lineItems: [
new LineItem('1', '5467890102019', quantities: [
new Quantity(QuantityQualifier::DeliveredQuantity, '39486058.01', 'MTQ'),
]),
]),
]))
->build();
$edi = (new MsconsWriter())->write(new Interchange('5098765111111', '5471615111118', '020102', '1000', '1', [$message]));use Malakhov\Mscons\Csv\CsvConverter;
(new CsvConverter())->toFile($message, 'output.csv');bin/mscons validate input.edi
bin/mscons to-csv input.edi -o output.csv
bin/mscons build data.json -o output.ediPHP and Composer are not required on the host — everything runs through Docker via bin/dev:
./bin/dev composer install
./bin/dev vendor/bin/phpunit
./bin/dev vendor/bin/phpstan analyse
./bin/dev vendor/bin/php-cs-fixer fix --dry-run --diffThe three worked examples in the source spec (§6) use legacy GS1-temporary
codes that don't match the "current" restricted code tables in §4/§5 (see
docs/superpowers/specs/2026-07-14-mscons-sdk-design.md and the plan's
Global Constraints for details). This SDK models both code sets so it stays
compatible with real-world senders following either edition.
This SDK targets MSCONS under EANCOM® 2002 S3 (GS1's implementation
guideline, Reference Directory D.01B) — the retail/consumer-goods subset of
UN/EDIFACT. It does not implement the German energy-market national
guideline (edi@energy MSCONS MIG, UN Directory D.04B S3), which structures
meter-reading data differently (e.g. an STS segment, OBIS codes carried in
PIA, market-location parties, balancing-group location groups). If you're
integrating with German energy-market counterparties, verify which variant
they send before assuming compatibility.
- Introducing UN/EDIFACT —
UNECE/UN-CEFACT's overview of the UN/EDIFACT standard this SDK's
Edifactsyntax layer implements (interchange/message/segment/element hierarchy, service segments, syntax rules). mscons.pdf(in this repo) — EANCOM® 2002 S3 MSCONS message spec (GS1 Edition 2016 Upd. 2021, Reference Directory D.01B, EANCOM Subset Version 004) this SDK'sModel/Parser/Writerdomain layer implements.