vendor/endroid/qr-code-bundle/src/Twig/QrCodeRuntime.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * (c) Jeroen van den Enden <info@endroid.nl>
  5. *
  6. * This source file is subject to the MIT license that is bundled
  7. * with this source code in the file LICENSE.
  8. */
  9. namespace Endroid\QrCodeBundle\Twig;
  10. use Endroid\QrCode\Factory\QrCodeFactoryInterface;
  11. use Endroid\QrCode\QrCode;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Twig\Extension\RuntimeExtensionInterface;
  14. final class QrCodeRuntime implements RuntimeExtensionInterface
  15. {
  16. private $qrCodeFactory;
  17. private $urlGenerator;
  18. public function __construct(QrCodeFactoryInterface $qrCodeFactory, UrlGeneratorInterface $urlGenerator)
  19. {
  20. $this->qrCodeFactory = $qrCodeFactory;
  21. $this->urlGenerator = $urlGenerator;
  22. }
  23. public function qrCodeUrlFunction(string $text, array $options = []): string
  24. {
  25. return $this->getQrCodeReference($text, $options, UrlGeneratorInterface::ABSOLUTE_URL);
  26. }
  27. public function qrCodePathFunction(string $text, array $options = []): string
  28. {
  29. return $this->getQrCodeReference($text, $options, UrlGeneratorInterface::ABSOLUTE_PATH);
  30. }
  31. public function getQrCodeReference(string $text, array $options = [], int $referenceType): string
  32. {
  33. $qrCode = $this->qrCodeFactory->create($text, $options);
  34. if ($qrCode instanceof QrCode) {
  35. $supportedExtensions = $qrCode->getWriter()->getSupportedExtensions();
  36. $options['extension'] = current($supportedExtensions);
  37. }
  38. $options['text'] = $text;
  39. return $this->urlGenerator->generate('qr_code_generate', $options, $referenceType);
  40. }
  41. public function qrCodeDataUriFunction(string $text, array $options = []): string
  42. {
  43. return $this->qrCodeFactory->create($text, $options)->writeDataUri();
  44. }
  45. }