Skip to content

Commit bcca9bc

Browse files
author
Corey McCormick
committed
Work on rewrite of Generator
1 parent 596f5c9 commit bcca9bc

1 file changed

Lines changed: 88 additions & 215 deletions

File tree

src/SimpleSoftwareIO/QrCode/Generator.php

Lines changed: 88 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -2,289 +2,162 @@
22

33
namespace SimpleSoftwareIO\QrCode;
44

5-
use BaconQrCode;
65
use BaconQrCode\Common\ErrorCorrectionLevel;
76
use BaconQrCode\Encoder\Encoder;
8-
use BaconQrCode\Renderer\Color\Rgb;
9-
use BaconQrCode\Renderer\Image\Eps;
10-
use BaconQrCode\Renderer\Image\Png;
11-
use BaconQrCode\Renderer\Image\RendererInterface;
12-
use BaconQrCode\Renderer\Image\Svg;
7+
use BaconQrCode\Renderer\Eye\EyeInterface;
8+
use BaconQrCode\Renderer\Eye\ModuleEye;
9+
use BaconQrCode\Renderer\Eye\SimpleCircleEye;
10+
use BaconQrCode\Renderer\Eye\SquareEye;
11+
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
12+
use BaconQrCode\Renderer\ImageRenderer;
13+
use BaconQrCode\Renderer\Module\DotsModule;
14+
use BaconQrCode\Renderer\Module\ModuleInterface;
15+
use BaconQrCode\Renderer\Module\RoundnessModule;
16+
use BaconQrCode\Renderer\Module\SquareModule;
17+
use BaconQrCode\Renderer\RendererInterface;
18+
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
1319
use BaconQrCode\Writer;
20+
use InvalidArgumentException;
1421

