Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Jun 4, 2024
1 parent d3b89df commit 7104b51
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 95 deletions.
133 changes: 51 additions & 82 deletions src/SpaydQr.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,25 @@

final class SpaydQr
{
public const QR_SIZE = 300;
public const QR_MARGIN = 0;
public const QR_CODE_WRITER = QrCodeWriter::Png;
public const QR_CODE_SIZE = 300;
public const QR_CODE_MARGIN = 0;

private function __construct(
public readonly SpaydBuilder $spayd,
private readonly BuilderInterface $qrCodeBuilder,
) {
}

/**
* @see https://qr-platba.cz/pro-vyvojare/specifikace-formatu/ Atributy, které jsou schopny zpracovat všechny banky v ČR pro tuzemský platební styk
*/
public static function create(
string $iban,
Money $amount,
DateTimeInterface $dueDate = null,
string $message = null,
int $variableSymbol = null,
int $specificSymbol = null,
int $constantSymbol = null,
QrCodeWriter $writer = QrCodeWriter::Png
): self {
$spayd = SpaydBuilder::create()
->add(SpaydKey::Iban, $iban)
->addAmount($amount);

if ($dueDate !== null) {
$spayd->add(SpaydKey::DueDate, $dueDate);
}

if ($message !== null) {
$spayd->add(SpaydKey::Message, $message);
}

if ($variableSymbol !== null) {
$spayd->add(SpaydKey::VariableSymbol, $variableSymbol);
}

if ($specificSymbol !== null) {
$spayd->add(SpaydKey::SpecificSymbol, $specificSymbol);
}

if ($constantSymbol !== null) {
$spayd->add(SpaydKey::ConstantSymbol, $constantSymbol);
}

return new self(
$spayd,
SpaydBuilder::create()
->add(SpaydKey::Iban, $iban)
->addAmount($amount),
Builder::create()
->writer($writer->endroid())
->writer(self::QR_CODE_WRITER->endroid())
->encoding(new Encoding('UTF-8')),
);
}
Expand All @@ -79,50 +48,6 @@ public static function testable(?Spayd $spayd = null, ?BuilderInterface $qrCodeB
);
}

# region QR code
public function setWriter(QrCodeWriter $writer): self
{
$this->qrCodeBuilder
->writer($writer->endroid());
return $this;
}

public function getContentType(): string
{
return $this->buildQrCode(null, null)->getMimeType();
}

public function getContent(int $size = self::QR_SIZE, int $margin = self::QR_MARGIN): string
{
return $this->buildQrCode($size, $margin)->getString();
}

public function getDataUri(int $size = self::QR_SIZE, int $margin = self::QR_MARGIN): string
{
return $this->buildQrCode($size, $margin)->getDataUri();
}

public function writeFile(string $path, int $size = self::QR_SIZE, int $margin = self::QR_MARGIN): void
{
$this->buildQrCode($size, $margin)->saveToFile($path);
}

private function buildQrCode(?int $size, ?int $margin): ResultInterface
{
$this->qrCodeBuilder->data($this->spayd->build());

if ($size !== null) {
$this->qrCodeBuilder->size($size);
}

if ($margin !== null) {
$this->qrCodeBuilder->margin($margin);
}

return $this->qrCodeBuilder->build();
}
# endregion

# region SPayD - commonly supported optional values <https://qr-platba.cz/pro-vyvojare/specifikace-formatu/> (Tabulka 3)
public function setConstantSymbol(int $constantSymbol): self
{
Expand Down Expand Up @@ -169,4 +94,48 @@ public function setVariableSymbol(int $variableSymbol): self
return $this;
}
# endregion

# region QR code
public function setWriter(QrCodeWriter $writer): self
{
$this->qrCodeBuilder
->writer($writer->endroid());
return $this;
}

public function getContentType(): string
{
return $this->buildQrCode(null, null)->getMimeType();
}

