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:
- <?php
- use Jejik\MT940\AccountInterface;
- use Jejik\MT940\Reader;
- $db = new ORM(); // Whatever your flavour is...
- $reader = new Reader();
- $reader->setAccountClass(function ($accountNumber) use ($db) {
- $account = $db::factory('My\Account')->findBy(array(
- 'number' => $accountNumber,
- ));
- return $account ?: new My\Account();
- });
- $reader->setStatementClass(function (AccountInterface $account, $number) use ($db) {
- $statement = $db::factory('My\Statement')->findBy(array(
- 'account' => $account->getNumber(),
- 'number' => $number,
- ));
- return $statement ?: new My\Statement();
- });
- $reader->setTransactionClass('My\Transaction')
- ->setContraAccountClass('My\ContraAccount')
- ->setOpeningBalanceClass('My\OpeningBalance')
- ->setClosingBalanceClass('My\ClosingBalance');
- foreach ($reader->getStatements(file_get_contents('mt940.txt'))) {
- $statement->save();
- }
Jejik/MT940 is licensed under the MIT license. Have fun with it!
Comments
#1 Richard van Wingerden
#2 Sander Marechal (http://www.jejik.com)
Comments have been retired for this article.