Skip to content
This repository has been archived by the owner on Nov 23, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from kouinkouin/master
Browse files Browse the repository at this point in the history
TransferMessage::generate returns a string
  • Loading branch information
netsensei authored Oct 3, 2017
2 parents 92b5a50 + bd2e205 commit aad0e84
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 88 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm
- 7.0
- 7.1

before_script:
- travis_retry composer self-update
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/thephpleague/:package_name).
We accept contributions via Pull Requests on [Github](https://github.com/kouinkouin/be-bank-transfer-message/pulls).


## Pull Requests
Expand Down
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# BE Bank Transfer Message

[![Latest Version](https://img.shields.io/github/release/netsensei/be-bank-transfer-message.svg?style=flat-square)](https://github.com/netsensei/be-bank-transfer-message/releases)
[![Latest Version](https://img.shields.io/github/release/kouinkouin/be-bank-transfer-message.svg?style=flat-square)](https://github.com/kouinkouin/be-bank-transfer-message/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/netsensei/be-bank-transfer-message/master.svg?style=flat-square)](https://travis-ci.org/netsensei/be-bank-transfer-message)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/thephpleague/:package_name.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/:package_name/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/thephpleague/:package_name.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/:package_name)
[![Total Downloads](https://img.shields.io/packagist/dt/netsensei/be-bank-transfer-message.svg?style=flat-square)](https://packagist.org/packages/netsensei/be-bank-transfer-message)
[![Build Status](https://img.shields.io/travis/kouinkouin/be-bank-transfer-message/master.svg?style=flat-square)](https://travis-ci.org/kouinkouin/be-bank-transfer-message)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/kouinkouin/be-bank-transfer-message.svg?style=flat-square)](https://scrutinizer-ci.com/g/kouinkouin/be-bank-transfer-message/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/kouinkouin/be-bank-transfer-message.svg?style=flat-square)](https://scrutinizer-ci.com/g/kouinkouin/be-bank-transfer-message)
[![Total Downloads](https://img.shields.io/packagist/dt/kouinkouin/be-bank-transfer-message.svg?style=flat-square)](https://packagist.org/packages/kouinkouin/be-bank-transfer-message)

This package contains a validator and generator for structured messages included in Belgian bank transfers. Common use cases:

Expand All @@ -19,7 +19,7 @@ The structured message format adheres to the [Febelfin Guidelines](https://www.f
Via Composer

``` bash
$ composer require netsensei/be-bank-transfer-message
$ composer require kouinkouin/be-bank-transfer-message
```

## Usage
Expand All @@ -29,14 +29,14 @@ $ composer require netsensei/be-bank-transfer-message
Based on a random number

``` php
$transferMessage = new Netsensei\BeBankTransferMessage\TransferMessage();
$transferMessage = new BeBankTransferMessage\TransferMessage();
echo transferMessage->getStructuredMessage();
```

Based on a predefined number

``` php
$transferMessage = new Netsensei\BeBankTransferMessage\TransferMessage(12345);
$transferMessage = new BeBankTransferMessage\TransferMessage(12345);
echo transferMessage->getStructuredMessage();
```

Expand Down Expand Up @@ -69,15 +69,15 @@ echo transferMessage->getStructuredMessage();
A valid message

``` php
$transferMessage = new Netsensei\BeBankTransferMessage\TransferMessage();
$transferMessage = new BeBankTransferMessage\TransferMessage();
$transferMessage->setStructuredMessage('+++090/9337/55493+++');
$result = $transferMessage->validate(); // TRUE
```

An invalid message

``` php
$transferMessage = new Netsensei\BeBankTransferMessage\TransferMessage();
$transferMessage = new BeBankTransferMessage\TransferMessage();
$transferMessage->setStructuredMessage('+++011/9337/55493+++');
$result = $transferMessage->validate(); // FALSE
```
Expand All @@ -92,11 +92,12 @@ $ phpunit

## Contributing

Please see [CONTRIBUTING](https://github.com/thephpleague/:package_name/blob/master/CONTRIBUTING.md) for details.
Please see [CONTRIBUTING](https://github.com/kouinkouin/be-bank-transfer-message/blob/master/CONTRIBUTING.md) for details.

## Credits

- [Matthias Vandermaesen](https://github.com/netsensei)
- [Matthias Vandermaesen](https://github.com/Netsensei)
- [kouinkouin](https://github.com/kouinkouin)

## License

Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "netsensei/be-bank-transfer-message",
"name": "kouinkouin/be-bank-transfer-message",
"description": "Generate and validate Belgian bank transfer structured messages",
"keywords": [
"netsensei",
"kouinkouin",
"be-bank-transfer-message",
"finance",
"belgium",
Expand All @@ -12,18 +12,23 @@
"order",
"payment"
],
"homepage": "https://github.com/netsensei/be-bank-transfer-message",
"homepage": "https://github.com/kouinkouin/be-bank-transfer-message",
"license": "MIT",
"authors": [
{
"name": "Matthias Vandermaesen",
"email": "[email protected]",
"homepage": "http://www.colada.be",
"role": "lead"
},
{
"name": "kouinkouin",
"email": "[email protected]",
"role": "lead"
}
],
"require": {
"php" : ">=5.3.0"
"php" : ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit" : "4.*"
Expand Down
136 changes: 67 additions & 69 deletions src/TransferMessage.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
<?php

/**
* This file is part of the netsensei.bebanktransfermessage library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/netsensei/be-bank-transfer-message/
* @version 1.0.0
* @package netsensei.bebanktransfermessage
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Netsensei\BeBankTransferMessage;

use Netsensei\BeBankTransferMessage\Exception\TransferMessageException;
Expand Down Expand Up @@ -50,7 +38,7 @@ class TransferMessage
/**
* A structured message with a valid formatting
*
* @var int
* @var string
*/
private $structuredMessage = null;

Expand All @@ -65,23 +53,75 @@ public function __construct($number = null)
$this->generate();
}

/**
* Generate a valid structured message based on the number
*
* @param string $circumfix The circumfix. Defaults to the plus sign
*
* @return string A valid structured message
*/
public function generate($circumfix = self::CIRCUMFIX_PLUS)
{
$this->modulus = $this->mod($this->number);

$structuredMessage = str_pad($this->number, 10, 0, STR_PAD_LEFT).str_pad($this->modulus, 2, 0, STR_PAD_LEFT);

$pattern = ['/^([0-9]{3})([0-9]{4})([0-9]{5})$/'];
$replace = [str_pad('$1/$2/$3', 14, $circumfix, STR_PAD_BOTH)];
$this->structuredMessage = preg_replace($pattern, $replace, $structuredMessage);

return $this->structuredMessage;
}

/**
* The mod97 calculation
*
* If the modulus is 0, the result is substituted to 97
*
* @param int $dividend The dividend
*
* @return int The modulus
*/
private function mod($dividend)
{
$modulus = $dividend % self::MODULO;

return ($modulus > 0) ? $modulus : self::MODULO;
}

/**
* Get the number
*
* @return int The number used to generate a structured message
*/
public function getNumber()
{
return $this->number;
}

/**
* Set the number
*
* If no number is passed to this method, a random number will be generated
*
* @param int $number The number used to generate a structured message
*
* @throws \TransferMessageException If the number is out of bounds
* @throws TransferMessageException If the number is out of bounds
*/
public function setNumber($number = null)
{
try {
if (is_null($number)) {
$this->number = mt_rand(1, 9999999999);
if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
$this->number = random_int(1, 9999999999);
} else {
$this->number = mt_rand(1, 9999999999);
}
} else {
if (($number < 1) || ($number > 9999999999)) {
throw new \InvalidArgumentException('The number should be an integer larger then 0 and smaller then 9999999999.');
throw new \InvalidArgumentException(
'The number should be an integer larger then 0 and smaller then 9999999999.'
);
}

$this->number = $number;
Expand All @@ -92,31 +132,31 @@ public function setNumber($number = null)
}

/**
* Get the number
* Get the modulus
*
* @return int The number used to generate a structured message
* @return int The modulus resulting from the modulo operation
*/
public function getNumber()
public function getModulus()
{
return $this->number;
return $this->modulus;
}

/**
* Get the modulus
* Get the structured message
*
* @return int The modulus resulting from the modulo operation
* @return string A valid formatted structured message
*/
public function getModulus()
public function getStructuredMessage()
{
return $this->modulus;
return $this->structuredMessage;
}

/**
* Set a structured message
*
* @param string $structuredMessage A structured message
*
* @throws \TransferMessageException If the format is not valid
* @throws TransferMessageException If the format is not valid
*/
public function setStructuredMessage($structuredMessage)
{
Expand All @@ -132,33 +172,6 @@ public function setStructuredMessage($structuredMessage)
}
}

/**
* Get the structured message
*
* @return string A valid formatted structured message
*/
public function getStructuredMessage()
{
return $this->structuredMessage;
}

/**
* Generate a valid structured message based on the number
*
* @param string $circumfix The circumfix. Defaults to the plus sign
* @return string A valid structured message
*/
public function generate($circumfix = self::CIRCUMFIX_PLUS)
{
$this->modulus = $this->mod($this->number);

$structuredMessage = str_pad($this->number, 10, 0, STR_PAD_LEFT).str_pad($this->modulus, 2, 0, STR_PAD_LEFT);

$pattern = array('/^([0-9]{3})([0-9]{4})([0-9]{5})$/');
$replace = array(str_pad('$1/$2/$3', 14, $circumfix, STR_PAD_BOTH));
$this->structuredMessage = preg_replace($pattern, $replace, $structuredMessage);
}

/**
* Validates a structured message
*
Expand All @@ -169,28 +182,13 @@ public function generate($circumfix = self::CIRCUMFIX_PLUS)
*/
public function validate()
{
$pattern = array('/^[\+\*]{3}([0-9]{3})[\/]?([0-9]{4})[\/]?([0-9]{5})[\+\*]{3}$/');
$replace = array('${1}${2}${3}');
$pattern = ['/^[\+\*]{3}([0-9]{3})[\/]?([0-9]{4})[\/]?([0-9]{5})[\+\*]{3}$/'];
$replace = ['${1}${2}${3}'];
$rawStructuredMessage = preg_replace($pattern, $replace, $this->structuredMessage);

$number = substr($rawStructuredMessage, 0, 10);
$modulus = substr($rawStructuredMessage, 10, 2);

return ($modulus == $this->mod($number)) ? true : false;
}

/**
* The mod97 calculation
*
* If the modulus is 0, the result is substituted to 97
*
* @param int $dividend The dividend
* @return int The modulus
*/
private function mod($dividend)
{
$modulus = $dividend % self::MODULO;

return ($modulus > 0) ? $modulus : self::MODULO;
}
}

0 comments on commit aad0e84

Please sign in to comment.