-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQRCode.php
More file actions
124 lines (103 loc) · 4.13 KB
/
Copy pathQRCode.php
File metadata and controls
124 lines (103 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
declare(strict_types=1);
namespace GlobusStudio\QRCode;
use GlobusStudio\QRCode\Encoder\Encoder;
use GlobusStudio\QRCode\ErrorCorrection\ErrorCorrectionLevel;
use GlobusStudio\QRCode\Renderer\HtmlRenderer;
use GlobusStudio\QRCode\Renderer\PngRenderer;
use GlobusStudio\QRCode\Renderer\RendererInterface;
use GlobusStudio\QRCode\Renderer\StringRenderer;
use GlobusStudio\QRCode\Renderer\SvgRenderer;
final class QRCode
{
private string $data;
private int $errorCorrectionLevel;
private ?int $version;
private ?int $maskPattern;
/** @var bool[][]|null */
private ?array $matrix = null;
public function __construct(string $data, int $errorCorrectionLevel = ErrorCorrectionLevel::M, ?int $version = null, ?int $maskPattern = null)
{
if ($data === '') {
throw new \InvalidArgumentException('Data cannot be empty');
}
$this->data = $data;
$this->errorCorrectionLevel = ErrorCorrectionLevel::validate($errorCorrectionLevel);
$this->version = $version;
$this->maskPattern = $maskPattern;
}
/**
* @return bool[][]
*/
public function getMatrix(): array
{
if ($this->matrix === null) {
$this->matrix = Encoder::encode($this->data, $this->errorCorrectionLevel, $this->version, $this->maskPattern);
}
return $this->matrix;
}
public function render(RendererInterface $renderer): string
{
return $renderer->render($this->getMatrix());
}
/**
* @param array{level?: int, mask?: int, size?: int, margin?: int, foreground?: string, background?: string} $options
*/
public static function svg(string $data, array $options = []): string
{
$qr = new self($data, $options['level'] ?? ErrorCorrectionLevel::M, null, $options['mask'] ?? null);
return $qr->render(new SvgRenderer([
'size' => $options['size'] ?? 2,
'margin' => $options['margin'] ?? 2,
'foreground' => $options['foreground'] ?? '#000000',
'background' => $options['background'] ?? '#ffffff',
]));
}
/**
* @param array{level?: int, mask?: int, size?: int, margin?: int, foreground?: int, background?: int} $options
*/
public static function png(string $data, array $options = []): string
{
$qr = new self($data, $options['level'] ?? ErrorCorrectionLevel::M, null, $options['mask'] ?? null);
$renderer = new PngRenderer([
'size' => $options['size'] ?? 4,
'margin' => $options['margin'] ?? 4,
'foreground' => $options['foreground'] ?? 0x000000,
'background' => $options['background'] ?? 0xFFFFFF,
]);
return $renderer->renderDataUri($qr->getMatrix());
}
/**
* @param array{level?: int, mask?: int, size?: string, foreground?: string, background?: string} $options
*/
public static function html(string $data, array $options = []): string
{
$qr = new self($data, $options['level'] ?? ErrorCorrectionLevel::M, null, $options['mask'] ?? null);
return $qr->render(new HtmlRenderer([
'size' => $options['size'] ?? '2px',
'foreground' => $options['foreground'] ?? '#000000',
'background' => $options['background'] ?? '#ffffff',
]));
}
/**
* @param array{level?: int, mask?: int, dark?: string, light?: string, margin?: int} $options
*/
public static function string(string $data, array $options = []): string
{
$qr = new self($data, $options['level'] ?? ErrorCorrectionLevel::M, null, $options['mask'] ?? null);
return $qr->render(new StringRenderer([
'dark' => $options['dark'] ?? "\xe2\x96\x88\xe2\x96\x88",
'light' => $options['light'] ?? ' ',
'margin' => $options['margin'] ?? 1,
]));
}
/**
* @param array{level?: int, mask?: int} $options
* @return bool[][]
*/
public static function matrix(string $data, array $options = []): array
{
$qr = new self($data, $options['level'] ?? ErrorCorrectionLevel::M, null, $options['mask'] ?? null);
return $qr->getMatrix();
}
}