boda/edi-parser
Simple positional EDI parser that transforms a raw fixed-width EDI string into a structured key-value array using templates. Supports parsing header/body/footer sections and grouped lines for nested records. Install via Composer and use as a Symfony bundle.
Extract Core Logic:
EdiParser class (located in src/Boda/EdiParserBundle/Parser/EdiParser.php).vendor folder.Install via Composer:
composer require boda/edi-parser
Note: Since this is a Symfony bundle, manually extract the EdiParser class or use a wrapper.
Basic Usage in Laravel:
// Register the parser in a service provider
$this->app->singleton('edi.parser', function ($app) {
return new \Boda\EdiParserBundle\Parser\EdiParser($app['config']['edi.templates']);
});
// Parse EDI in a controller or command
$rawEdi = file_get_contents('path/to/edi_file.txt');
$parser = app('edi.parser');
$parsedData = $parser->parse($rawEdi, 'invoice_template');
Define a Template:
Create a config file (config/edi.php) to define your EDI template structure:
return [
'templates' => [
'invoice' => [
'header' => [
'IDENTIFIER' => ['start' => 0, 'length' => 2],
'DOT' => ['start' => 2, 'length' => 1],
// ... other fields
],
'body' => [
'group' => [
'IDENTIFIER' => ['start' => 0, 'length' => 2],
'CONTENT' => ['start' => 5, 'length' => 20],
],
],
],
],
];
First Use Case: Parse a simple EDI file and log the output:
use Illuminate\Support\Facades\Log;
$parsed = app('edi.parser')->parse($rawEdi, 'invoice');
Log::info('Parsed EDI:', ['data' => $parsed]);
EdiParser with a predefined template.EdiParsed) or send alerts for failures.Example:
// app/Services/EdiParserService.php
class EdiParserService {
public function process(string $filePath, string $templateName) {
$rawEdi = file_get_contents($filePath);
$parsed = app('edi.parser')->parse($rawEdi, $templateName);
// Validate
$validator = Validator::make($parsed, [
'header.IDENTIFIER' => 'required|string',
'body.*.CONTENT' => 'required',
]);
if ($validator->fails()) {
throw new \RuntimeException('EDI validation failed');
}
// Store
Edi::create($parsed);
}
}
config/edi.php for fixed EDI formats.// Custom template loader
$template = TemplateRepository::find($templateId);
$parsed = app('edi.parser')->parse($rawEdi, $template->toArray());
config_cache or a migration-based approach to version templates.EdiParseJob::dispatch($rawEdi, $templateName)->onQueue('edi');
event(new EdiParsed($parsedData));
return new EdiResource($parsedData);
Combine positional parsing with other formats (e.g., CSV for delimited fields):
$header = app('edi.parser')->parse(substr($rawEdi, 0, 100), 'header_template');
$body = array_map(function ($line) {
return str_getcsv($line);
}, explode("\n", substr($rawEdi, 100)));
Service Container Binding:
Bind the EdiParser to Laravel’s container in a service provider:
$this->app->bind('edi.parser', function ($app) {
return new \Boda\EdiParserBundle\Parser\EdiParser(
$app['config']['edi.templates']
);
});
Configuration: Use Laravel’s config system to define templates:
// config/edi.php
return [
'templates' => [
'invoice' => [
'header' => [
'IDENTIFIER' => ['start' => 0, 'length' => 2],
// ...
],
],
],
];
Artisan Commands: Create a command to test EDI parsing:
// app/Console/Commands/ParseEdi.php
class ParseEdi extends Command {
protected $signature = 'edi:parse {file} {template}';
public function handle() {
$rawEdi = file_get_contents($this->argument('file'));
$parsed = app('edi.parser')->parse($rawEdi, $this->argument('template'));
$this->info(json_encode($parsed, JSON_PRETTY_PRINT));
}
}
Testing: Use Laravel’s testing tools to mock the parser:
$this->app->instance('edi.parser', Mockery::mock(EdiParser::class));
$mockParser->shouldReceive('parse')->andReturn($mockData);
$template = Cache::remember("edi.template.{$templateName}", now()->addHours(1), function () use ($templateName) {
return config("edi.templates.{$templateName}");
});
$handle = fopen($filePath, 'r');
while (!feof($handle)) {
$chunk = fread($handle, 8192);
$parsedChunk = app('edi.parser')->parse($chunk, $template);
// Process chunk
}
Symfony Dependency Hell:
EdiParser class and avoid including the full bundle.Template Mismatches:
$validator = Validator::make($parsed, [
'header.IDENTIFIER' => 'required|size:2',
'body.*.CONTENT' => 'required|string',
]);
No Built-in Error Handling:
try {
$parsed = app('edi.parser')->parse($rawEdi, $template);
} catch (\Exception $e) {
Log::error("EDI parsing failed: {$e->getMessage()}");
throw new \RuntimeException('Failed to parse EDI', 0, $e);
}
Static Templates:
$template = Template::where('name', $templateName)->first()->toArray();
$parsed = app('edi.parser')->parse($rawEdi, $template);
No Support for X12/EDIFACT:
baselink/edi) for these formats.Outdated Codebase:
How can I help you explore Laravel packages today?