Skip to content

Commit

Permalink
add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Apr 23, 2021
1 parent f74b97e commit 599db5d
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/vendor
/composer.lock
/tmp
/.php_cs.cache
/.php_cs.cache
/.phpunit.result.cache
26 changes: 26 additions & 0 deletions src/Config/AbstractConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace rikudou\EuQrPayment\Config;

abstract class AbstractConfiguration implements ConfigurationInterface
{
public function getVersion(): string
{
return '002';
}

public function getCustomData(): iterable
{
return [];
}

public function getAmountPrecision(): ?int
{
return null;
}

public function getDueDateHandler(): ?DueDateHandlerInterface
{
return null;
}
}
7 changes: 7 additions & 0 deletions src/Config/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace rikudou\EuQrPayment\Config;

final class Configuration extends AbstractConfiguration
{
}
34 changes: 34 additions & 0 deletions src/Config/ConfigurationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace rikudou\EuQrPayment\Config;

interface ConfigurationInterface
{
/**
* The standard version like 001 or 002
*
* @return string
*/
public function getVersion(): string;

/**
* Additional custom data to be appended at the generated string
*
* @return iterable<string>
*/
public function getCustomData(): iterable;

/**
* The precision to be applied to the amount
*
* @return int|null
*/
public function getAmountPrecision(): ?int;

/**
* The standard does not support setting due date, you can use this to set your own non-standard implementation
*
* @return DueDateHandlerInterface|null
*/
public function getDueDateHandler(): ?DueDateHandlerInterface;
}
12 changes: 12 additions & 0 deletions src/Config/DueDateHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace rikudou\EuQrPayment\Config;

use DateTimeInterface;

interface DueDateHandlerInterface
{
public function setDueDate(DateTimeInterface $dueDate): void;

public function getDueDate(): DateTimeInterface;
}
36 changes: 33 additions & 3 deletions src/QrPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use DateTimeInterface;
use Endroid\QrCode\QrCode;
use rikudou\EuQrPayment\Config\Configuration;
use rikudou\EuQrPayment\Config\ConfigurationInterface;
use rikudou\EuQrPayment\Exceptions\InvalidIbanException;
use rikudou\EuQrPayment\Exceptions\InvalidOptionException;
use rikudou\EuQrPayment\Exceptions\UnsupportedMethodException;
Expand Down Expand Up @@ -60,18 +62,27 @@ final class QrPayment implements QrPaymentInterface
*/
private $currency = 'EUR';

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @param string|IbanInterface $iban
*/
public function __construct($iban)
public function __construct($iban, ?ConfigurationInterface $configuration = null)
{
if (is_string($iban)) {
$iban = new IBAN($iban);
}
if (!$iban instanceof IbanInterface) {
throw new \InvalidArgumentException('The IBAN must be a string or ' . IbanInterface::class . ', ' . Utils::getType($iban) . ' given');
}
if ($configuration === null) {
$configuration = new Configuration();
}
$this->iban = $iban;
$this->configuration = $configuration;
}

/**
Expand Down Expand Up @@ -318,17 +329,28 @@ public function getQrString(): string
}
}

if ($this->getAmount()) {
$amount = $this->configuration->getAmountPrecision() !== null
? number_format($this->getAmount(), $this->configuration->getAmountPrecision(), '.', '')
: $this->getAmount();
} else {
$amount = '';
}

$result[] = 'BCD'; // the service tag
$result[] = '002'; // version
$result[] = $this->configuration->getVersion();
$result[] = $this->getCharacterSet();
$result[] = 'SCT'; // identification
$result[] = $this->getBic();
$result[] = $this->getBeneficiaryName();
$result[] = $this->getIban()->asString();
$result[] = $this->getAmount() ? $this->getCurrency() . $this->getAmount() : '';
$result[] = $amount ? $this->getCurrency() . $amount : '';
$result[] = $this->getPurpose();
$result[] = $this->getRemittanceText();
$result[] = $this->getInformation();
foreach ($this->configuration->getCustomData() as $customDatum) {
$result[] = $customDatum;
}

$result = implode("\n", $result);

Expand Down Expand Up @@ -378,11 +400,19 @@ public function setOptions(array $options)

public function setDueDate(DateTimeInterface $dueDate)
{
if ($handler = $this->configuration->getDueDateHandler()) {
$handler->setDueDate($dueDate);

return $this;
}
throw new UnsupportedMethodException('The European standard does not support setting due date');
}

public function getDueDate(): DateTimeInterface
{
if ($handler = $this->configuration->getDueDateHandler()) {
return $handler->getDueDate();
}
throw new UnsupportedMethodException('The European standard does not support setting due date');
}

Expand Down

0 comments on commit 599db5d

Please sign in to comment.