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
<?php
/**
* BaconQrCode
*
* @link http://github.com/Bacon/BaconQrCode For the canonical source repository
* @copyright 2013 Ben 'DASPRiD' Scholzen
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace BaconQrCode;
use BaconQrCode\Common\ErrorCorrectionLevel;
use BaconQrCode\Encoder\Encoder;
use BaconQrCode\Exception;
use BaconQrCode\Renderer\RendererInterface;
/**
* QR code writer.
*/
class Writer
{
/**
* Renderer instance.
*
* @var RendererInterface
*/
protected $renderer;
/**
* Creates a new writer with a specific renderer.
*
* @param RendererInterface $renderer
*/
public function __construct(RendererInterface $renderer)
{
$this->renderer = $renderer;
}
/**
* Sets the renderer used to create a byte stream.
*
* @param RendererInterface $renderer
* @return Writer
*/
public function setRenderer(RendererInterface $renderer)
{
$this->renderer = $renderer;
return $this;
}
/**
* Gets the renderer used to create a byte stream.
*
* @return RendererInterface
*/
public function getRenderer()
{
return $this->renderer;
}
/**
* Writes QR code and returns it as string.
*
* Content is a string which *should* be encoded in UTF-8, in case there are
* non ASCII-characters present.
*
* @param string $content
* @param string $encoding
* @param integer $ecLevel
* @return string
* @throws Exception\InvalidArgumentException
*/
public function writeString(
$content,
$encoding = Encoder::DEFAULT_BYTE_MODE_ECODING,
$ecLevel = ErrorCorrectionLevel::L
) {
if (strlen($content) === 0) {
throw new Exception\InvalidArgumentException('Found empty contents');
}
$qrCode = Encoder::encode($content, new ErrorCorrectionLevel($ecLevel), $encoding);
return $this->getRenderer()->render($qrCode);
}
/**
* Writes QR code to a file.
*
* @see Writer::writeString()
* @param string $content
* @param string $filename
* @param string $encoding
* @param integer $ecLevel
* @return void
*/
public function writeFile(
$content,
$filename,
$encoding = Encoder::DEFAULT_BYTE_MODE_ECODING,
$ecLevel = ErrorCorrectionLevel::L
) {
file_put_contents($filename, $this->writeString($content, $encoding, $ecLevel));
}
}