Skip to content

Commit

Permalink
Merge pull request #40 from sprain/set-image-format-in-output
Browse files Browse the repository at this point in the history
Add setQrCodeImageFormat functionality to AbstractOutput
  • Loading branch information
sprain authored Apr 28, 2020
2 parents 16566e5 + 6a66f7f commit bc93e74
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 12 deletions.
6 changes: 5 additions & 1 deletion example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@

// … or output a full payment part
$output = new QrBill\PaymentPart\Output\HtmlOutput\HtmlOutput($qrBill, 'en');
print $output->setPrintable(false)->getPaymentPart();

print $output
->setPrintable(false)
->setQrCodeImageFormat(QrBill\QrCode\QrCode::FILE_FORMAT_SVG)
->getPaymentPart();
6 changes: 5 additions & 1 deletion example/example_scor.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@

// … or output a full payment part
$output = new QrBill\PaymentPart\Output\HtmlOutput\HtmlOutput($qrBill, 'en');
print $output->setPrintable(false)->getPaymentPart();

print $output
->setPrintable(false)
->setQrCodeImageFormat(QrBill\QrCode\QrCode::FILE_FORMAT_SVG)
->getPaymentPart();
25 changes: 25 additions & 0 deletions src/PaymentPart/Output/AbstractOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sprain\SwissQrBill\PaymentPart\Output\Element\Text;
use Sprain\SwissQrBill\PaymentPart\Output\Element\Title;
use Sprain\SwissQrBill\QrBill;
use Sprain\SwissQrBill\QrCode\QrCode;

abstract class AbstractOutput
{
Expand All @@ -19,11 +20,15 @@ abstract class AbstractOutput
/** @var bool */
protected $printable;

/** @var string */
private $qrCodeImageFormat;

public function __construct(QrBill $qrBill, string $language)
{
$this->qrBill = $qrBill;
$this->language = $language;
$this->printable = false;
$this->qrCodeImageFormat = QrCode::FILE_FORMAT_SVG;
}

public function getQrBill(): ?QrBill
Expand All @@ -48,6 +53,18 @@ public function isPrintable(): bool
return $this->printable;
}

public function setQrCodeImageFormat(string $fileExtension): self
{
$this->qrCodeImageFormat = $fileExtension;

return $this;
}

public function getQrCodeImageFormat(): string
{
return $this->qrCodeImageFormat;
}

protected function getInformationElements(): array
{
$informationElements = [];
Expand Down Expand Up @@ -151,4 +168,12 @@ protected function getFurtherInformationElements(): array

return $furtherInformationElements;
}

protected function getQrCode(): QrCode
{
$qrCode = $this->qrBill->getQrCode();
$qrCode->setWriterByExtension($this->getQrCodeImageFormat());

return $qrCode;
}
}
4 changes: 1 addition & 3 deletions src/PaymentPart/Output/HtmlOutput/HtmlOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public function getPaymentPart(): string

private function addSwissQrCodeImage(string $paymentPart): string
{
$qrCode = $this->qrBill->getQrCode();
$qrCode->setWriterByExtension('svg');

$qrCode = $this->getQrCode();
$paymentPart = str_replace('{{ swiss-qr-image }}', $qrCode->writeDataUri(), $paymentPart);

return $paymentPart;
Expand Down
4 changes: 4 additions & 0 deletions src/PaymentPart/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public function getPaymentPart();
public function setPrintable(bool $printable);

public function isPrintable(): bool;

public function setQrCodeImageFormat(string $imageFormat);

public function getQrCodeImageFormat(): string;
}
24 changes: 17 additions & 7 deletions src/QrCode/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@

use Endroid\QrCode\QrCode as BaseQrCode;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\WriterRegistry;
use Sprain\SwissQrBill\QrCode\Exception\UnsupportedFileExtensionException;

class QrCode extends BaseQrCode implements QrCodeInterface
{
const FILE_FORMAT_PNG = 'png';
const FILE_FORMAT_SVG = 'svg';

// A file extension is supported if the underlying library supports it,
// including the possibility to add a logo in the center of the qr code.
private const SUPPORTED_EXTENSIONS = ['png', 'svg'];
const SUPPORTED_FILE_FORMATS = [
self::FILE_FORMAT_PNG,
self::FILE_FORMAT_SVG
];

public function writeFile(string $path): void
{
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$this->setWriterByExtension($extension);
parent::writeFile($path);
}

if (!in_array($extension, self::SUPPORTED_EXTENSIONS)) {
public function setWriterByExtension(string $extension): void
{
if (!in_array($extension, self::SUPPORTED_FILE_FORMATS)) {
throw new UnsupportedFileExtensionException(sprintf(
'Your file cannot be saved. Only these file extensions are supported: %s',
implode(', ', self::SUPPORTED_EXTENSIONS)
'The qr code file cannot be created. Only these file extensions are supported: %s. You provided: %s.',
implode(', ', self::SUPPORTED_FILE_FORMATS),
$extension
));
}

$this->setWriterByExtension($extension);
parent::writeFile($path);
parent::setWriterByExtension($extension);
}
}
7 changes: 7 additions & 0 deletions tests/PaymentPart/Output/HtmlOutput/HtmlOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\HtmlOutput;
use Sprain\SwissQrBill\QrBill;
use Sprain\SwissQrBill\QrCode\QrCode;
use Sprain\Tests\SwissQrBill\TestQrBillCreatorTrait;

class HtmlOutputTest extends TestCase
Expand All @@ -21,12 +22,17 @@ public function testValidQrBills(string $name, QrBill $qrBill)
$variations = [
[
'printable' => false,
'format' => QrCode::FILE_FORMAT_SVG,
'file' => __DIR__ . '/../../../TestData/HtmlOutput/' . $name . '.html'
],
[
'printable' => true,
'format' => QrCode::FILE_FORMAT_SVG,
'file' => __DIR__ . '/../../../TestData/HtmlOutput/' . $name . '.print.html'
]

// Note: Testing the exact output with a png qr code is not possible, as the png contents are
// not always exactly the same on each server configuration.
];

foreach ($variations as $variation) {
Expand All @@ -35,6 +41,7 @@ public function testValidQrBills(string $name, QrBill $qrBill)

$htmlOutput = (new HtmlOutput($qrBill, 'en'));
$htmlOutput->setPrintable($variation['printable']);
$htmlOutput->setQrCodeImageFormat($variation['format']);
$output = $htmlOutput->getPaymentPart();

if ($this->regenerateReferenceHtmlOutputs) {
Expand Down

0 comments on commit bc93e74

Please sign in to comment.