@@ -25,7 +25,7 @@ class FixedLengthFileParser implements ParserInterface
2525 * Arrays describing field_name, start, and length for each value
2626 * encoded in a line of the file to be parsed.
2727 *
28- * @var array<int, array{field_name: string, start?: int, length: int}>
28+ * @var array<int, array{field_name: string, start?: int, length: int, align?: 'left'|'right'|'both' }>
2929 */
3030 protected array $ choppingMap = [];
3131
@@ -66,9 +66,10 @@ class FixedLengthFileParser implements ParserInterface
6666 *
6767 * Each entry must contain 'field_name' and 'length'. The 'start' key is
6868 * optional — if omitted, it is calculated from the previous entry's
69- * start + length.
69+ * start + length. The 'align' key is optional and can be 'left', 'right',
70+ * or 'both' (default is 'both').
7071 *
71- * @param array<int, array{field_name: string, start?: int, length: int}> $map
72+ * @param array<int, array{field_name: string, start?: int, length: int, align?: 'left'|'right'|'both' }> $map
7273 */
7374 public function setChoppingMap (array $ map ): void
7475 {
@@ -167,9 +168,15 @@ protected function parseLine(string $buffer): array
167168 : $ start + $ this ->choppingMap [$ i ]['length ' ];
168169
169170 $ name = $ this ->choppingMap [$ i ]['field_name ' ];
170- $ currentLine [$ name ] = trim (
171- substr ($ buffer , $ start , $ this ->choppingMap [$ i ]['length ' ])
172- );
171+ $ rawValue = substr ($ buffer , $ start , $ this ->choppingMap [$ i ]['length ' ]);
172+
173+ $ align = $ this ->choppingMap [$ i ]['align ' ] ?? 'both ' ;
174+
175+ $ currentLine [$ name ] = match ($ align ) {
176+ 'left ' => rtrim ($ rawValue ),
177+ 'right ' => ltrim ($ rawValue ),
178+ default => trim ($ rawValue ),
179+ };
173180 }
174181
175182 // Apply callback if registered
0 commit comments