Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Mar 11, 2015
1 parent 5120cb4 commit 34ad9ba
Show file tree
Hide file tree
Showing 26 changed files with 2,124 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.idea/
/.settings/
/vendor/
/tests/build/
.project
composer.lock
10 changes: 10 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
filter:
paths: ["src/*"]
tools:
external_code_coverage: true
php_code_coverage: true
php_sim: true
php_mess_detector: true
php_pdepend: true
php_analyzer: true
php_cpd: true
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php
php:
- 5.5
- 5.6
- hhvm
- 7
script:
- phpunit -c tests/phpunit.xml --coverage-clover=coverage.clover
before_script:
- composer install
after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
1,298 changes: 1,298 additions & 0 deletions assets/camt.053.001.02.xsd

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name" : "genkgo/camt",
"require" : {
"php" : ">=5.5",
"jschaedl/iban": "~1.1.0",
"mathiasverraes/money": "~1.2.1"
},
"require-dev" : {
"phpunit/phpunit" : "~4.0",
"phpunit/phpunit-mock-objects" : "2.3.0"
},
"autoload" : {
"psr-4" : {
"Genkgo\\Camt\\" : [
"src",
"tests"
]
}
}
}
34 changes: 34 additions & 0 deletions src/Camt053/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Genkgo\Camt\Camt053;

use Genkgo\Camt\Iban;

/**
* Class Account
* @package Genkgo\Camt\Camt053
*/
class Account {


/**
* @var Iban
*/
private $iban;

public function __construct(Iban $iban)
{
$this->iban = $iban;
}

/**
* @return Iban
*/
public function getIban()
{
return $this->iban;
}




}
78 changes: 78 additions & 0 deletions src/Camt053/Balance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace Genkgo\Camt\Camt053;

use DateTimeImmutable;
use Money\Money;

/**
* Class Balance
* @package Genkgo\Camt\Camt053
*/
class Balance {

/**
*
*/
const TYPE_OPENING = 'start';
/**
*
*/
const TYPE_CLOSING = 'closing';

/**
* @var Money
*/
private $amount;
/**
* @var string
*/
private $type;

/**
* @var DateTimeImmutable
*/
private $date;

/**
* @param Money $amount
* @param $type
*/
private function __construct ($type, Money $amount, DateTimeImmutable $date) {
$this->type = $type;
$this->amount = $amount;
$this->date = $date;
}

/**
* @return Money
*/
public function getAmount()
{
return $this->amount;
}

/**
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* @param Money $amount
* @return static
*/
public static function opening(Money $amount, DateTimeImmutable $date) {
return new static (self::TYPE_OPENING, $amount, $date);
}

/**
* @param Money $amount
* @return static
*/
public static function closing (Money $amount, DateTimeImmutable $date) {
return new static (self::TYPE_CLOSING, $amount, $date);
}

}
8 changes: 8 additions & 0 deletions src/Camt053/Entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Genkgo\Camt\Camt053;

class Entry {



}
46 changes: 46 additions & 0 deletions src/Camt053/GroupHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Genkgo\Camt\Camt053;

/**
* Class GroupHeader
* @package Genkgo\Camt\Camt053
*/
class GroupHeader {

/**
* @var string
*/
private $messageId;
/**
* @var \DateTimeImmutable
*/
private $createdOn;

/**
* @param $messageId
* @param \DateTimeImmutable $createdOn
*/
public function __construct($messageId, \DateTimeImmutable $createdOn)
{
$this->messageId = $messageId;
$this->createdOn = $createdOn;
}

/**
* @return string
*/
public function getMessageId()
{
return $this->messageId;
}

/**
* @return \DateTimeImmutable
*/
public function getCreatedOn()
{
return $this->createdOn;
}


}
129 changes: 129 additions & 0 deletions src/Camt053/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
namespace Genkgo\Camt\Camt053;

use DateTimeImmutable;
use DOMDocument;
use Genkgo\Camt\Exception\InvalidMessageException;
use Genkgo\Camt\Iban;
use Money\Currency;
use Money\Money;

/**
* Class Message
* @package Genkgo\Camt\Camt053
*/
class Message {

/**
* @var \SimpleXMLElement[]
*/
private $document;
/**
* @var
*/
private $groupHeader;
/**
* @var
*/
private $statements;

/**
* @param DOMDocument $document
* @throws InvalidMessageException
*/
public function __construct(DOMDocument $document) {
$this->validate($document);
$this->document = simplexml_import_dom($document);
}

/**
* @return GroupHeader
*/
public function getGroupHeader() {
if ($this->groupHeader === null) {
$groupHeaderXml = $this->document->BkToCstmrStmt->GrpHdr;

$this->groupHeader = new GroupHeader(
(string) $groupHeaderXml->MsgId,
new DateTimeImmutable((string) $groupHeaderXml->CreDtTm)
);
}

return $this->groupHeader;
}

/**
* @return Statement[]
*/
public function getStatements() {
if ($this->statements === null) {
$this->statements = [];

$statementsXml = $this->document->BkToCstmrStmt->Stmt;
foreach ($statementsXml as $statementXml) {
$statement = new Statement(
$statementXml->Id,
new DateTimeImmutable((string) $statementXml->CreDtTm),
new Account(new Iban((string) $statementXml->Acct->Id->IBAN))
);

$this->addBalancesToStatement($statementXml, $statement);

$this->statements[] = $statement;
}

}

return $this->statements;
}

/**
* @param DOMDocument $document
* @throws InvalidMessageException
*/
private function validate (DOMDocument $document) {
libxml_use_internal_errors(true);
$valid = $document->schemaValidate(dirname(dirname(__DIR__)).'/assets/camt.053.001.02.xsd', LIBXML_SCHEMA_CREATE);
$errors = libxml_get_errors();
libxml_clear_errors();

if (!$valid) {
throw new InvalidMessageException("Provided XML is not valid according to the XSD");
}
}

/**
* @param $statementXml
* @param $statement
*/
private function addBalancesToStatement($statementXml, $statement)
{
$balancesXml = $statementXml->Bal;
foreach ($balancesXml as $balanceXml) {
$amount = (string)$balanceXml->Amt;
$currency = (string)$balanceXml->Amt['Ccy'];
$date = (string)$balanceXml->Dt->Dt;

if ($balanceXml->Tp->CdOrPrtry->Cd === 'OPBD') {
$balance = Balance::opening(
new Money(
Money::stringToUnits($amount),
new Currency($currency)
),
new DateTimeImmutable($date)
);
} else {
$balance = Balance::closing(
new Money(
Money::stringToUnits($amount),
new Currency($currency)
),
new DateTimeImmutable($date)
);
}

$statement->addBalance($balance);
}
}

}
29 changes: 29 additions & 0 deletions src/Camt053/MessageFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Genkgo\Camt\Camt053;

use DOMDocument;
use Genkgo\Camt\MessageFormatInterface;

class MessageFormat implements MessageFormatInterface {

public function getXmlNs()
{
return 'urn:iso:std:iso:20022:tech:xsd:camt.053.001.02';
}

public function getMsgId()
{
return 'camt.053.001.02';
}

public function getName()
{
return 'BankToCustomerStatementV02';
}

public function getMessage(DOMDocument $document)
{
return new Message($document);
}

}
Loading

0 comments on commit 34ad9ba

Please sign in to comment.