public function getContent(int $size = self::QR_CODE_SIZE, int $margin = self::QR_CODE_MARGIN): string
{
return $this->buildQrCode($size, $margin)->getString();
}

public function getDataUri(int $size = self::QR_CODE_SIZE, int $margin = self::QR_CODE_MARGIN): string
{
return $this->buildQrCode($size, $margin)->getDataUri();
}

public function writeFile(string $path, int $size = self::QR_CODE_SIZE, int $margin = self::QR_CODE_MARGIN): void
{
$this->buildQrCode($size, $margin)->saveToFile($path);
}

private function buildQrCode(?int $size, ?int $margin): ResultInterface
{
$this->qrCodeBuilder->data($this->spayd->build());

if ($size !== null) {
$this->qrCodeBuilder->size($size);
}

if ($margin !== null) {
$this->qrCodeBuilder->margin($margin);
}

return $this->qrCodeBuilder->build();
}
# endregion
}
27 changes: 14 additions & 13 deletions tests/SpaydQrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public function testGetContentWorks(?int $expectedSize, ?int $expectedMargin): v
$qrCodeResult = $this->getMockBuilder(ResultInterface::class)->getMock();
$qrCodeBuilder->expects($this->once())
->method('size')
->with($expectedSize ?: SpaydQr::QR_SIZE);
->with($expectedSize ?: SpaydQr::QR_CODE_SIZE);
$qrCodeBuilder->expects($this->once())
->method('margin')
->with($expectedMargin ?: SpaydQr::QR_MARGIN);
->with($expectedMargin ?: SpaydQr::QR_CODE_MARGIN);
$qrCodeBuilder->expects($this->once())
->method('data')
->with($expectedSPayD);
Expand Down Expand Up @@ -132,10 +132,10 @@ public function testGetDataUriWorks(?int $expectedSize, ?int $expectedMargin): v
$qrCodeResult = $this->getMockBuilder(ResultInterface::class)->getMock();
$qrCodeBuilder->expects($this->once())
->method('size')
->with($expectedSize ?: SpaydQr::QR_SIZE);
->with($expectedSize ?: SpaydQr::QR_CODE_SIZE);
$qrCodeBuilder->expects($this->once())
->method('margin')
->with($expectedMargin ?: SpaydQr::QR_MARGIN);
->with($expectedMargin ?: SpaydQr::QR_CODE_MARGIN);
$qrCodeBuilder->expects($this->once())
->method('data')
->with($expectedSPayD);
Expand Down Expand Up @@ -175,10 +175,10 @@ public function testWriteFileWorks(?int $expectedSize, ?int $expectedMargin): vo
$qrCodeResult = $this->getMockBuilder(ResultInterface::class)->getMock();
$qrCodeBuilder->expects($this->once())
->method('size')
->with($expectedSize ?: SpaydQr::QR_SIZE);
->with($expectedSize ?: SpaydQr::QR_CODE_SIZE);
$qrCodeBuilder->expects($this->once())
->method('margin')
->with($expectedMargin ?: SpaydQr::QR_MARGIN);
->with($expectedMargin ?: SpaydQr::QR_CODE_MARGIN);
$qrCodeBuilder->expects($this->once())
->method('data')
->with($expectedSPayD);
Expand All @@ -202,13 +202,14 @@ public function testEndToEnd(): void
$spaydQr = SpaydQr::create(
self::IBAN,
Money::CZK(123),
dueDate: new DateTime(),
message: 'MSG',
variableSymbol: 1,
specificSymbol: 2,
constantSymbol: 3,
writer: QrCodeWriter::Svg,
);
)
->setDueDate(new DateTime())
->setMessage('MSG')
->setVariableSymbol(1)
->setSpecificSymbol(2)
->setConstantSymbol(3)
->setWriter(QrCodeWriter::Svg)
;

$this->assertNotEmpty($spaydQr->spayd->build());
$this->assertNotEmpty($spaydQr->getDataUri());
Expand Down

0 comments on commit 7104b51

Please sign in to comment.