Skip to content

Recipes

The recipes parse the sample statement file: three statements of two accounts.

Operations above 100 in absolute value, debits and credits alike:

recipe-filter.php
<?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());
}
}
Output
Large operation: 5000.00 - VIR JOHNDOE / FOOBAR
recipe-summary.php
<?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']);
Output
Total credits: 5000.00
Total debits: 63.01
Operations: 4

Amounts are floats by default: add the exact decimal strings of getAmount(true) (since 6.6) with bcmath when the result must be exact.

For an API response, or to store the statements: since 6.7, every object has a toArray() and is JsonSerializable.

recipe-array.php
<?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-d
echo json_encode($statements[0], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), "\n";
Output
{
"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.

A file can hold the statements of several accounts. Group them with the account number of their balance:

recipe-accounts.php
<?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());
}
Output
Account 00012345603
Statements: 1
Final balance: 16672.86
Account 00020427603
Statements: 2
Final balance: 21632.64

getOperationCode() (since 6.7) gives the label of an operation code, its direction and its SWIFT equivalent:

operation-codes.php
<?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 ?? '-'
);
}
}
Output
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.

iterateFile() (since 6.7) reads the file line by line and yields one statement at a time:

cfonb120-iterate.php
<?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 file
foreach ($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())
);
}
Output
00012345603 16695.65 → 16672.86 1 operations
00020427603 16672.86 → 21632.64 3 operations
00020427603 21632.64 → 21632.64 0 operations

More in large files.