ILIAS  trunk Revision v12.0_alpha-1541-g23eaa5e013d
ToSvgQrCodeTransformation.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
21
28use BaconQrCode as External;
29
31{
34
35 protected const string ENCODING = 'UTF-8';
36
37 public function __construct(
38 protected ErrorCorrectionLevel $error_correction_level,
39 protected int $size_in_px,
40 ) {
41 $this->assertIntGreaterThanZero($size_in_px);
42 }
43
44 public function transform(mixed $from): SVG
45 {
46 if (!$from instanceof URI) {
47 throw new \InvalidArgumentException("Argument must be of type " . URI::class);
48 }
49
50 $writer = new External\Writer(
51 new External\Renderer\ImageRenderer(
52 new External\Renderer\RendererStyle\RendererStyle($this->size_in_px),
53 new External\Renderer\Image\SvgImageBackEnd(),
54 ),
55 );
56
57 $raw_svg_string = $writer->writeString(
58 $from->__toString(),
59 self::ENCODING,
60 $this->mapErrorCorrectionLevel($this->error_correction_level),
61 );
62
63 return new SVG($raw_svg_string);
64 }
65
66 protected function mapErrorCorrectionLevel(ErrorCorrectionLevel $level): External\Common\ErrorCorrectionLevel
67 {
68 return match ($level) {
69 ErrorCorrectionLevel::LOW => External\Common\ErrorCorrectionLevel::L(),
70 ErrorCorrectionLevel::MEDIUM => External\Common\ErrorCorrectionLevel::M(),
71 ErrorCorrectionLevel::QUARTILE => External\Common\ErrorCorrectionLevel::Q(),
72 ErrorCorrectionLevel::HIGH => External\Common\ErrorCorrectionLevel::H(),
73 };
74 }
75
76 protected function assertIntGreaterThanZero(int $number): void
77 {
78 if (0 >= $number) {
79 throw new \InvalidArgumentException("Number must be greater than zero.");
80 }
81 }
82}
Data transfer object that carries the raw SVG data as a string, created from trusted sources.
Definition: SVG.php:30
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
__construct(protected ErrorCorrectionLevel $error_correction_level, protected int $size_in_px,)
A transformation is a function from one datatype to another.
An entity that renders components to a string output.
Definition: Renderer.php:31
ErrorCorrectionLevel
Error correction levels as defined by ISO/IEC 18004.
@ HIGH
~7% of codewords can be restored.