1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sprain\SwissQrBill\QrCode; |
4
|
|
|
|
5
|
|
|
use Endroid\QrCode\QrCode as BaseQrCode; |
6
|
|
|
use Endroid\QrCode\QrCodeInterface; |
7
|
|
|
use Sprain\SwissQrBill\QrCode\Exception\UnsupportedFileExtensionException; |
8
|
|
|
|
9
|
|
|
class QrCode extends BaseQrCode implements QrCodeInterface |
10
|
|
|
{ |
11
|
|
|
const FILE_FORMAT_PNG = 'png'; |
12
|
|
|
const FILE_FORMAT_SVG = 'svg'; |
13
|
|
|
|
14
|
|
|
// A file extension is supported if the underlying library supports it, |
15
|
|
|
// including the possibility to add a logo in the center of the qr code. |
16
|
|
|
const SUPPORTED_FILE_FORMATS = [ |
17
|
|
|
self::FILE_FORMAT_PNG, |
18
|
|
|
self::FILE_FORMAT_SVG |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public function writeFile(string $path): void |
22
|
|
|
{ |
23
|
|
|
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
24
|
|
|
$this->setWriterByExtension($extension); |
25
|
|
|
|
26
|
|
|
$this->addLogoInMatchingFileFormat(); |
27
|
|
|
|
28
|
|
|
parent::writeFile($path); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function writeDataUri(): string |
32
|
|
|
{ |
33
|
|
|
$this->addLogoInMatchingFileFormat(); |
34
|
|
|
|
35
|
|
|
return parent::writeDataUri(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setWriterByExtension(string $extension): void |
39
|
|
|
{ |
40
|
|
|
if (!in_array($extension, self::SUPPORTED_FILE_FORMATS)) { |
41
|
|
|
throw new UnsupportedFileExtensionException(sprintf( |
42
|
|
|
'The qr code file cannot be created. Only these file extensions are supported: %s. You provided: %s.', |
43
|
|
|
implode(', ', self::SUPPORTED_FILE_FORMATS), |
44
|
|
|
$extension |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
parent::setWriterByExtension($extension); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function addLogoInMatchingFileFormat(): void |
52
|
|
|
{ |
53
|
|
|
if ($this->getWriter()->supportsExtension(self::FILE_FORMAT_SVG)) { |
54
|
|
|
$this->setWriterOptions([ |
55
|
|
|
'force_xlink_href' => true |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|