1522
class Generator
1623
{
17-
/**
18-
* Holds the BaconQrCode Writer Object.
19-
*
20-
* @var \BaconQrCode\Writer
21-
*/
22-
protected $writer;
23-
24-
/**
25-
* Holds the QrCode error correction levels. This is stored by using the BaconQrCode ErrorCorrectionLevel class constants.
26-
*
27-
* @var \BaconQrCode\Common\ErrorCorrectionLevel
28-
*/
29-
protected $errorCorrection = ErrorCorrectionLevel::L;
30-
31-
/**
32-
* Holds the Encoder mode to encode a QrCode.
33-
*
34-
* @var string
35-
*/
24+
protected $pixels = 100;
25+
26+
protected $margin = 0;
27+
28+
protected $errorCorrection = null;
29+
3630
protected $encoding = Encoder::DEFAULT_BYTE_MODE_ECODING;
3731

38-
/**
39-
* Holds an image string that will be merged with the QrCode.
40-
*
41-
* @var null|string
42-
*/
43-
protected $imageMerge = null;
44-
45-
/**
46-
* The percentage that a merged image should take over the source image.
47-
*
48-
* @var float
49-
*/
50-
protected $imagePercentage = .2;
51-
52-
/**
53-
* BaconQrCodeGenerator constructor.
54-
*
55-
* @param Writer|null $writer
56-
* @param RendererInterface|null $format
57-
*/
58-
public function __construct(Writer $writer = null, RendererInterface $format = null)
59-
{
60-
$format = $format ?: new Svg();
61-
$this->writer = $writer ?: new Writer($format);
62-
}
32+
protected $style = 'square';
6333

64-
/**
65-
* Generates a QrCode.
66-
*
67-
* @param string $text The text to be converted into a QrCode
68-
* @param null|string $filename The filename and path to save the QrCode file
69-
*
70-
* @return string|void Returns a QrCode string depending on the format, or saves to a file.
71-
*/
72-
public function generate($text, $filename = null)
73-
{
74-
$qrCode = $this->writer->writeString($text, $this->encoding, $this->errorCorrection);
34+
protected $styleSize = null;
7535

76-
if ($this->imageMerge !== null) {
77-
$merger = new ImageMerge(new Image($qrCode), new Image($this->imageMerge));
78-
$qrCode = $merger->merge($this->imagePercentage);
79-
}
36+
protected $eyeStyle = null;
8037

81-
if ($filename === null) {
82-
return $qrCode;
83-
}
38+
public function generate(string $text, string $filename = null)
39+
{
40+
$render = $this->getRenderer();
8441

85-
return file_put_contents($filename, $qrCode);
42+
return $this->getWriter($render)->writeString($text, $this->encoding, $this->errorCorrection);
8643
}
8744

88-
/**
89-
* Merges an image with the center of the QrCode.
90-
*
91-
* @param $filepath string The filepath to an image
92-
* @param $percentage float The amount that the merged image should be placed over the qrcode.
93-
* @param $absolute boolean Whether to use an absolute filepath or not.
94-
*
95-
* @return $this
96-
*/
97-
public function merge($filepath, $percentage = .2, $absolute = false)
45+
public function size(int $pixels): self
9846
{
99-
if (function_exists('base_path') && !$absolute) {
100-
$filepath = base_path().$filepath;
101-
}
102-
103-
$this->imageMerge = file_get_contents($filepath);
104-
$this->imagePercentage = $percentage;
47+
$this->pixels = $pixels;
10548

10649
return $this;
10750
}
10851

109-
/**
110-
* Merges an image string with the center of the QrCode, does not check for correct format.
111-
*
112-
* @param $content string The string contents of an image.
113-
* @param $percentage float The amount that the merged image should be placed over the qrcode.
114-
*
115-
* @return $this
116-
*/
117-
public function mergeString($content, $percentage = .2)
52+
public function format(): self
11853
{
119-
$this->imageMerge = $content;
120-
$this->imagePercentage = $percentage;
121-
122-
return $this;
54+
//png, eps, svg, jpg
55+
//
12356
}
12457

125-
/**
126-
* Switches the format of the outputted QrCode or defaults to SVG.
127-
*
128-
* @param string $format The desired format.
129-
*
130-
* @throws \InvalidArgumentException
131-
*
132-
* @return $this
133-
*/
134-
public function format($format)
58+
public function color($red, $green, $blue, $alpha): self
13559
{
136-
switch ($format) {
137-
case 'png':
138-
$this->writer->setRenderer(new Png());
139-
break;
140-
case 'eps':
141-
$this->writer->setRenderer(new Eps());
142-
break;
143-
case 'svg':
144-
$this->writer->setRenderer(new Svg());
145-
break;
146-
default:
147-
throw new \InvalidArgumentException('Invalid format provided.');
148-
}
60+
}
14961

150-
return $this;
62+
public function backgroundColor($red, $green, $blue, $alpha): self
63+
{
15164
}
15265

153-
/**
154-
* Changes the size of the QrCode.
155-
*
156-
* @param int $pixels The size of the QrCode in pixels
157-
*
158-
* @return $this
159-
*/
160-
public function size($pixels)
66+
public function eyeColor(array $eye1, array $eye2, array $eye3): self
16167
{
162-
$this->writer->getRenderer()->setHeight($pixels);
163-
$this->writer->getRenderer()->setWidth($pixels);
68+
}
16469

165-
return $this;
70+
public function gradient(): self
71+
{
16672
}
16773

168-
/**
169-
* Changes the foreground color of a QrCode.
170-
*
171-
* @param int $red
172-
* @param int $green
173-
* @param int $blue
174-
*
175-
* @return $this
176-
*/
177-
public function color($red, $green, $blue)
74+
public function eye(string $style): self
17875
{
179-
$this->writer->getRenderer()->setForegroundColor(new Rgb($red, $green, $blue));
76+
if (! in_array($style, ['square', 'circle'])) {
77+
throw new InvalidArgumentException("\$style must be square or circle. {$style} is not a valid eye style.");
78+
}
79+
80+
$this->eyeStyle = $style;
18081

18182
return $this;
18283
}
18384

184-
/**
185-
* Changes the background color of a QrCode.
186-
*
187-
* @param int $red
188-
* @param int $green
189-
* @param int $blue
190-
*
191-
* @return $this
192-
*/
193-
public function backgroundColor($red, $green, $blue)
85+
public function style(string $style, float $size = 0.5): self
19486
{
195-
$this->writer->getRenderer()->setBackgroundColor(new Rgb($red, $green, $blue));
87+
if (! in_array($style, ['square', 'dot', 'round'])) {
88+
throw new InvalidArgumentException("\$style must be square, dot, or round. {$style} is not a valid QrCode style.");
89+
}
90+
91+
if ($size < 0 || $size >= 1) {
92+
throw new InvalidArgumentException("\$size must be between 0 and 1. {$size} is not valid.");
93+
}
94+
95+
$this->style = $style;
96+
$this->styleSize = $size;
19697

19798
return $this;
19899
}
199100

200-
/**
201-
* Changes the error correction level of a QrCode.
202-
*
203-
* @param string $level Desired error correction level. L = 7% M = 15% Q = 25% H = 30%
204-
*
205-
* @return $this
206-
*/
207-
public function errorCorrection($level)
101+
public function encoding(string $encoding): self
208102
{
209-
$this->errorCorrection = constant("BaconQrCode\Common\ErrorCorrectionLevel::$level");
103+
$this->encoding = strtoupper($encoding);
210104

211105
return $this;
212106
}
213107

214-
/**
215-
* Creates a margin around the QrCode.
216-
*
217-
* @param int $margin The desired margin in pixels around the QrCode
218-
*
219-
* @return $this
220-
*/
221-
public function margin($margin)
108+
public function errorCorrection(string $errorCorrection): self
222109
{
223-
$this->writer->getRenderer()->setMargin($margin);
110+
$errorCorrection = strtoupper($errorCorrection);
111+
$this->errorCorrection = ErrorCorrectionLevel::$errorCorrection();
224112

225113
return $this;
226114
}
227115

228-
/**
229-
* Sets the Encoding mode.
230-
*
231-
* @param string $encoding
232-
*
233-
* @return $this
234-
*/
235-
public function encoding($encoding)
116+
public function margin(int $margin): self
236117
{
237-
$this->encoding = $encoding;
118+
$this->margin = $margin;
238119

239120
return $this;
240121
}
241122

242-
/**
243-
* Creates a new datatype object and then generates a QrCode.
244-
*
245-
* @param $method
246-
* @param $arguments
247-
*/
248-
public function __call($method, $arguments)
123+
protected function getWriter(ImageRenderer $renderer): Writer
249124
{
250-
$dataType = $this->createClass($method);
125+
return (new Writer($renderer));
126+
}
251127

252-
$dataType->create($arguments);
128+
protected function getRenderer(): ImageRenderer
129+
{
130+
$renderer = new ImageRenderer(
131+
new RendererStyle($this->pixels, $this->margin, $this->getModule(), $this->getEye()),
132+
new SvgImageBackEnd
133+
);
253134

254-
return $this->generate(strval($dataType));
135+
return $renderer;
255136
}
256137

257-
/**
258-
* Creates a new DataType class dynamically.
259-
*
260-
* @param string $method
261-
*
262-
* @return SimpleSoftwareIO\QrCode\DataTypes\DataTypeInterface
263-
*/
264-
private function createClass($method)
138+
protected function getModule(): ModuleInterface
265139
{
266-
$class = $this->formatClass($method);
140+
if ($this->style === 'dot') {
141+
return (new DotsModule($this->styleSize));
142+
}
267143

268-
if (!class_exists($class)) {
269-
throw new \BadMethodCallException();
144+
if ($this->style === 'round') {
145+
return (new RoundnessModule($this->styleSize));
270146
}
271147

272-
return new $class();
148+
return SquareModule::instance();
273149
}
274150

275-
/**
276-
* Formats the method name correctly.
277-
*
278-
* @param $method
279-
*
280-
* @return string
281-
*/
282-
private function formatClass($method)
151+
protected function getEye(): EyeInterface
283152
{
284-
$method = ucfirst($method);
153+
if ($this->eyeStyle === 'square') {
154+
return SquareEye::instance();
155+
}
285156

286-
$class = "SimpleSoftwareIO\QrCode\DataTypes\\".$method;
157+
if ($this->eyeStyle === 'circle') {
158+
return SimpleCircleEye::instance();
159+
}
287160

288-
return $class;
161+
return (new ModuleEye($this->getModule()));
289162
}
290163
}

0 commit comments

Comments
 (0)