Recipes
The recipes parse the sample statement file: three statements of two accounts.
Filtering operations
Section titled “Filtering operations”Operations above 100 in absolute value, debits and credits alike:
<?php
use Silarhi\Cfonb\Banking\Operation;use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
foreach ($statements as $statement) { $largeOperations = array_filter( $statement->getOperations(), fn (Operation $operation) => abs($operation->getAmount()) > 100 );
foreach ($largeOperations as $operation) { echo sprintf("Large operation: %.2f - %s\n", $operation->getAmount(), $operation->getLabel()); }}Large operation: 5000.00 - VIR JOHNDOE / FOOBARBuilding a transaction summary
Section titled “Building a transaction summary”<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
$summary = ['total_credit' => 0.0, 'total_debit' => 0.0, 'operation_count' => 0];
foreach ($statements as $statement) { foreach ($statement->getOperations() as $operation) { $amount = $operation->getAmount(); if ($amount > 0) { $summary['total_credit'] += $amount; } else { $summary['total_debit'] += abs($amount); } ++$summary['operation_count']; }}
echo sprintf("Total credits: %.2f\n", $summary['total_credit']);echo sprintf("Total debits: %.2f\n", $summary['total_debit']);echo sprintf("Operations: %d\n", $summary['operation_count']);Total credits: 5000.00Total debits: 63.01Operations: 4Amounts are floats by default: add the exact decimal strings of getAmount(true) (since 6.6) with bcmath when the
result must be exact.
Converting to an array or JSON
Section titled “Converting to an array or JSON”For an API response, or to store the statements: since 6.7, every object has a toArray() and is JsonSerializable.
<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
// Every object is JsonSerializable, through its toArray(): amounts are exact decimal strings, dates are Y-m-decho json_encode($statements[0], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), "\n";{ "old_balance": { "bank_code": "10278", "desk_code": "02204", "account_number": "00012345603", "currency_code": "EUR", "date": "2020-04-06", "amount": "16695.65" }, "new_balance": { "bank_code": "10278", "desk_code": "02204", "account_number": "00012345603", "currency_code": "EUR", "date": "2020-04-07", "amount": "16672.86" }, "operations": [ { "bank_code": "10556", "desk_code": "02204", "account_number": "00012345603", "currency_code": "EUR", "code": "B1", "date": "2020-04-07", "value_date": "2020-04-07", "label": "PRLV SEPA ONLINE SAS", "full_label": "PRLV SEPA ONLINE SAS DEDIBOX 3706114", "reference": "0000000", "amount": "-22.79", "internal_code": "6772", "reject_code": null, "exempt_code": "0", "bank_operation_reference": null, "details": [ { "bank_code": "10556", "desk_code": "02204", "account_number": "00012345603", "code": "B1", "date": "2020-04-07", "qualifier": "LIB", "additional_information": "DEDIBOX 3706114", "internal_code": "6772", "currency_code": "EUR" } ], "sepa_information": { "payer_name": null, "payer_id": null, "payer_id_type": null, "beneficiary_name": null, "beneficiary_id": null, "beneficiary_id_type": null, "ultimate_debtor_name": null, "ultimate_debtor_id": null, "ultimate_debtor_id_type": null, "ultimate_creditor_name": null, "ultimate_creditor_id": null, "ultimate_creditor_id_type": null, "remittance_information": null, "structured_remittance_information": null, "end_to_end_reference": null, "purpose": null, "remittance_reference": null, "transaction_reference": null, "original_amount": null, "beneficiary_account": null, "mandate_reference": null, "sequence_type": null, "debtor_account": null } } ]}The keys are snake_case and stable: a field the bank leaves blank is null, not missing.
Handling multiple account statements
Section titled “Handling multiple account statements”A file can hold the statements of several accounts. Group them with the account number of their balance:
<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();$statements = $reader->parse(file_get_contents('statements.txt'));
// Group the statements by account$accountStatements = [];foreach ($statements as $statement) { $accountNumber = $statement->getOldBalance()->getAccountNumber(); $accountStatements[$accountNumber][] = $statement;}
foreach ($accountStatements as $accountNumber => $statementsOfAccount) { // The statements follow the order of the file: the last one holds the latest balance $last = $statementsOfAccount[array_key_last($statementsOfAccount)];
echo sprintf("Account %s\n", $accountNumber); echo sprintf(" Statements: %d\n", count($statementsOfAccount)); echo sprintf(" Final balance: %.2f\n", $last->getNewBalance()->getAmount());}Account 00012345603 Statements: 1 Final balance: 16672.86Account 00020427603 Statements: 2 Final balance: 21632.64Labeling operation codes
Section titled “Labeling operation codes”getOperationCode() (since 6.7) gives the label of an operation code, its direction and its SWIFT equivalent:
<?php
use Silarhi\Cfonb\Cfonb120Reader;
$reader = new Cfonb120Reader();
foreach ($reader->parse(file_get_contents('statements.txt')) as $statement) { foreach ($statement->getOperations() as $operation) { // null for a code the CFONB does not define $code = $operation->getOperationCode();
echo sprintf( "%-22s %s %s (%s, %s)\n", $operation->getLabel(), $operation->getCode(), $code?->label() ?? 'Unknown code', $code?->swiftCode() ?? '-', $code?->direction()->name ?? '-' ); }}PRLV SEPA ONLINE SAS B1 Prélèvements SEPA domiciliés (DDT, Debit)PRLV SEPA FREE MOBILE B1 Prélèvements SEPA domiciliés (DDT, Debit)VIR JOHNDOE / FOOBAR 05 Virements reçus (TRF, Credit)VIR BOR TEST / FOOBAR 06 Virements émis (TRF, Debit)The whole list: operation codes.
Reading a large file
Section titled “Reading a large file”iterateFile() (since 6.7) reads the file line by line and yields one statement at a time:
<?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 operationsMore in large files.
