-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStringRenderer.php
More file actions
54 lines (42 loc) · 1.4 KB
/
Copy pathStringRenderer.php
File metadata and controls
54 lines (42 loc) · 1.4 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
<?php
declare(strict_types=1);
namespace GlobusStudio\QRCode\Renderer;
final class StringRenderer implements RendererInterface
{
private string $darkChar;
private string $lightChar;
private int $margin;
/**
* @param array{dark?: string, light?: string, margin?: int} $options
*/
public function __construct(array $options = [])
{
$this->darkChar = $options['dark'] ?? "\xe2\x96\x88\xe2\x96\x88";
$this->lightChar = $options['light'] ?? ' ';
$this->margin = max(0, (int) ($options['margin'] ?? 1));
}
/**
* @param bool[][] $matrix
*/
public function render(array $matrix): string
{
$moduleCount = count($matrix);
$output = '';
$marginLine = str_repeat($this->lightChar, $moduleCount + $this->margin * 2);
for ($i = 0; $i < $this->margin; $i++) {
$output .= $marginLine . "\n";
}
for ($r = 0; $r < $moduleCount; $r++) {
$line = str_repeat($this->lightChar, $this->margin);
for ($c = 0; $c < $moduleCount; $c++) {
$line .= $matrix[$r][$c] ? $this->darkChar : $this->lightChar;
}
$line .= str_repeat($this->lightChar, $this->margin);
$output .= $line . "\n";
}
for ($i = 0; $i < $this->margin; $i++) {
$output .= $marginLine . "\n";
}
return $output;
}
}