22
33namespace Zpl ;
44
5- use Zpl \Commands \Exception ;
5+ use Zpl \Enums \Barcode ;
6+ use Zpl \Enums \Orientation ;
7+ use Zpl \Enums \Unit ;
68
79abstract 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 ;
0 commit comments