Skip to content

Commit

Permalink
Adding the possibility to disable hexadecimal within texts
Browse files Browse the repository at this point in the history
  • Loading branch information
stephandesouza committed Jan 6, 2021
1 parent 7b544c9 commit 89b3914
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ parameters:
level: 8
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
checkMissingIterableValueType: false
29 changes: 28 additions & 1 deletion src/Weez/Zpl/Model/Element/ZebraText.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class ZebraText extends ZebraElement
*/
protected $text;

/**
* @var bool
*/
protected $allowHexadecimal;

/**
* @var PrinterOptions
*/
Expand All @@ -71,8 +76,26 @@ public function __construct($positionX, $positionY, $text, $fontSize = null, $ze
$this->positionY = $positionY;
$this->fieldBlock = $fieldBlock;
$this->printerOptions = new PrinterOptions();
$this->allowHexadecimal = true;
}

/**
* @return bool
*/
public function isAllowHexadecimal(): bool
{
return $this->allowHexadecimal;
}

/**
* @param bool $allowHexadecimal
* @return self
*/
public function setAllowHexadecimal(bool $allowHexadecimal)
{
$this->allowHexadecimal = $allowHexadecimal;
return $this;
}

/**
*
Expand All @@ -98,7 +121,11 @@ public function getZplCode($_printerOptions = null)
$zpl .= $this->fieldBlock->getZplCode($printerOptions);
}

$zpl .= "^FH\\^FD"; //We allow hexadecimal and start element
if ($this->allowHexadecimal) {
$zpl .= "^FH\\";
}

$zpl .= "^FD"; //We allow hexadecimal and start element
$zpl .= ZplUtils::convertAccentToZplAsciiHexa($this->text);
$zpl .= ZplUtils::zplCommandSautLigne("FS");

Expand Down
34 changes: 34 additions & 0 deletions tests/Weez/Zpl/Tests/Model/Element/ZebraTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Weez\Zpl\Tests\Model\Element;

use PHPUnit\Framework\TestCase;
use Weez\Zpl\Constant\ZebraAlignment;
use Weez\Zpl\Model\Element\ZebraFieldBlock;
use Weez\Zpl\Model\Element\ZebraText;

class ZebraTextTest extends TestCase
{

public function testGetZplCode(): void
{
$text = new ZebraText(20, 20, "Text");
self::assertEquals("^FT20,20^FH\^FDText^FS\n", $text->getZplCode());
}

public function testGetZplCodeWithBlockTest(): void
{
$block = new ZebraFieldBlock(500, 2, 1, new ZebraAlignment(ZebraAlignment::RIGHT));
$text = new ZebraText(20, 20, "Text", null, null, null, $block);
self::assertEquals("^FT20,20^FB500,2,1,R^FH\^FDText^FS\n", $text->getZplCode());
}

public function testGetZplCodeAllowDecimalTest(): void
{
$text = new ZebraText(20, 20, "Text\&Multilined");
$text->setAllowHexadecimal(false);

self::assertFalse($text->isAllowHexadecimal());
self::assertEquals("^FT20,20^FDText\&Multilined^FS\n", $text->getZplCode());
}
}

0 comments on commit 89b3914

Please sign in to comment.