Readers
The three readers are final readonly classes of the Silarhi\Cfonb namespace, without state: one instance can parse
any number of files.
Cfonb120Reader
Section titled “Cfonb120Reader”use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse($content);| Member | Description |
|---|---|
parse(string $content, bool $strict = true): array |
Parses the contents of a CFONB 120 file into a list of Statement |
iterate(string $content, bool $strict = true): Generator |
Since 6.7: the statements one by one, each as soon as its 07 new balance is read |
iterateFile(string $path, bool $strict = true): Generator |
Since 6.7: the statements one by one, read line by line from the file |
LINE_LENGTH |
120, the length of a line |
$strict = false turns blank operation references into empty strings instead of errors:
strict mode.
Cfonb240Reader
Section titled “Cfonb240Reader”use Silarhi\Cfonb\Cfonb240Reader;
$reader = new Cfonb240Reader();$transfers = $reader->parse($content);| Member | Description |
|---|---|
parse(string $content, bool $strict = true): array |
Parses the contents of a CFONB 240 file into a list of Transfer |
iterate(string $content, bool $strict = true): Generator |
Since 6.7: the transfers one by one, each as soon as its 39 total is read |
iterateFile(string $path, bool $strict = true): Generator |
Since 6.7: the transfers one by one, read line by line from the file |
LINE_LENGTH |
240, the length of a line |
$strict has no effect on CFONB 240 files.
CfonbReader
Section titled “CfonbReader”Both formats behind one object, e.g. a single service to inject:
use Silarhi\Cfonb\Banking\Statement;use Silarhi\Cfonb\Banking\Transfer;use Silarhi\Cfonb\CfonbReader;
$reader = new CfonbReader();
foreach ($reader->parseCfonb120('My Content') as $statement) { assert($statement instanceof Statement);}
foreach ($reader->parseCfonb240('My Content') as $transfer) { assert($transfer instanceof Transfer);}| Member | Description |
|---|---|
__construct(Cfonb120Reader $cfonb120Reader = new …, Cfonb240Reader $cfonb240Reader = new …) |
The readers to delegate to |
parse(string $content, bool $strict = true): array |
Since 6.7: either reader, by the format of the file |
parseCfonb120(string $content, bool $strict = true): array |
Cfonb120Reader::parse() |
parseCfonb240(string $content, bool $strict = true): array |
Cfonb240Reader::parse() |
parse() detects the format with CfonbFormat::fromContent(): the statements of a CFONB 120 file, the transfers of a
CFONB 240 file, an empty list for an empty file, and a ParseException for a first record code of neither format.
CfonbFormat
Section titled “CfonbFormat”Since 6.7, the Silarhi\Cfonb\CfonbFormat enum tells the two formats apart, by the record code of the first line that
is not empty: 01, 04, 05 and 07 are CFONB 120 records, 31, 34 and 39 CFONB 240 records.
| Member | Description |
|---|---|
Cfonb120, Cfonb240 |
The two formats |
fromContent(string $content): CfonbFormat |
The format of the content, or a ParseException |
tryFromContent(string $content): ?CfonbFormat |
The format of the content, null when it is empty or of neither |
lineLength(): int |
120 or 240 |
Large files
Section titled “Large files”parse() returns every statement or transfer of the file at once, and needs its content as a string. iterateFile()
reads the file line by line instead and yields one statement or transfer at a time, so the memory used does not grow
with the size of the file:
<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();
// Line by line from the file: one statement in memory at a time, whatever the size of the fileforeach ($reader->iterateFile('statements.txt') as $statement) { echo sprintf( "%s %s → %s %d operations\n", $statement->getAccount()->getAccountNumber(), $statement->getOldBalance()->getAmount(true), $statement->getNewBalance()->getAmount(true), count($statement->getOperations()) );}00012345603 16695.65 → 16672.86 1 operations00020427603 16672.86 → 21632.64 3 operations00020427603 21632.64 → 21632.64 0 operationsiterate() does the same on a string. iterateFile() throws a RuntimeException when the file cannot be opened, at the
call; the file stays open until the last statement is read, or the generator is destroyed. A file without line breaks
is read in full, as parse() does.
$contentis the contents of the file, not its path: read it withfile_get_contents()or from an upload.iterateFile()takes the path.- Lines are separated by
\n,\r\nor\r(\rsince 6.5); a file without line breaks is split everyLINE_LENGTHcharacters. Empty lines, and line breaks at the start and the end, are ignored; a line of spaces or tabs is not, and throws aParseException. - The characters are kept as they are: no encoding conversion, only the whitespace around each field is trimmed.
- An empty string gives an empty list.
The readers accept an optional FileParser in their constructor: it is internal (Silarhi\Cfonb\Parser), and not
meant to be replaced.
Errors
Section titled “Errors”parse() throws a ParseException on the first line it cannot parse: nothing is returned for
the lines before it. iterate() and iterateFile() throw it while iterating: the statements and transfers yielded
before it are complete.
