Skip to content

Commit f3d2fbb

Browse files
authored
Refactor barcode and orientation handling to use enums; update constr… (#39)
* Refactor barcode and orientation handling to use enums; update constructor and method signatures for improved type safety * Refactor fontMapper property to use AbstractMapper for improved type safety
1 parent b59e18d commit f3d2fbb

9 files changed

Lines changed: 271 additions & 264 deletions

File tree

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Simple usage (create a label with one cell and print it):
1717
require __DIR__ . '/vendor/autoload.php';
1818

1919
// Use millimeters for coordinates and dimensions
20-
$driver = new \Zpl\ZplBuilder('mm');
20+
use Zpl\Enums\Unit;
21+
$driver = new \Zpl\ZplBuilder(Unit::MM);
2122

2223
// Set encoding (optional)
2324
$driver->setEncoding(28);
@@ -37,7 +38,8 @@ Advanced usage (multiple elements, barcodes, QR codes, graphics, pages and raw c
3738
<?php
3839
require __DIR__ . '/vendor/autoload.php';
3940

40-
$driver = new \Zpl\ZplBuilder('mm');
41+
use Zpl\Enums\Unit;
42+
$driver = new \Zpl\ZplBuilder(Unit::MM);
4143
$driver->setFontMapper(new \Zpl\Fonts\Generic());
4244
$driver->setDpi(300); // change printer DPI when needed
4345

@@ -46,9 +48,10 @@ $driver->drawRect(5, 5, 50, 30);
4648
$driver->drawCircle(60, 5, 25);
4749

4850
// Text with explicit coordinates
49-
$driver->drawText(5, 40, 'Product: ABC-123', 'N');
51+
use Zpl\Enums\Orientation;
52+
$driver->drawText(5, 40, 'Product: ABC-123', Orientation::NORMAL);
5053

51-
// Code 128 barcode (x, y, height, data, print human readable?)
54+
// Code 128 barcode (x, y, height, data, print human-readable?)
5255
$driver->drawCode128(5, 50, 20, 'ABC123456789', true);
5356

5457
// QR Code (x, y, data, module size)
@@ -98,7 +101,7 @@ $builder->drawCustomBox(10, 10, 50, 20);
98101

99102
Notes and tips
100103

101-
- Units: the constructor accepts the unit string (for example: `mm` or `dots`). When using `mm`, coordinates are converted to printer dots using the configured DPI (default 203).
104+
- Units: the constructor accepts a Unit enum (for example: `Unit::MM` or `Unit::DOTS`). When using `Unit::MM`, coordinates are converted to printer dots using the configured DPI (default 203).
102105
- DPI: default resolution is 203 DPI. Use `setDpi()` to change it or `getDpi()` to read it.
103106
- Font mapping: use `setFontMapper()` to provide a mapper implementing `\Zpl\Fonts\AbstractMapper` (the included `\Zpl\Fonts\Generic` maps logical font ids to ZPL fonts).
104107
- Special characters: common control characters are converted to their ZPL-safe hex sequences automatically (see library mappings for details).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"license": "GPL-3.0-or-later",
88
"homepage": "https://github.com/andersonls/zpl",
99
"require": {
10-
"php": ">=7.4"
10+
"php": ">=8.2"
1111
},
1212
"require-dev": {
1313
"ext-gd": "*",

src/AbstractBuilder.php

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Zpl;
44

5-
use Zpl\Commands\Exception;
5+
use Zpl\Enums\Barcode;
6+
use Zpl\Enums\Orientation;
7+
use Zpl\Enums\Unit;
68

79
abstract class AbstractBuilder
810
{
9-
protected string $unit = 'dots';
10-
1111
/**
1212
* Current position of X coordinate in user unit
1313
*/
@@ -24,28 +24,12 @@ abstract class AbstractBuilder
2424

2525
protected float $width = 0;
2626

27-
/** @var string */
28-
public const UNIT_DOTS = 'dots';
29-
30-
/** @var string */
31-
public const UNIT_MM = 'mm';
32-
33-
/**
34-
* @throws BuilderException
35-
*/
36-
public function __construct(string $unit = 'dots')
37-
{
38-
if ($this->verifyUnit($unit) === true) {
39-
$this->unit = $unit;
40-
} else {
41-
throw new BuilderException('Unit ' . $unit . ' not recognized. Please use one of the constants of the class.');
42-
}
43-
}
27+
public function __construct(protected Unit $unit = Unit::DOTS) {}
4428

4529
/**
4630
* @param string $font The font number on the printer
4731
* @param float $size The font's size in pt
48-
* @param float|null $width The font's width in pt
32+
* @param ?float $width The font's width in pt
4933
*/
5034
abstract public function setFont(string $font, float $size, ?float $width = null): void;
5135

@@ -55,14 +39,16 @@ abstract public function setFont(string $font, float $size, ?float $width = null
5539
* @param float $x X position in user units
5640
* @param float $y Y position in user units
5741
* @param string $text Text to be inserted
58-
* @param string $orientation The text orientation. Available options:
59-
* N = normal
60-
* R = rotated 90 degrees
61-
* I = inverted 180 degrees
62-
* B = bottom-up 270 degrees, read from bottom up
42+
* @param Orientation $orientation The text orientation.
6343
* @param bool $invert Invert the color based on the background behind the text
6444
*/
65-
abstract public function drawText(float $x, float $y, string $text, string $orientation = 'N', bool $invert = false): void;
45+
abstract public function drawText(
46+
float $x,
47+
float $y,
48+
string $text,
49+
Orientation $orientation = Orientation::NORMAL,
50+
bool $invert = false
51+
): void;
6652

6753
/**
6854
* @param float $x1 X1 position in user units
@@ -139,36 +125,46 @@ abstract public function drawCircle(
139125
): void;
140126

141127
/**
142-
* @param string $type Barcode type, from Zpl\Enums\Barcode values
128+
* @param Barcode $type Barcode type
143129
* @param float $x X position in user units
144130
* @param float $y Y position in user units
145131
* @param float $height height of the barcode in user units
146132
* @param string $data Data to draw the barcode
147133
* @param bool $printData Whether to print the data or not
148134
* @param bool $labelAbove Text data position above or not
149-
* @param string $orientation The text orientation. Available options:
150-
* N = normal
151-
* R = rotated 90 degrees
152-
* I = inverted 180 degrees
153-
* B = bottom-up 270 degrees, read from bottom up
135+
* @param Orientation $orientation The text orientation
154136
* @param int $size Scale of the barcode (1-9)
155137
*/
156-
abstract public function drawBarcode(string $type, float $x, float $y, float $height, string $data, bool $printData = false, bool $labelAbove = false, string $orientation = 'N', int $size = 0): void;
138+
abstract public function drawBarcode(
139+
Barcode $type,
140+
float $x,
141+
float $y,
142+
float $height,
143+
string $data,
144+
bool $printData = false,
145+
bool $labelAbove = false,
146+
Orientation $orientation = Orientation::NORMAL,
147+
int $size = 0
148+
): void;
157149

158150
/**
159151
* @param float $x X position in user units
160152
* @param float $y Y position in user units
161153
* @param float $height height of the barcode in user units
162154
* @param string $data Data to draw the barcode
163155
* @param bool $printData Whether to print the data or not
164-
* @param string $orientation The text orientation. Available options:
165-
* N = normal
166-
* R = rotated 90 degrees
167-
* I = inverted 180 degrees
168-
* B = bottom-up 270 degrees, read from bottom up
156+
* @param Orientation $orientation The text orientation
169157
* @param int $size Scale of the barcode (1-9)
170158
*/
171-
abstract public function drawCode128(float $x, float $y, float $height, string $data, bool $printData = false, string $orientation = 'N', int $size = 0): void;
159+
abstract public function drawCode128(
160+
float $x,
161+
float $y,
162+
float $height,
163+
string $data,
164+
bool $printData = false,
165+
Orientation $orientation = Orientation::NORMAL,
166+
int $size = 0
167+
): void;
172168

173169
/**
174170
* @param float $x X position in user units
@@ -183,47 +179,29 @@ abstract public function drawQrCode(float $x, float $y, string $data, int $size
183179
* @param float $height height of the barcode in user units
184180
* @param string $data Data to draw the barcode
185181
* @param bool $printData Whether to print the data or not
186-
* @param string $orientation The text orientation. Available options:
187-
* N = normal
188-
* R = rotated 90 degrees
189-
* I = inverted 180 degrees
190-
* B = bottom-up 270 degrees, read from bottom up
182+
* @param Orientation $orientation The text orientation.
191183
* @param int $size Scale of the barcode (1-9)
192184
*/
193-
abstract public function drawCode39(float $x, float $y, float $height, string $data, bool $printData = false, string $orientation = 'N', int $size = 0): void;
185+
abstract public function drawCode39(
186+
float $x,
187+
float $y,
188+
float $height,
189+
string $data,
190+
bool $printData = false,
191+
Orientation $orientation = Orientation::NORMAL,
192+
int $size = 0
193+
): void;
194194

195195
/**
196196
* @param float $x X position in user units
197197
* @param float $y Y position in user units
198198
* @param string $image Filename of the image to draw
199199
* @param int $width Width of the image to be added
200-
*
201-
* @throws Exception
202200
*/
203201
abstract public function drawGraphic(float $x, float $y, string $image, int $width = 0): void;
204202

205203
abstract public function newPage(): void;
206204

207-
/**
208-
* Verify if the $unit is valid.
209-
*
210-
*
211-
* @return bool true if the unit is valid, false otherwise.
212-
*
213-
* @throws \Exception
214-
*/
215-
protected function verifyUnit(string $unit): bool
216-
{
217-
$r = new \ReflectionClass('\Zpl\AbstractBuilder');
218-
$constants = $r->getConstants();
219-
$key = array_search($unit, $constants) ?: '';
220-
if (preg_match('/UNIT/', $key)) {
221-
return true;
222-
} else {
223-
return false;
224-
}
225-
}
226-
227205
public function setXY(float $x, float $y): void
228206
{
229207
$this->x = $x;

src/Enums/Barcode.php

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,35 @@
22

33
namespace Zpl\Enums;
44

5-
class Barcode
5+
enum Barcode: string
66
{
7-
const AZTEK = 'B0';
8-
const CODE11 = 'B1';
9-
const INTERLEAVED2 = 'B2';
10-
const CODE39 = 'B3';
11-
const CODE49 = 'B4';
12-
const PLANET = 'B5';
13-
const PDF417 = 'B7';
14-
const EAN8 = 'B8';
15-
const UPCE = 'B9';
16-
const CODE93 = 'BA';
17-
const CODABLOCK = 'BB';
18-
const CODE128 = 'BC';
19-
const UPS = 'BD';
20-
const EAN13 = 'BE';
21-
const MICROPDF417 = 'BF';
22-
const INDUSTRIAL2 = 'BI';
23-
const STANDARD2 = 'BJ';
24-
const ANSI = 'BK';
25-
const LOGMARS = 'BL';
26-
const MSI = 'BM';
27-
const PLESSEY = 'BP';
28-
const QR = 'BQ';
29-
const GS1 = 'BR';
30-
const UPC_EAN = 'BS';
31-
const TLC39 = 'BT';
32-
const UPCA = 'BU';
33-
const DATAMATRIX = 'BX';
34-
const DEFAULT = 'BY';
35-
const POSTAL = 'BZ';
36-
37-
/** @return array<string> **/
38-
public static function values(): array
39-
{
40-
return (new \ReflectionClass(static::class))->getConstants();
41-
}
42-
43-
public static function isValid(string $value): bool
44-
{
45-
return in_array($value, static::values(), true);
46-
}
47-
48-
/** @throws \InvalidArgumentException */
49-
public static function validate(string $value): void
50-
{
51-
$values = self::values();
52-
if (in_array($value, $values, true)) {
53-
return;
54-
}
55-
56-
throw new \InvalidArgumentException('Valid values for barcode type are: ' . implode(',', $values));
57-
}
7+
case AZTEK = 'B0';
8+
case CODE11 = 'B1';
9+
case INTERLEAVED2 = 'B2';
10+
case CODE39 = 'B3';
11+
case CODE49 = 'B4';
12+
case PLANET = 'B5';
13+
case PDF417 = 'B7';
14+
case EAN8 = 'B8';
15+
case UPCE = 'B9';
16+
case CODE93 = 'BA';
17+
case CODABLOCK = 'BB';
18+
case CODE128 = 'BC';
19+
case UPS = 'BD';
20+
case EAN13 = 'BE';
21+
case MICROPDF417 = 'BF';
22+
case INDUSTRIAL2 = 'BI';
23+
case STANDARD2 = 'BJ';
24+
case ANSI = 'BK';
25+
case LOGMARS = 'BL';
26+
case MSI = 'BM';
27+
case PLESSEY = 'BP';
28+
case QR = 'BQ';
29+
case GS1 = 'BR';
30+
case UPC_EAN = 'BS';
31+
case TLC39 = 'BT';
32+
case UPCA = 'BU';
33+
case DATAMATRIX = 'BX';
34+
case DEFAULT = 'BY';
35+
case POSTAL = 'BZ';
5836
}

src/Enums/Orientation.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,10 @@
22

33
namespace Zpl\Enums;
44

5-
class Orientation
5+
enum Orientation: string
66
{
7-
const NORMAL = 'N'; // normal as default
8-
const ROTATED = 'R'; // rotated 90 degrees
9-
const INVERTED = 'I'; // inverted 180 degrees
10-
const BOTTOM_UP = 'B'; // bottom-up 270 degrees, read from bottom up
11-
12-
/** @return array<string> **/
13-
public static function values(): array
14-
{
15-
return ['N', 'R', 'I', 'B'];
16-
}
17-
18-
public static function isValid(string $value): bool
19-
{
20-
return in_array($value, static::values(), true);
21-
}
22-
23-
/** @throws \InvalidArgumentException */
24-
public static function validate(string $value): void
25-
{
26-
$values = self::values();
27-
if (in_array($value, $values, true)) {
28-
return;
29-
}
30-
31-
throw new \InvalidArgumentException('Valid values for orientation are: ' . implode(',', $values));
32-
}
7+
case NORMAL = 'N'; // normal as default
8+
case ROTATED = 'R'; // rotated 90 degrees
9+
case INVERTED = 'I'; // inverted 180 degrees
10+
case BOTTOM_UP = 'B'; // bottom-up 270 degrees, read from bottom up
3311
}

src/Enums/Unit.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Zpl\Enums;
4+
5+
enum Unit: string
6+
{
7+
case DOTS = 'dots';
8+
case MM = 'mm';
9+
}

0 commit comments

Comments
 (0)