Download the PHP package husail/edi-sdk without Composer
On this page you can find all versions of the php package husail/edi-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download husail/edi-sdk
More information about husail/edi-sdk
Files in husail/edi-sdk
Package edi-sdk
Short Description Generic SDK for reading, writing and validating fixed-width EDI files.
License MIT
Homepage https://github.com/husail/edi-sdk
Informations about the package edi-sdk
husail/edi-sdk
Generic PHP SDK for reading, writing and validating fixed-width EDI files.
๐ Requirements
- PHP 8.2+
symfony/yaml(for YAML layout driver)
๐ฆ Installation
๐ง Core concepts
Define the file layout once and reuse it for:
- writing files
- parsing files
- validating files
The SDK handles:
- field positions and lengths
- padding and normalization
- numeric formatting
- line endings
- typed value casting
- structural validation
Layouts can be defined using:
- PHP
- YAML
- JSON
It also supports complex file structures such as:
- headers and trailers
- repeatable groups
- interleaved record types
- CNAB-style batch structures
๐ Defining a layout
PHP Builder
YAML
line_endingmust use single-quoted escape sequences ('\r\n','\n'). Double quotes cause YAML to interpret the escape before the SDK processes it.
JSON
Same structure as YAML.
๐ Writing a file
๐ Reading a file
Without
castdefined on the field, the parser returns raw strings. Addcast: int,cast: floatorcast: dateto the field definition for automatic conversion.
โ Validating a file
๐ณ Sequence nodes
The sequence tree describes how records are ordered and grouped in the file.
| Node | Factory | Description |
|---|---|---|
RecordNode |
Record::one($layout) |
Exactly one required record |
RecordNode |
Record::optional($layout) |
One optional record |
ManyNode |
Record::many($layout) |
Zero or more records of the same type |
GroupNode |
Group::repeat($identifyBy, $children) |
Repeatable group of records (e.g. batches) |
AmbiguousNode |
Group::ambiguous($identifyBy, $children) |
Interleaved record types at the same position |
The identifyBy closure receives the raw line and returns the record name it belongs to, or null to close the group.
Example: repeatable batches
Composite identify_by in YAML
Some formats use the same character at a given position for multiple record types.
The YAML driver supports composite identify_by rules with multiple match conditions.
More specific rules must come first โ the first matching rule wins.
Each match supports value (exact equality) and in (list of accepted values).
children is optional when identify_by is present โ the driver automatically infers a ManyNode
for each record declared in the rules, preserving order. If neither children nor identify_by
is present, a LayoutException is thrown.
๐ Field definition reference
| Property | Type | Description |
|---|---|---|
name |
string |
Field key in parser output |
pos |
int |
Start position, 1-based |
len |
int |
Length in characters |
type |
alpha\|numeric |
Determines default padding |
const |
?string |
Fixed value โ writer ignores input, validator enforces |
default |
?string |
Fallback when value is null or empty |
required |
bool |
Validator emits error when ALPHA field is empty (default: true) |
cast |
?string |
Parser cast: int, float, date |
decimal_places |
int |
Implicit decimal places for numeric values (requires cast: float) |
format |
?string |
Date format, required when cast: date (e.g. dmY) |
padding_char |
?string |
Overrides default padding char for the type |
padding_side |
left\|right |
Overrides default padding side for the type |
Default padding
| Type | Char | Side |
|---|---|---|
alpha |
space | right |
numeric |
0 |
left |
requiredonly applies to ALPHA fields. For NUMERIC, zeros are valid values and cannot be distinguished from unfilled fields in a fixed-width format.
โ๏ธ Custom validators
โ๏ธ Custom layout driver
Implement LayoutDriverInterface to load layouts from any source.
Your driver is responsible only for parsing the format โ the ArrayLayoutMapper
handles building the FileLayout from the normalized array.
๐งช Testing
๐ค Contributing
Contributions, issues and pull requests are welcome. \ If you find a bug or have a suggestion, feel free to open an issue.
๐ License
Licensed under the MIT License.