|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/qrcode-library project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Da\QrCode\Writer; |
|
13
|
|
|
|
|
14
|
|
|
use BaconQrCode\Renderer\Color\Rgb; |
|
15
|
|
|
use BaconQrCode\Renderer\RendererInterface; |
|
16
|
|
|
use Da\QrCode\Contracts\QrCodeInterface; |
|
17
|
|
|
use Da\QrCode\Contracts\WriterInterface; |
|
18
|
|
|
use ReflectionClass; |
|
19
|
|
|
use ReflectionException; |
|
20
|
|
|
|
|
21
|
|
|
abstract class AbstractWriter implements WriterInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var RendererInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $renderer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* AbstractWriter constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param RendererInterface $renderer |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function __construct(RendererInterface $renderer) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->renderer = $renderer; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function writeDataUri(QrCodeInterface $qrCode): string |
|
42
|
|
|
{ |
|
43
|
|
|
return 'data:' . $this->getContentType() . ';base64,' . base64_encode($this->writeString($qrCode)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function writeFile(QrCodeInterface $qrCode, $path) |
|
50
|
|
|
{ |
|
51
|
|
|
return file_put_contents($path, $this->writeString($qrCode)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
* @throws ReflectionException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getName(): string |
|
59
|
|
|
{ |
|
60
|
|
|
return strtolower(str_replace('Writer', '', (new ReflectionClass($this))->getShortName())); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $color |
|
65
|
|
|
* |
|
66
|
|
|
* @return Rgb |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function convertColor(array $color): Rgb |
|
69
|
|
|
{ |
|
70
|
|
|
return new Rgb($color['r'], $color['g'], $color['b']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $errorCorrectionLevel |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function convertErrorCorrectionLevel($errorCorrectionLevel): string |
|
79
|
|
|
{ |
|
80
|
|
|
$name = strtoupper($errorCorrectionLevel[0]); |
|
81
|
|
|
$errorCorrectionLevel = constant('BaconQrCode\Common\ErrorCorrectionLevel::' . $name); |
|
82
|
|
|
|
|
83
|
|
|
return $errorCorrectionLevel; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|