Skip to content

Commit dd18300

Browse files
committed
Introduce Align enum for text alignment in drawCell method
1 parent dc885e6 commit dd18300

6 files changed

Lines changed: 28 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ require __DIR__ . '/vendor/autoload.php';
1818

1919
// Use millimeters for coordinates and dimensions
2020
use Zpl\Enums\Unit;
21+
use Zpl\Enums\Align;
2122
$driver = new \Zpl\ZplBuilder(Unit::MM);
2223

2324
// Set encoding (optional)
2425
$driver->setEncoding(28);
2526

2627
$driver->setFont('0', 16);
2728
$driver->setXY(0, 0);
28-
$driver->drawCell(100, 10, 'Hello World', true, true, 'C');
29+
$driver->drawCell(100, 10, 'Hello World', true, true, Align::CENTER);
2930

3031
$zpl = $driver->toZpl();
3132

src/AbstractBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Zpl;
44

5+
use Zpl\Enums\Align;
56
use Zpl\Enums\Barcode;
67
use Zpl\Enums\Orientation;
78
use Zpl\Enums\Unit;
@@ -96,15 +97,15 @@ abstract public function drawRect(
9697
* @param string $text Text to be drawn
9798
* @param bool $border Whether the cell have a border or not
9899
* @param bool $ln Whether to advance the X, Y coordinates to the next line
99-
* @param string $align Alignment of the text inside the cell (L = left, C = center, R = right, J = justified)
100+
* @param ?Align $align Alignment of the text inside the cell
100101
*/
101102
abstract public function drawCell(
102103
float $width,
103104
float $height,
104105
string $text,
105106
bool $border = false,
106107
bool $ln = false,
107-
string $align = ''
108+
?Align $align = null
108109
): void;
109110

110111
/**

src/Enums/Align.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Zpl\Enums;
4+
5+
enum Align: string
6+
{
7+
case LEFT = 'L';
8+
case CENTER = 'C';
9+
case RIGHT = 'R';
10+
case JUSTIFIED = 'J';
11+
}

src/PdfBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Zpl;
44

5+
use Zpl\Enums\Align;
56
use Zpl\Enums\Barcode;
67
use Zpl\Enums\Orientation;
78
use Zpl\Enums\Unit;
@@ -102,9 +103,9 @@ public function drawCell(
102103
string $text,
103104
bool $border = false,
104105
bool $ln = false,
105-
string $align = ''
106+
?Align $align = null
106107
): void {
107-
$this->pdfDriver->Cell($width, $height, $this->_($text), $border, $ln, $align);
108+
$this->pdfDriver->Cell($width, $height, $this->_($text), $border, $ln, $align->value ?? '');
108109
}
109110

110111
/**

src/ZplBuilder.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Zpl;
44

55
use Zpl\Commands\GraphicField;
6+
use Zpl\Enums\Align;
67
use Zpl\Enums\Barcode;
78
use Zpl\Enums\Orientation;
89
use Zpl\Enums\Unit;
@@ -268,7 +269,7 @@ public function drawCell(
268269
string $text,
269270
bool $border = false,
270271
bool $ln = false,
271-
string $align = ''
272+
?Align $align = null
272273
): void {
273274
$x = $this->getX();
274275
$y = $this->getY();
@@ -279,8 +280,8 @@ public function drawCell(
279280
$offsetX = 10;
280281
$offsetY = $this->toDots($height) / 4;
281282
$this->commands[] = '^FO' . ($this->toDots($x) + $offsetX) . ',' . ($this->toDots($y) + $offsetY);
282-
if ($align !== '') {
283-
$this->commands[] = '^FB' . ($this->toDots($width) - $offsetX) . ',' . ($this->toDots($height) - $offsetY) . ',0,' . $align;
283+
if ($align) {
284+
$this->commands[] = '^FB' . ($this->toDots($width) - $offsetX) . ',' . ($this->toDots($height) - $offsetY) . ',0,' . $align->value;
284285
}
285286
$this->commands[] = '^FH^FD' . strtr($text, self::CONTROL_CHAR_HEX_MAPPINGS) . '^FS';
286287
}

tests/ZplTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Zpl\Tests;
44

55
use PHPUnit\Framework\Attributes\Test;
6+
use Zpl\Enums\Align;
67
use Zpl\Enums\Barcode;
78
use Zpl\Enums\Orientation;
89

@@ -104,15 +105,15 @@ public function drawCircle(): void
104105
#[Test]
105106
public function drawCell(): void
106107
{
107-
$this->driver->drawCell(100, 10, 'Hello World', true, true, 'C');
108+
$this->driver->drawCell(100, 10, 'Hello World', true, true, Align::CENTER);
108109

109110
$this->assertEquals("^XA\n^FO0,0^GB100,10,3,B,0^FS\n^FO10,2.5\n^FB90,7.5,0,C\n^FH^FDHello World^FS\n^XZ\n", $this->getZpl());
110111
}
111112

112113
#[Test]
113-
public function drawCellWithEspecialCharaters(): void
114+
public function drawCellWithEspecialCharacters(): void
114115
{
115-
$this->driver->drawCell(100, 10, 'E ^~_ C', true, true, 'C');
116+
$this->driver->drawCell(100, 10, 'E ^~_ C', true, true, Align::CENTER);
116117

117118
$this->assertEquals("^XA\n^FO0,0^GB100,10,3,B,0^FS\n^FO10,2.5\n^FB90,7.5,0,C\n^FH^FDE _5E_7E_5F C^FS\n^XZ\n", $this->getZpl());
118119
}
@@ -126,7 +127,7 @@ public function drawText(): void
126127
}
127128

128129
#[Test]
129-
public function drawTextWithEspecialCharaters(): void
130+
public function drawTextWithEspecialCharacters(): void
130131
{
131132
$this->driver->drawText(5, 40, 'E ^~_ C');
132133

0 commit comments

Comments
 (0)