QrCodeFactory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\QrCode\Factory;
  10. use Endroid\QrCode\ErrorCorrectionLevel;
  11. use Endroid\QrCode\Exception\ValidationException;
  12. use Endroid\QrCode\QrCode;
  13. use Endroid\QrCode\QrCodeInterface;
  14. use Endroid\QrCode\WriterRegistryInterface;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. use Symfony\Component\PropertyAccess\PropertyAccess;
  17. class QrCodeFactory implements QrCodeFactoryInterface
  18. {
  19. private $writerRegistry;
  20. /** @var OptionsResolver */
  21. private $optionsResolver;
  22. private $defaultOptions;
  23. /** @var array */
  24. private $definedOptions = [
  25. 'writer',
  26. 'writer_options',
  27. 'size',
  28. 'margin',
  29. 'foreground_color',
  30. 'background_color',
  31. 'encoding',
  32. 'round_block_size',
  33. 'error_correction_level',
  34. 'logo_path',
  35. 'logo_width',
  36. 'logo_height',
  37. 'label',
  38. 'label_font_size',
  39. 'label_font_path',
  40. 'label_alignment',
  41. 'label_margin',
  42. 'validate_result',
  43. ];
  44. public function __construct(array $defaultOptions = [], WriterRegistryInterface $writerRegistry = null)
  45. {
  46. $this->defaultOptions = $defaultOptions;
  47. $this->writerRegistry = $writerRegistry;
  48. }
  49. public function create(string $text = '', array $options = []): QrCodeInterface
  50. {
  51. $options = $this->getOptionsResolver()->resolve($options);
  52. $accessor = PropertyAccess::createPropertyAccessor();
  53. $qrCode = new QrCode($text);
  54. if ($this->writerRegistry instanceof WriterRegistryInterface) {
  55. $qrCode->setWriterRegistry($this->writerRegistry);
  56. }
  57. foreach ($this->definedOptions as $option) {
  58. if (isset($options[$option])) {
  59. if ('writer' === $option) {
  60. $options['writer_by_name'] = $options[$option];
  61. $option = 'writer_by_name';
  62. }
  63. if ('error_correction_level' === $option) {
  64. $options[$option] = new ErrorCorrectionLevel($options[$option]);
  65. }
  66. $accessor->setValue($qrCode, $option, $options[$option]);
  67. }
  68. }
  69. if (!$qrCode instanceof QrCodeInterface) {
  70. throw new ValidationException('QR Code was messed up by property accessor');
  71. }
  72. return $qrCode;
  73. }
  74. private function getOptionsResolver(): OptionsResolver
  75. {
  76. if (!$this->optionsResolver instanceof OptionsResolver) {
  77. $this->optionsResolver = $this->createOptionsResolver();
  78. }
  79. return $this->optionsResolver;
  80. }
  81. private function createOptionsResolver(): OptionsResolver
  82. {
  83. $optionsResolver = new OptionsResolver();
  84. $optionsResolver
  85. ->setDefaults($this->defaultOptions)
  86. ->setDefined($this->definedOptions)
  87. ;
  88. return $optionsResolver;
  89. }
  90. }