Skip to content

Commit

Permalink
Add compatibility with endroid/qr-code v6
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsche committed Oct 29, 2024
1 parent 5c92924 commit ab0e74c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-5.x.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
5.4.0
-----

* QR code generation: Add compatibility with endroid/qr-code v6

5.3.2
-----

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"doctrine/data-fixtures": "^1.6",
"doctrine/persistence": "^3.2",
"easycorp/easyadmin-bundle": "^4.8.5",
"endroid/qr-code": "^4.8 || ^5.0",
"endroid/qr-code": "^4.8 || ^5.0 || ^6.0",
"friendsofphp/php-cs-fixer": "^3.49.0",
"fakerphp/faker": "^1.23",
"google/recaptcha": "^1.3",
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ parameters:
count: 1
path: src/Paginator/DoctrineORMPaginator.php

-
message: "#^Call to an undefined static method Endroid\\\\QrCode\\\\Builder\\\\Builder\\:\\:create\\(\\)\\.$#"
count: 1
path: src/Twig/Extension/QrCodeExtension.php

-
message: "#^Call to function method_exists\\(\\) with 'Endroid\\\\\\\\QrCode\\\\\\\\Builder\\\\\\\\Builder' and 'createBuilder' will always evaluate to false\\.$#"
count: 1
path: src/Twig/Extension/QrCodeExtension.php

-
message: "#^Method Leapt\\\\CoreBundle\\\\Tests\\\\LeaptCoreTestingKernel\\:\\:configureContainer\\(\\) is unused\\.$#"
count: 1
Expand Down
37 changes: 27 additions & 10 deletions src/Twig/Extension/QrCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,33 @@ public function getQrCodeFromString(string $qrCodeContent, int $size = 200, int
$roundBlockSizeMode = new RoundBlockSizeModeMargin();
}

$result = Builder::create()
->writer(new PngWriter())
->writerOptions([])
->data($qrCodeContent)
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel($errorCorrectionLevel)
->size($size)
->margin($margin)
->roundBlockSizeMode($roundBlockSizeMode)
->build();
if (method_exists(Builder::class, 'createBuilder')) {
// v4 & v5
$result = Builder::create()
->writer(new PngWriter())
->writerOptions([])
->data($qrCodeContent)
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel($errorCorrectionLevel)
->size($size)
->margin($margin)
->roundBlockSizeMode($roundBlockSizeMode)
->build();
} else {
// v6
$builder = new Builder(
writer: new PngWriter(),
writerOptions: [],
data: $qrCodeContent,
encoding: new Encoding('UTF-8'),
errorCorrectionLevel: $errorCorrectionLevel,
size: $size,
margin: $margin,
roundBlockSizeMode: $roundBlockSizeMode,
);

$result = $builder->build();
}

return $result->getDataUri();
}
Expand Down

0 comments on commit ab0e74c

Please sign in to comment.