QrCodeFactory.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | /* |
| 5 | * (c) Jeroen van den Enden <info@endroid.nl> |
| 6 | * |
| 7 | * This source file is subject to the MIT license that is bundled |
| 8 | * with this source code in the file LICENSE. |
| 9 | */ |
| 10 | namespace WP2FA_Vendor\Endroid\QrCode\Factory; |
| 11 | |
| 12 | use WP2FA_Vendor\Endroid\QrCode\ErrorCorrectionLevel; |
| 13 | use WP2FA_Vendor\Endroid\QrCode\Exception\ValidationException; |
| 14 | use WP2FA_Vendor\Endroid\QrCode\QrCode; |
| 15 | use WP2FA_Vendor\Endroid\QrCode\QrCodeInterface; |
| 16 | use WP2FA_Vendor\Endroid\QrCode\WriterRegistryInterface; |
| 17 | use WP2FA_Vendor\Symfony\Component\OptionsResolver\OptionsResolver; |
| 18 | use WP2FA_Vendor\Symfony\Component\PropertyAccess\PropertyAccess; |
| 19 | class QrCodeFactory implements QrCodeFactoryInterface |
| 20 | { |
| 21 | private $writerRegistry; |
| 22 | /** @var OptionsResolver */ |
| 23 | private $optionsResolver; |
| 24 | /** @var array<string, mixed> */ |
| 25 | private $defaultOptions; |
| 26 | /** @var array<int, string> */ |
| 27 | private $definedOptions = ['writer', 'writer_options', 'size', 'margin', 'foreground_color', 'background_color', 'encoding', 'round_block_size', 'round_block_size_mode', 'error_correction_level', 'logo_path', 'logo_width', 'logo_height', 'label', 'label_font_size', 'label_font_path', 'label_alignment', 'label_margin', 'validate_result']; |
| 28 | /** @param array<string, mixed> $defaultOptions */ |
| 29 | public function __construct(array $defaultOptions = [], WriterRegistryInterface $writerRegistry = null) |
| 30 | { |
| 31 | $this->defaultOptions = $defaultOptions; |
| 32 | $this->writerRegistry = $writerRegistry; |
| 33 | } |
| 34 | public function create(string $text = '', array $options = []) : QrCodeInterface |
| 35 | { |
| 36 | $options = $this->getOptionsResolver()->resolve($options); |
| 37 | $accessor = PropertyAccess::createPropertyAccessor(); |
| 38 | $qrCode = new QrCode($text); |
| 39 | if ($this->writerRegistry instanceof WriterRegistryInterface) { |
| 40 | $qrCode->setWriterRegistry($this->writerRegistry); |
| 41 | } |
| 42 | foreach ($this->definedOptions as $option) { |
| 43 | if (isset($options[$option])) { |
| 44 | if ('writer' === $option) { |
| 45 | $options['writer_by_name'] = $options[$option]; |
| 46 | $option = 'writer_by_name'; |
| 47 | } |
| 48 | if ('error_correction_level' === $option) { |
| 49 | $options[$option] = new ErrorCorrectionLevel($options[$option]); |
| 50 | } |
| 51 | $accessor->setValue($qrCode, $option, $options[$option]); |
| 52 | } |
| 53 | } |
| 54 | if (!$qrCode instanceof QrCodeInterface) { |
| 55 | throw new ValidationException('QR Code was messed up by property accessor'); |
| 56 | } |
| 57 | return $qrCode; |
| 58 | } |
| 59 | private function getOptionsResolver() : OptionsResolver |
| 60 | { |
| 61 | if (!$this->optionsResolver instanceof OptionsResolver) { |
| 62 | $this->optionsResolver = $this->createOptionsResolver(); |
| 63 | } |
| 64 | return $this->optionsResolver; |
| 65 | } |
| 66 | private function createOptionsResolver() : OptionsResolver |
| 67 | { |
| 68 | $optionsResolver = new OptionsResolver(); |
| 69 | $optionsResolver->setDefaults($this->defaultOptions)->setDefined($this->definedOptions); |
| 70 | return $optionsResolver; |
| 71 | } |
| 72 | } |
| 73 |