Skip to content

Commit 5fa5348

Browse files
committed
Move GdWriter to GdTrait
1 parent 1cf9904 commit 5fa5348

File tree

5 files changed

+13
-57
lines changed

5 files changed

+13
-57
lines changed

src/Writer/AbstractGdWriter.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,10 @@
44

55
namespace Endroid\QrCode\Writer;
66

7-
use Endroid\QrCode\Label\LabelInterface;
8-
use Endroid\QrCode\Logo\LogoInterface;
9-
use Endroid\QrCode\QrCodeInterface;
10-
use Endroid\QrCode\Writer\Result\GdResult;
11-
use Endroid\QrCode\Writer\Result\ResultInterface;
12-
137
/**
14-
* @deprecated since 6.0, use GdWriter instead. This class will be removed in 7.0.
8+
* @deprecated since 6.0, use GdTrait instead. This class will be removed in 7.0.
159
*/
1610
abstract readonly class AbstractGdWriter implements WriterInterface, ValidatingWriterInterface
1711
{
18-
private GdWriter $gdWriter;
19-
20-
public function __construct()
21-
{
22-
$this->gdWriter = new GdWriter();
23-
}
24-
25-
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): GdResult
26-
{
27-
return $this->gdWriter->write($qrCode, $logo, $label, $options);
28-
}
29-
30-
public function validateResult(ResultInterface $result, string $expectedData): void
31-
{
32-
$this->gdWriter->validateResult($result, $expectedData);
33-
}
12+
use GdTrait;
3413
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Endroid\QrCode\Writer\Result\ResultInterface;
1919
use Zxing\QrReader;
2020

21-
final readonly class GdWriter implements WriterInterface, ValidatingWriterInterface
21+
trait GdTrait
2222
{
2323
public function getMatrix(QrCodeInterface $qrCode): MatrixInterface
2424
{
@@ -27,7 +27,8 @@ public function getMatrix(QrCodeInterface $qrCode): MatrixInterface
2727
return $matrixFactory->create($qrCode);
2828
}
2929

30-
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): GdResult
30+
/** @param array<string, mixed> $options */
31+
public function writeGd(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): GdResult
3132
{
3233
if (!extension_loaded('gd')) {
3334
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');

src/Writer/GifWriter.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@
1212

1313
final readonly class GifWriter implements WriterInterface, ValidatingWriterInterface
1414
{
15-
public function __construct(
16-
private GdWriter $gdWriter = new GdWriter(),
17-
) {
18-
}
15+
use GdTrait;
1916

2017
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): ResultInterface
2118
{
22-
$gdResult = $this->gdWriter->write($qrCode, $logo, $label, $options);
19+
$gdResult = $this->writeGd($qrCode, $logo, $label, $options);
2320

2421
return new GifResult($gdResult->getMatrix(), $gdResult->getImage());
2522
}
26-
27-
public function validateResult(ResultInterface $result, string $expectedData): void
28-
{
29-
$this->gdWriter->validateResult($result, $expectedData);
30-
}
3123
}

src/Writer/PngWriter.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212

1313
final readonly class PngWriter implements WriterInterface, ValidatingWriterInterface
1414
{
15+
use GdTrait;
16+
1517
public const WRITER_OPTION_COMPRESSION_LEVEL = 'compression_level';
1618
public const WRITER_OPTION_NUMBER_OF_COLORS = 'number_of_colors';
1719

18-
public function __construct(
19-
private GdWriter $gdWriter = new GdWriter(),
20-
) {
21-
}
22-
2320
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): ResultInterface
2421
{
2522
if (!isset($options[self::WRITER_OPTION_COMPRESSION_LEVEL])) {
@@ -34,7 +31,7 @@ public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?Lab
3431
};
3532
}
3633

37-
$gdResult = $this->gdWriter->write($qrCode, $logo, $label, $options);
34+
$gdResult = $this->writeGd($qrCode, $logo, $label, $options);
3835

3936
return new PngResult(
4037
$gdResult->getMatrix(),
@@ -43,9 +40,4 @@ public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?Lab
4340
$options[self::WRITER_OPTION_NUMBER_OF_COLORS]
4441
);
4542
}
46-
47-
public function validateResult(ResultInterface $result, string $expectedData): void
48-
{
49-
$this->gdWriter->validateResult($result, $expectedData);
50-
}
5143
}

src/Writer/WebPWriter.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,18 @@
1212

1313
final readonly class WebPWriter implements WriterInterface, ValidatingWriterInterface
1414
{
15-
public const WRITER_OPTION_QUALITY = 'quality';
15+
use GdTrait;
1616

17-
public function __construct(
18-
private GdWriter $gdWriter = new GdWriter(),
19-
) {
20-
}
17+
public const WRITER_OPTION_QUALITY = 'quality';
2118

2219
public function write(QrCodeInterface $qrCode, ?LogoInterface $logo = null, ?LabelInterface $label = null, array $options = []): ResultInterface
2320
{
2421
if (!isset($options[self::WRITER_OPTION_QUALITY])) {
2522
$options[self::WRITER_OPTION_QUALITY] = -1;
2623
}
2724

28-
$gdResult = $this->gdWriter->write($qrCode, $logo, $label, $options);
25+
$gdResult = $this->writeGd($qrCode, $logo, $label, $options);
2926

3027
return new WebPResult($gdResult->getMatrix(), $gdResult->getImage(), $options[self::WRITER_OPTION_QUALITY]);
3128
}
32-
33-
public function validateResult(ResultInterface $result, string $expectedData): void
34-
{
35-
$this->gdWriter->validateResult($result, $expectedData);
36-
}
3729
}

0 commit comments

Comments
 (0)