Injecting custom classes in Jejik/mt940

by Sander Marechal

I have just released a major update to the Jejik/mt940 library. The new 0.3 version allows you to easily extend and override the built-in classes with your own implementations. This makes it possible to easily integrate the MT940 library into your application using a database abstraction such as Active Record or ORM like Doctrine 2. As usual, you van install it through Composer.

Using the new functionality is quite straight-forward. The Jejik\MT940\Reader class now also acts as a factory for creating the various objects that make up a parsed MT940 document. You can set the factory methods using the following methods on the Reader class:

  • setStatementClass($className) defaults to Jejik\MT940\Statement
  • setAccountClass($className) defaults to Jejik\MT940\Account
  • setContraAccountClass($className) defaults to Jejik\MT940\Account
  • setTransactionClass($className) defaults to Jejik\MT940\Transaction
  • setOpeningBalanceClass($className) defaults to Jejik\MT940\Balance
  • setClosingBalanceClass($className) defaults to Jejik\MT940\Balance

You can either specify the classname as a string, or provide a PHP callable that returns an object. Your classes do not have to extend the built-in classes but they must implement the proper interfaces.

The callable for the Statement class is passed an AccountInterface and the statement sequence number as parameters. The callable for the Account class and ContraAccount class are passed the account number as parameter. The other callables are not passed any variables.

If the callable for the Account class or Statement class returns null then that statement will be skipped by the parser.

An example, integrating MT940 with your ORM:

  1. <?php
  3. use Jejik\MT940\AccountInterface;
  4. use Jejik\MT940\Reader;
  6. $db = new ORM(); // Whatever your flavour is...
  7. $reader = new Reader();
  9. $reader->setAccountClass(function ($accountNumber) use ($db) {
  10.     $account = $db::factory('My\Account')->findBy(array(
  11.         'number' => $accountNumber,
  12.     ));
  14.     return $account ?: new My\Account();
  15. });
  17. $reader->setStatementClass(function (AccountInterface $account, $number) use ($db) {
  18.     $statement = $db::factory('My\Statement')->findBy(array(
  19.         'account' => $account->getNumber(),
  20.         'number'  => $number,
  21.     ));
  23.     return $statement ?: new My\Statement();
  24. });
  26. $reader->setTransactionClass('My\Transaction')
  27.        ->setContraAccountClass('My\ContraAccount')
  28.        ->setOpeningBalanceClass('My\OpeningBalance')
  29.        ->setClosingBalanceClass('My\ClosingBalance');
  31. foreach ($reader->getStatements(file_get_contents('mt940.txt'))) {
  32.     $statement->save();
  33. }

Jejik/MT940 is licensed under the MIT license. Have fun with it!

Creative Commons Attribution-ShareAlike


#1 Richard van Wingerden

How to install your Jejik/mt940 in a Drupal 7 environment?

#2 Sander Marechal (

You can install it using Composer. Alternatively, you can download it from Github and extract it wherever you want.

Comments have been retired for this article.