33namespace Zpl ;
44
55use Zpl \Commands \GraphicField ;
6+ use Zpl \Enums \Barcode ;
7+ use Zpl \Enums \Orientation ;
68
79class ZplBuilder extends AbstractBuilder
810{
@@ -163,6 +165,7 @@ public function setDarkness(float $value): void
163165 */
164166 public function setOrientation (string $ orientation = 'N ' ): void
165167 {
168+ Orientation::validate ($ orientation );
166169 $ this ->commands [] = '^FW ' . $ orientation ;
167170 }
168171
@@ -182,7 +185,7 @@ public function invertLabelOrientation(bool $isInvert = true): void
182185 */
183186 public function drawText (float $ x , float $ y , string $ text , string $ orientation = 'N ' , bool $ invert = false ): void
184187 {
185- $ this ->commands [] = ' ^FW ' . $ orientation ;
188+ $ this ->setOrientation ( $ orientation) ;
186189 $ this ->commands [] = '^FO ' . $ this ->toDots ($ x ) . ', ' . $ this ->toDots ($ y );
187190 if ($ invert === true ) {
188191 $ this ->commands [] = '^FR ' ;
@@ -297,33 +300,57 @@ public function drawCell(
297300 /**
298301 * {@inheritDoc}
299302 *
300- * @see \Zpl\AbstractBuilder::drawCode128 ()
303+ * @see \Zpl\AbstractBuilder::drawBarcode ()
301304 */
302- public function drawCode128 ( float $ x , float $ y , float $ height , string $ data , bool $ printData = false , string $ orientation = 'N ' , int $ size = 0 ): void
305+ 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
303306 {
304- $ validOrientations = ['N ' , 'R ' , 'I ' , 'B ' ];
305- if (in_array ($ orientation , $ validOrientations ) === false ) {
306- throw new \InvalidArgumentException ('Valid values for orientation are: ' . implode (', ' , $ validOrientations ));
307- }
307+ Barcode::validate ($ type );
308+ Orientation::validate ($ orientation );
308309
309310 $ this ->commands [] = '^FO ' . $ this ->toDots ($ x ) . ', ' . $ this ->toDots ($ y );
310311 if ($ size > 0 && $ size <= 9 ) {
311312 $ this ->commands [] = '^BY ' . $ size ;
312313 }
313- $ this ->commands [] = '^BC ' . $ orientation . ', ' . $ this ->toDots ($ height ) . ', ' . ($ printData === true ? 'Y ' : 'N ' ) . ',N,N,A ' ;
314+
315+ $ barcode = [strtoupper ($ type ), $ orientation , $ this ->toDots ($ height ), $ printData , $ labelAbove , 'N ' , 'A ' ];
316+ if (in_array ($ barcode [0 ], [Barcode::CODE39 , Barcode::CODE11 , Barcode::ANSI , Barcode::PLESSEY ])) {
317+ array_splice ($ barcode , 2 , 0 , 'N ' );
318+ } elseif ($ barcode [0 ] === Barcode::QR ) {
319+ array_splice ($ barcode , 2 , 0 , '2 ' );
320+ $ data = 'QA, ' . $ data ;
321+ $ barcode = array_slice ($ barcode , 0 , 3 );
322+ $ barcode [] = $ height ;
323+ } elseif ($ barcode [0 ] === Barcode::MSI ) {
324+ array_splice ($ barcode , 2 , 0 , 'B ' );
325+ }
326+ if (in_array ($ barcode [0 ], ['B1 ' , 'B2 ' , 'B5 ' , 'B8 ' , 'BE ' , 'BI ' , 'BU ' , 'BS ' ])) {
327+ $ barcode = array_slice ($ barcode , 0 , 5 );
328+ } elseif (in_array ($ barcode [0 ], ['B3 ' , 'B4 ' , 'BA ' , 'BB ' , 'BD ' , 'BF ' , 'BJ ' , 'BK ' , 'BL ' , 'BM ' , 'BP ' ])) {
329+ $ barcode = array_slice ($ barcode , 0 , 6 );
330+ }
331+
332+ $ this ->commands [] = $ this ->command ($ barcode );
314333 $ this ->commands [] = '^FD ' . $ data . '^FS ' ;
315334 }
316335
336+ /**
337+ * {@inheritDoc}
338+ *
339+ * @see \Zpl\AbstractBuilder::drawCode128()
340+ */
341+ public function drawCode128 (float $ x , float $ y , float $ height , string $ data , bool $ printData = false , string $ orientation = 'N ' , int $ size = 0 ): void
342+ {
343+ $ this ->drawBarcode (Barcode::CODE128 , $ x , $ y , $ height , $ data , $ printData , false , $ orientation , $ size );
344+ }
345+
317346 /**
318347 * {@inheritDoc}
319348 *
320349 * @see \Zpl\AbstractBuilder::drawQrCode()
321350 */
322351 public function drawQrCode (float $ x , float $ y , string $ data , int $ size = 10 ): void
323352 {
324- $ this ->commands [] = '^FO ' . $ this ->toDots ($ x ) . ', ' . $ this ->toDots ($ y );
325- $ this ->commands [] = '^BQN,2, ' . $ size ;
326- $ this ->commands [] = '^FDQA, ' . $ data . '^FS ' ;
353+ $ this ->drawBarcode (Barcode::QR , $ x , $ y , (float ) $ size , $ data );
327354 }
328355
329356 /**
@@ -333,12 +360,7 @@ public function drawQrCode(float $x, float $y, string $data, int $size = 10): vo
333360 */
334361 public function drawCode39 (float $ x , float $ y , float $ height , string $ data , bool $ printData = false , string $ orientation = 'N ' , int $ size = 0 ): void
335362 {
336- $ this ->commands [] = '^FO ' . $ this ->toDots ($ x ) . ', ' . $ this ->toDots ($ y );
337- if ($ size > 0 && $ size <= 9 ) {
338- $ this ->commands [] = '^BY ' . $ size ;
339- }
340- $ this ->commands [] = '^B3 ' . $ orientation . ',N, ' . $ this ->toDots ($ height ) . ', ' . ($ printData === true ? 'Y ' : 'N ' ) . ',N ' ;
341- $ this ->commands [] = '^FD ' . $ data . '^FS ' ;
363+ $ this ->drawBarcode (Barcode::CODE39 , $ x , $ y , $ height , $ data , $ printData , false , $ orientation , $ size );
342364 }
343365
344366 /**
0 commit comments