Skip to content

Commit

Permalink
Allow custom schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
z38 committed Mar 25, 2018
1 parent 3eefd9f commit e0cadcf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
19 changes: 13 additions & 6 deletions src/Z38/SwissPayment/Message/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
abstract class AbstractMessage implements MessageInterface
{
const SCHEMA_LOCATION = 'http://www.six-interbank-clearing.com/de/%s';

/**
* Builds the DOM of the actual message
*
Expand All @@ -27,6 +25,13 @@ abstract protected function buildDom(\DOMDocument $doc);
*/
abstract protected function getSchemaName();

/**
* Gets the location of the schema
*
* @return string|null The location or null
*/
abstract protected function getSchemaLocation();

/**
* Builds a DOM document of the message
*
Expand All @@ -35,13 +40,15 @@ abstract protected function getSchemaName();
public function asDom()
{
$schema = $this->getSchemaName();
$ns = sprintf(self::SCHEMA_LOCATION, $schema);
$location = $this->getSchemaLocation();

$doc = new \DOMDocument('1.0', 'UTF-8');
$root = $doc->createElement('Document');
$root->setAttribute('xmlns', $ns);
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$root->setAttribute('xsi:schemaLocation', sprintf('%s %s', $ns, $schema));
$root->setAttribute('xmlns', $schema);
if ($location !== null) {
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$root->setAttribute('xsi:schemaLocation', $schema.' '.$location);
}
$root->appendChild($this->buildDom($doc));
$doc->appendChild($root);

Expand Down
8 changes: 8 additions & 0 deletions src/Z38/SwissPayment/Message/CustomerCreditTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public function getPaymentCount()
* {@inheritdoc}
*/
protected function getSchemaName()
{
return 'http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd';
}

/**
* {@inheritdoc}
*/
protected function getSchemaLocation()
{
return 'pain.001.001.03.ch.02.xsd';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

class CustomerCreditTransferTest extends TestCase
{
const SCHEMA = 'pain.001.001.03.ch.02.xsd';
const NS_URI_ROOT = 'http://www.six-interbank-clearing.com/de/';
const SCHEMA = 'http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd';
const SCHEMA_PATH = 'pain.001.001.03.ch.02.xsd';

protected function buildMessage()
{
Expand Down Expand Up @@ -194,7 +194,7 @@ public function testGroupHeader()
$doc = new \DOMDocument();
$doc->loadXML($xml);
$xpath = new \DOMXPath($doc);
$xpath->registerNamespace('pain001', self::NS_URI_ROOT.self::SCHEMA);
$xpath->registerNamespace('pain001', self::SCHEMA);

$nbOfTxs = $xpath->evaluate('string(//pain001:GrpHdr/pain001:NbOfTxs)');
$this->assertEquals('12', $nbOfTxs);
Expand All @@ -206,7 +206,7 @@ public function testGroupHeader()
public function testSchemaValidation()
{
$xml = $this->buildMessage()->asXml();
$schemaPath = __DIR__.'/../../../../'.self::SCHEMA;
$schemaPath = __DIR__.'/../../../../'.self::SCHEMA_PATH;

$doc = new \DOMDocument();
$doc->loadXML($xml);
Expand Down

0 comments on commit e0cadcf

Please sign in to comment.