Skip to content

Commit

Permalink
Improve injection of unsupportedCharacterReplacements
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Jun 23, 2024
1 parent 0e99f88 commit 2021479
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/QrBill.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ final class QrBill implements SelfValidatableInterface
private ?PaymentReference $paymentReference = null;
private ?AdditionalInformation $additionalInformation = null;

/** @var array<string, string> */
private array $unsupportedCharacterReplacements = [];

/** @var AlternativeScheme[] */
private array $alternativeSchemes = [];

Expand Down Expand Up @@ -162,10 +165,14 @@ public function addAlternativeScheme(AlternativeScheme $alternativeScheme): self
return $this;
}

/**
* @throws InvalidQrBillDataException
*/
public function getQrCode(?string $fileFormat = null, array $unsupportedCharacterReplacements = []): QrCode
public function setUnsupportedCharacterReplacements(array $unsupportedCharacterReplacements): self

Check failure on line 168 in src/QrBill.php

View workflow job for this annotation

GitHub Actions / Static code analysis

Method Sprain\SwissQrBill\QrBill::setUnsupportedCharacterReplacements() has parameter $unsupportedCharacterReplacements with no value type specified in iterable type array.
{
$this->unsupportedCharacterReplacements = $unsupportedCharacterReplacements;

return $this;
}

public function getQrCode(?string $fileFormat = null): QrCode
{
if (!$this->isValid()) {
throw new InvalidQrBillDataException(
Expand All @@ -176,7 +183,7 @@ public function getQrCode(?string $fileFormat = null, array $unsupportedCharacte
return QrCode::create(
$this->getQrCodeContent(),
$fileFormat,
$unsupportedCharacterReplacements
$this->unsupportedCharacterReplacements
);
}

Expand Down
18 changes: 18 additions & 0 deletions src/QrCode/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ final class QrCode
/** @var array<string, bool> $writerOptions */
private array $writerOptions = [SvgWriter::WRITER_OPTION_FORCE_XLINK_HREF => true];

/**
* @param string $data
* @param string|null $fileFormat
* @param array<string, string> $unsupportedCharacterReplacements
* @return self
* @throws UnsupportedFileExtensionException
*/
public static function create(string $data, string $fileFormat = null, array $unsupportedCharacterReplacements = []): self
{
if (null === $fileFormat) {
Expand All @@ -45,6 +52,12 @@ public static function create(string $data, string $fileFormat = null, array $un
return new self($data, $fileFormat, $unsupportedCharacterReplacements);
}

/**
* @param string $data
* @param string $fileFormat
* @param array<string, string> $unsupportedCharacterReplacements
* @throws UnsupportedFileExtensionException
*/
private function __construct(string $data, string $fileFormat, array $unsupportedCharacterReplacements)
{
$data = $this->replaceUnsupportedCharacters($data, $unsupportedCharacterReplacements);
Expand Down Expand Up @@ -132,6 +145,11 @@ public function avoidCompactSvgs(): void
}
}

/**
* @param string $data
* @param array<string, string> $unsupportedCharacterReplacements
* @return string
*/
private function replaceUnsupportedCharacters(string $data, array $unsupportedCharacterReplacements): string
{
foreach ($unsupportedCharacterReplacements as $character => $replacement) {
Expand Down
4 changes: 3 additions & 1 deletion tests/QrBillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,11 @@ public function testItConsidersReplacementCharacters()
'»' => '"',
];

$qrBill->setUnsupportedCharacterReplacements($unsupportedCharacterReplacements);

$this->assertStringContainsString(
'Team "We are the Champions!"',
$qrBill->getQrCode(null, $unsupportedCharacterReplacements)->getText()
$qrBill->getQrCode()->getText()
);
}

Expand Down

0 comments on commit 2021479

Please sign in to comment.