Skip to content

Commit

Permalink
Refactor datamatrix 3th encoding param feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Sep 5, 2024
1 parent 52c5360 commit 611192c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ Create a composer.json in your projects root-directory:
```json
{
"require": {
"tecnickcom/tc-lib-barcode": "^2.0"
"tecnickcom/tc-lib-barcode": "^2.3"
}
}
```

Or add to an existing project with:

```bash
composer require tecnickcom/tc-lib-barcode ^2.0
composer require tecnickcom/tc-lib-barcode ^2.3
```

## Packaging
Expand All @@ -166,7 +166,7 @@ this library includes make targets for building these packages (`make rpm` and `
The packages are generated under the `target` directory.

When this library is installed using an RPM or DEB package, you can use it your code by including the autoloader:
```
```php
require_once ('/usr/share/php/Com/Tecnick/Barcode/autoload.php');
```

Expand Down
26 changes: 13 additions & 13 deletions src/Type/Square/Datamatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,32 @@ class Datamatrix extends \Com\Tecnick\Barcode\Type\Square
protected bool $gsonemode = false;

/**
* Datamatrix default encoding (0=ASCII (default), 1=C40, 2=TXT, 3=X12, 4=EDF, 5=BASE256)
* Datamatrix default encoding.
* See Data::SWITCHCDW for valid values.
*/
protected int $encoding = Data::ENC_ASCII;
protected int $defenc = Data::ENC_ASCII;

/**
* Set extra (optional) parameters:
* 1: SHAPE - S=square (default), R=rectangular
* 2: MODE - N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version
* 1: SHAPE: S=square (default), R=rectangular.
* 2: MODE: N=default, GS1 = the FNC1 codeword is added in the first position of Data Matrix ECC 200 version.
* 3: ENCODING: ASCII (default), C40, TXT, X12, EDIFACT, BASE256.
*/
protected function setParameters(): void
{
parent::setParameters();

// shape
if (isset($this->params[0]) && ($this->params[0] == 'R')) {
if (isset($this->params[0]) && ($this->params[0] === 'R')) {
$this->shape = 'R';
}

// mode
if (isset($this->params[1]) && ($this->params[1] === 'GS1')) {
$this->gsonemode = true;
}
$this->gsonemode = (isset($this->params[1]) && ($this->params[1] === 'GS1'));

// encoding
if (isset($this->params[2]) && array_key_exists(intval($this->params[2]), Data::SWITCHCDW)) {
$this->encoding = intval($this->params[2]);
if (isset($this->params[2])) {
$this->defenc = Data::ENCOPTS[$this->params[2]] ?? Data::ENC_ASCII;
}
}

Expand Down Expand Up @@ -231,7 +231,7 @@ protected function setGrid(
protected function getHighLevelEncoding(string $data): array
{
// STEP A. Start in predefined encodation.
$enc = $this->encoding; // current encoding mode
$enc = $this->defenc; // current encoding mode
$this->dmx->last_enc = $enc; // last used encoding
$pos = 0; // current position
$cdw = []; // array of codewords to be returned
Expand All @@ -240,8 +240,8 @@ protected function getHighLevelEncoding(string $data): array
$field_length = 0; // number of chars in current field

// Switch to predefined encoding (no action needed if ASCII because it's the default encoding)
if ($this->encoding !== Data::ENC_ASCII) {
$cdw[] = $this->dmx->getSwitchEncodingCodeword($this->encoding);
if ($this->defenc !== Data::ENC_ASCII) {
$cdw[] = $this->dmx->getSwitchEncodingCodeword($this->defenc);
++$cdw_num;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Type/Square/Datamatrix/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ class Data
*/
public const ENC_ASCII_NUM = 7;

/**
* Encoding options that can be specified as input parameter.
*
* @var array<string, int>
*/
public const ENCOPTS = [
'ASCII' => Data::ENC_ASCII,
'C40' => Data::ENC_C40,
'TXT' => Data::ENC_TXT,
'X12' => Data::ENC_X12,
'EDF' => Data::ENC_EDF,
'BASE256' => Data::ENC_BASE256,
];

/**
* Switch codewords.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Square/Datamatrix/Modes.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ protected function getMaxDataCodewords(int $numcw): int

/**
* Get the switching codeword to a new encoding mode (latch codeword)
*
* @param int $mode New encoding mode.
*
* @return int Switch codeword.
* @protected
*/
public function getSwitchEncodingCodeword(int $mode): int
{
Expand Down
14 changes: 7 additions & 7 deletions test/Square/DatamatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,37 +393,37 @@ public static function getGridDataProvider(): array
],
// Different encoding datamatrix
[
'DATAMATRIX,S,N,0', // ASCII
'DATAMATRIX,S,N,ASCII',
'01234567890',
'ac7dd9e1ebdb42d07fe928fb33cd307b'
],
[
'DATAMATRIX,S,N,1', // C40
'DATAMATRIX,S,N,C40',
'01234567890',
'958a7a3bcd036d7135489eb703a25633'
],
[
'DATAMATRIX,S,N,2', // TXT
'DATAMATRIX,S,N,TXT',
'01234567890',
'057981dfbf527b029ae59d65fb55f61d'
],
[
'DATAMATRIX,S,N,3', // X12
'DATAMATRIX,S,N,X12',
'01234567890',
'8d75b0fcfb2d0977abd95004a6ba98dd'
],
[
'DATAMATRIX,S,N,4', // EDF
'DATAMATRIX,S,N,EDF',
'01234567890',
'989eab3ca16c97e05dd2307bef32f64b'
],
[
'DATAMATRIX,S,N,5', // BASE256
'DATAMATRIX,S,N,BASE256',
'01234567890',
'8b4f688a774130bc654e39dfcfadb482'
],
[
'DATAMATRIX,S,GS1,1', // GS1 + C40
'DATAMATRIX,S,GS1,C40',
"\xE8" . '01095011010209171719050810ABCD1234' . "\xE8" . '2110',
'ba117111dfa40a40e1bb968c719d2eef'
]
Expand Down

0 comments on commit 611192c

Please sign in to comment.