diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index a8ff52ab..00000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -github: casperbakker diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 5739810e..33b5a11b 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - php-versions: ['8.1', '8.2', '8.3'] + php-versions: ['8.2', '8.3'] steps: - name: Checkout code diff --git a/Readme.md b/Readme.md index dfdfea25..2692cca2 100644 --- a/Readme.md +++ b/Readme.md @@ -7,8 +7,6 @@ This is an easy to use, non-bloated, framework independent, barcode generator in It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards. -*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* - ## No support for... - No support for any **2D** barcodes, like QR codes. - We only generate the 'bars' part of a barcode, without text below the barcode. If you want text of the code below the barcode, you could add it later to the output of this package. @@ -23,51 +21,97 @@ composer require picqer/php-barcode-generator If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well. ## Usage -Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want. +You want a barcode for a specific "type" (for example Code 128 or UPC) in a specific image format (for example PNG or SVG). + +- First, encode the string you want the barcode of into a `Barcode` object with one of the barcode types. +- Then, use one of the renderers to render the image of the bars in the `Barcode` object. + +> The "type" is a standard that defines which characters you can encode and which bars represent which character. The most used types are [code 128](https://en.wikipedia.org/wiki/Code_128) and [EAN/UPC](https://en.wikipedia.org/wiki/International_Article_Number). Not all characters can be encoded into each barcode type, and not all barcode scanners can read all types. ```php getBarcode('081231723897', $generator::TYPE_CODE_128); +// Make Barcode object of Code128 encoding. +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + +// Output the barcode as HTML in the browser with a HTML Renderer +$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); +echo $renderer->render($barcode); ``` Will result in this beauty:
![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg) -The `getBarcode()` method accepts the following parameters: -- `$barcode` String needed to encode in the barcode -- `$type` Type of barcode, use the constants defined in the class -- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default -- `$height` The total height of the barcode in pixels -- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color) - -Example of usage of all parameters: - +Each renderer has their own options. For example, you can set the height, width and color of a PNG: ```php getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor)); +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); +$renderer->setForegroundColor($colorRed); + +// Save PNG to the filesystem, with widthFactor 3 and height of 50 pixels +file_put_contents('barcode.png', $renderer->render($barcode, 3, 50)); ``` -## Image types +## Image renderers +Available image renderers: SVG, PNG, JPG and HTML. + +Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers: + +### SVG +A vector based SVG image. Gives the best quality to print. ```php -$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG -$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG -$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white +$renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default) +$renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image + +$renderer->render($barcode, 450.20, 75); // Width and height support floats +```` + +### PNG + JPG +All options for PNG and JPG are the same. +```php +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); +$renderer->setForegroundColor([255, 0, 0]); // Give a color for the bars, the background is always white. Give it as 3 times 0-255 values for red, green and blue. +$renderer->useGd(); // If you have Imagick and GD installed, but want to use GD +$renderer->useImagick(); // If you have Imagick and GD installed, but want to use Imagick + +$renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every bar is), and the height in pixels +```` + +### HTML +Gives HTML to use inline in a full HTML document. +```php +$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white + +$renderer->render($barcode, 450.20, 75); // Width and height support floats +```` + +### Dynamic HTML +Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size. +```php +$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white + +$renderer->render($barcode); +```` + +You can put the rendered HTML inside a div like this: +```html +
``` ## Accepted barcode types -These barcode types are supported. All types support different character sets or have mandatory lengths. Please see wikipedia for supported chars and lengths per type. +These barcode types are supported. All types support different character sets and some have mandatory lengths. Please see wikipedia for supported chars and lengths per type. + +You can find all supported types in the [src/Types](src/Types) folder. Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported. @@ -107,23 +151,122 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner [See example images for all supported barcode types](examples.md) ## A note about PNG and JPG images -If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). This package will use Imagick if that is installed, or fall back to GD. If you have both installed but you want a specific method, you can use `$generator->useGd()` or `$generator->useImagick()` to force your preference. +If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). This package will use Imagick if that is installed, or fall back to GD. If you have both installed, but you want a specific method, you can use `$renderer->useGd()` or `$renderer->useImagick()` to force your preference. ## Examples ### Embedded PNG image in HTML ```php +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); +echo ''; +``` + +### Save JPG barcode to disk +```php +$barcode = (new Picqer\Barcode\Types\TypeCodabar())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + +file_put_contents('barcode.jpg', $renderer->render($barcode)); +``` + +### Oneliner SVG output to disk +```php +file_put_contents('barcode.svg', (new Picqer\Barcode\Renderers\SvgRenderer())->render((new Picqer\Barcode\Types\TypeKix())->getBarcode('6825ME601'))); +``` + +## Upgrading to v3 +There is no need to change anything when upgrading from v2 to v3. Above you find the new preferred way of using this library since v3. But the old style still works. + +If you want to convert to the new style, here is an example: +```php +// Old style +$generator = new Picqer\Barcode\BarcodeGeneratorSVG(); +echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); + +// New style +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); +echo $renderer->render($barcode); +``` + +The width in the SVG and HTML renderer is now the width of the end result, instead of the widthFactor. If you want to keep dynamic widths, you can get the width of the encoded Barcode and multiply it by the widthFactor to get the same result as before. See here an example for a widthFactor of 2: +```php +// Old style +$generator = new Picqer\Barcode\BarcodeGeneratorSVG(); +echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2. 30); + +// New style +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); +echo $renderer->render($barcode, $barcode->getWidth() * 2, 30); +``` + +--- + +## Previous style generators +In version 3 the barcode type encoders and image renderers are completely separate. This makes building your own renderer way easier. The old way was using "generators". Below are the old examples of these generators, which still works in v3 as well. + +### Usage +Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want. + +```php +getBarcode('081231723897', $generator::TYPE_CODE_128); +``` + +Will result in this beauty:
+![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg) + +The `getBarcode()` method accepts the following parameters: +- `$barcode` String needed to encode in the barcode +- `$type` Type of barcode, use the constants defined in the class +- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default +- `$height` The total height of the barcode in pixels +- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color) + +Example of usage of all parameters: + +```php +getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor)); +``` + +### Image types +```php +$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG +$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG +$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG +$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML +$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML +``` + +#### Embedded PNG image in HTML +```php $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); echo ''; ``` -### Save JPG barcode to disk +#### Save JPG barcode to disk ```php $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR)); ``` -### Oneliner SVG output to disk +#### Oneliner SVG output to disk ```php file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX)); ``` + +--- +*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* diff --git a/composer.json b/composer.json index dadc0d2c..e65b8e8c 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "ext-mbstring": "*" }, "require-dev": { diff --git a/examples.md b/examples.md index 00329a5b..689c1928 100644 --- a/examples.md +++ b/examples.md @@ -2,137 +2,147 @@ These are examples of supported barcodes with this library. +All types can be found in the src/Types directory. -### C39 -![Barcode 1234567890ABC as C39](tests/verified-files/C39-1234567890ABC.svg) +### TypeCode39 -### C39+ +![Barcode 1234567890ABC as Picqer\Barcode\Types\TypeCode39](tests/verified-files/TypeCode39-1234567890ABC.svg) -![Barcode 1234567890ABC as C39+](tests/verified-files/C39+-1234567890ABC.svg) +### TypeCode39Checksum -### C39E +![Barcode 1234567890ABC as Picqer\Barcode\Types\TypeCode39Checksum](tests/verified-files/TypeCode39Checksum-1234567890ABC.svg) -![Barcode 1234567890abcABC as C39E](tests/verified-files/C39E-1234567890abcABC.svg) +### TypeCode39Extended -### C39E+ +![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode39Extended](tests/verified-files/TypeCode39Extended-1234567890abcABC.svg) -![Barcode 1234567890abcABC as C39E+](tests/verified-files/C39E+-1234567890abcABC.svg) +### TypeCode39ExtendedChecksum -### C93 +![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode39ExtendedChecksum](tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg) -![Barcode 1234567890abcABC as C93](tests/verified-files/C93-1234567890abcABC.svg) +### TypeCode93 -### S25 +![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode93](tests/verified-files/TypeCode93-1234567890abcABC.svg) -![Barcode 1234567890 as S25](tests/verified-files/S25-1234567890.svg) +### TypeStandard2of5 -### S25+ +![Barcode 1234567890 as Picqer\Barcode\Types\TypeStandard2of5](tests/verified-files/TypeStandard2of5-1234567890.svg) -![Barcode 1234567890 as S25+](tests/verified-files/S25+-1234567890.svg) +### TypeStandard2of5Checksum -### I25 +![Barcode 1234567890 as Picqer\Barcode\Types\TypeStandard2of5Checksum](tests/verified-files/TypeStandard2of5Checksum-1234567890.svg) -![Barcode 1234567890 as I25](tests/verified-files/I25-1234567890.svg) +### TypeInterleaved25 -### I25+ +![Barcode 1234567890 as Picqer\Barcode\Types\TypeInterleaved25](tests/verified-files/TypeInterleaved25-1234567890.svg) -![Barcode 1234567890 as I25+](tests/verified-files/I25+-1234567890.svg) +### TypeInterleaved25Checksum -### EAN13 +![Barcode 1234567890 as Picqer\Barcode\Types\TypeInterleaved25Checksum](tests/verified-files/TypeInterleaved25Checksum-1234567890.svg) -![Barcode 081231723897 as EAN13](tests/verified-files/EAN13-081231723897.svg) +### TypeEan13 -![Barcode 0049000004632 as EAN13](tests/verified-files/EAN13-0049000004632.svg) +![Barcode 081231723897 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-081231723897.svg) -![Barcode 004900000463 as EAN13](tests/verified-files/EAN13-004900000463.svg) +![Barcode 0049000004632 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-0049000004632.svg) -### ITF14 +![Barcode 004900000463 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-004900000463.svg) -![Barcode 00012345600012 as ITF14](tests/verified-files/ITF14-00012345600012.svg) +### TypeITF14 -![Barcode 05400141288766 as ITF14](tests/verified-files/ITF14-05400141288766.svg) +![Barcode 00012345600012 as Picqer\Barcode\Types\TypeITF14](tests/verified-files/TypeITF14-00012345600012.svg) -### C128 +![Barcode 05400141288766 as Picqer\Barcode\Types\TypeITF14](tests/verified-files/TypeITF14-05400141288766.svg) -![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg) +### TypeCode128 -![Barcode 1234567890abcABC-283*33 as C128](tests/verified-files/C128-1234567890abcABC-283-33.svg) +![Barcode 081231723897 as Picqer\Barcode\Types\TypeCode128](tests/verified-files/TypeCode128-081231723897.svg) -### C128A +![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeCode128](tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg) -![Barcode 1234567890 as C128A](tests/verified-files/C128A-1234567890.svg) +### TypeCode128A -### C128B +![Barcode 1234567890 as Picqer\Barcode\Types\TypeCode128A](tests/verified-files/TypeCode128A-1234567890.svg) -![Barcode 081231723897 as C128B](tests/verified-files/C128B-081231723897.svg) +### TypeCode128B -![Barcode 1234567890abcABC-283*33 as C128B](tests/verified-files/C128B-1234567890abcABC-283-33.svg) +![Barcode 081231723897 as Picqer\Barcode\Types\TypeCode128B](tests/verified-files/TypeCode128B-081231723897.svg) -### EAN2 +![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeCode128B](tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg) -![Barcode 22 as EAN2](tests/verified-files/EAN2-22.svg) +### TypeUpcExtension2 -### EAN5 +![Barcode 22 as Picqer\Barcode\Types\TypeUpcExtension2](tests/verified-files/TypeUpcExtension2-22.svg) -![Barcode 1234567890abcABC-283*33 as EAN5](tests/verified-files/EAN5-1234567890abcABC-283-33.svg) +### TypeUpcExtension5 -### EAN8 +![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeUpcExtension5](tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg) -![Barcode 1234568 as EAN8](tests/verified-files/EAN8-1234568.svg) +### TypeEan8 -### UPCA +![Barcode 1234568 as Picqer\Barcode\Types\TypeEan8](tests/verified-files/TypeEan8-1234568.svg) -![Barcode 123456789 as UPCA](tests/verified-files/UPCA-123456789.svg) +### TypeUpcA -### UPCE +![Barcode 123456789 as Picqer\Barcode\Types\TypeUpcA](tests/verified-files/TypeUpcA-123456789.svg) -![Barcode 123456789 as UPCE](tests/verified-files/UPCE-123456789.svg) +### TypeUpcE -### MSI +![Barcode 123456789 as Picqer\Barcode\Types\TypeUpcE](tests/verified-files/TypeUpcE-123456789.svg) -![Barcode 123456789 as MSI](tests/verified-files/MSI-123456789.svg) +### TypeMsi -### MSI+ +![Barcode 123456789 as Picqer\Barcode\Types\TypeMsi](tests/verified-files/TypeMsi-123456789.svg) -![Barcode 123456789 as MSI+](tests/verified-files/MSI+-123456789.svg) +### TypeMsiChecksum -### POSTNET +![Barcode 123456789 as Picqer\Barcode\Types\TypeMsiChecksum](tests/verified-files/TypeMsiChecksum-123456789.svg) -![Barcode 123456789 as POSTNET](tests/verified-files/POSTNET-123456789.svg) +### TypePostnet -### PLANET +![Barcode 123456789 as Picqer\Barcode\Types\TypePostnet](tests/verified-files/TypePostnet-123456789.svg) -![Barcode 123456789 as PLANET](tests/verified-files/PLANET-123456789.svg) +### TypePlanet -### RMS4CC +![Barcode 123456789 as Picqer\Barcode\Types\TypePlanet](tests/verified-files/TypePlanet-123456789.svg) -![Barcode 123456789 as RMS4CC](tests/verified-files/RMS4CC-123456789.svg) +### TypeRms4cc -### KIX +![Barcode 123456789 as Picqer\Barcode\Types\TypeRms4cc](tests/verified-files/TypeRms4cc-123456789.svg) -![Barcode 123456789 as KIX](tests/verified-files/KIX-123456789.svg) +### TypeKix -### IMB +![Barcode 123456789 as Picqer\Barcode\Types\TypeKix](tests/verified-files/TypeKix-123456789.svg) -![Barcode 123456789 as IMB](tests/verified-files/IMB-123456789.svg) +### TypeIntelligentMailBarcode -### CODABAR +![Barcode 123456789 as Picqer\Barcode\Types\TypeIntelligentMailBarcode](tests/verified-files/TypeIntelligentMailBarcode-123456789.svg) -![Barcode 123456789 as CODABAR](tests/verified-files/CODABAR-123456789.svg) +### TypeCodabar -### CODE11 +![Barcode 123456789 as Picqer\Barcode\Types\TypeCodabar](tests/verified-files/TypeCodabar-123456789.svg) -![Barcode 123456789 as CODE11](tests/verified-files/CODE11-123456789.svg) +### TypeCode11 -### PHARMA +![Barcode 123456789 as Picqer\Barcode\Types\TypeCode11](tests/verified-files/TypeCode11-123456789.svg) -![Barcode 123456789 as PHARMA](tests/verified-files/PHARMA-123456789.svg) +### TypePharmacode -### PHARMA2T +![Barcode 123456789 as Picqer\Barcode\Types\TypePharmacode](tests/verified-files/TypePharmacode-123456789.svg) -![Barcode 123456789 as PHARMA2T](tests/verified-files/PHARMA2T-123456789.svg) +### TypePharmacodeTwoCode + +![Barcode 123456789 as Picqer\Barcode\Types\TypePharmacodeTwoCode](tests/verified-files/TypePharmacodeTwoCode-123456789.svg) + +### TypeTelepen + +![Barcode 1234567890ASCD as Picqer\Barcode\Types\TypeTelepen](tests/verified-files/TypeTelepen-1234567890ASCD.svg) + +### TypeTelepenNumeric + +![Barcode 1234567890 as Picqer\Barcode\Types\TypeTelepenNumeric](tests/verified-files/TypeTelepenNumeric-1234567890.svg) diff --git a/generate-examples.php b/generate-examples.php index 5929908f..7be0f009 100644 --- a/generate-examples.php +++ b/generate-examples.php @@ -1,9 +1,5 @@ getBarcode('081231723897', $generatorSVG::TYPE_EAN_13)); -file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13, 0.25, 25.75)); +$barcode = $typeEncoderEan13->getBarcode('081231723897'); +file_put_contents('tests/verified-files/081231723897-ean13.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 2)); +file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 0.25, 25.75)); -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); -file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128)); +$barcode = $typeEncoderCode128->getBarcode('081231723897'); +file_put_contents('tests/verified-files/081231723897-code128.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); -file_put_contents('tests/verified-files/12345678903-imb.html', $generatorHTML->getBarcode('12345678903', $generatorHTML::TYPE_IMB)); +$barcode = $typeEncoderIMB->getBarcode('12345678903'); +file_put_contents('tests/verified-files/12345678903-imb.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); -$generatorDynamicHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); -file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $generatorDynamicHTML->getBarcode('081231723897', $generatorDynamicHTML::TYPE_CODE_128)); +$barcode = $typeEncoderCode128->getBarcode('081231723897'); +file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $dynamicHtmlRenderer->render($barcode)); -file_put_contents('tests/verified-files/12345678903-dynamic-imb.html', $generatorDynamicHTML->getBarcode('12345678903', $generatorDynamicHTML::TYPE_IMB)); +$barcode = $typeEncoderIMB->getBarcode('12345678903'); +file_put_contents('tests/verified-files/12345678903-dynamic-imb.html', $dynamicHtmlRenderer->render($barcode)); -$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); -file_put_contents('tests/verified-files/0049000004632-ean13.svg', $generatorSVG->getBarcode('0049000004632', $generatorSVG::TYPE_EAN_13)); +$barcode = $typeEncoderEan13->getBarcode('0049000004632'); +file_put_contents('tests/verified-files/0049000004632-ean13.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 2)); -// New style of verified files +// New style of verified files, defined in VerifiedBarcodeTest.php require(__DIR__ . '/tests/VerifiedBarcodeTest.php'); $verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes; -$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); foreach ($verifiedFiles as $verifiedFile) { - foreach ($verifiedFile['barcodes'] as $barcode) { - file_put_contents('tests/verified-files/' . getSaveFilename($verifiedFile['type'] . '-' . $barcode) . '.svg', $generatorSVG->getBarcode($barcode, $verifiedFile['type'])); + foreach ($verifiedFile['barcodes'] as $barcodeText) { + $barcode = (new $verifiedFile['type']())->getBarcode($barcodeText); + $result = $svgRenderer->render($barcode, $barcode->getWidth() * 2); + + file_put_contents('tests/verified-files/' . Picqer\Barcode\Helpers\StringHelpers::getSafeFilenameFrom($verifiedFile['type'] . '-' . $barcodeText) . '.svg', $result); } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 04053543..0a16ff99 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,4 +1,4 @@ parameters: paths: - src - level: 4 + level: 5 diff --git a/src/Barcode.php b/src/Barcode.php index 4ab9f20e..5b210b33 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -4,17 +4,18 @@ class Barcode { - protected $barcode; - protected $width = 0; - protected $height = 0; - protected $bars = []; + protected string $barcode; + protected int $width = 0; + protected int $height = 0; + protected array $bars = []; public function __construct(string $barcode) { $this->barcode = $barcode; } - public function addBar(BarcodeBar $bar) + // Add a bar to the barcode, either a bar or a space, at the right side of the barcode + public function addBar(BarcodeBar $bar): void { $this->bars[] = $bar; $this->width += $bar->getWidth(); @@ -40,4 +41,4 @@ public function getBars(): array { return $this->bars; } -} \ No newline at end of file +} diff --git a/src/BarcodeBar.php b/src/BarcodeBar.php index 332a6612..28bd595e 100644 --- a/src/BarcodeBar.php +++ b/src/BarcodeBar.php @@ -2,12 +2,13 @@ namespace Picqer\Barcode; -class BarcodeBar +// Represents a single bar or space in a barcode +readonly class BarcodeBar { - protected $width; - protected $height; - protected $positionVertical; - protected $type; + protected int $width; + protected int $height; + protected int $positionVertical; + protected int $type; const TYPE_BAR = 1; const TYPE_SPACING = 0; @@ -39,4 +40,4 @@ public function isBar(): bool { return $this->type === self::TYPE_BAR; } -} \ No newline at end of file +} diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index d7c1b2e8..8555b061 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -45,6 +45,7 @@ use Picqer\Barcode\Types\TypeEan13; use Picqer\Barcode\Types\TypeEan8; use Picqer\Barcode\Types\TypeIntelligentMailBarcode; +use Picqer\Barcode\Types\TypeInterface; use Picqer\Barcode\Types\TypeInterleaved25; use Picqer\Barcode\Types\TypeInterleaved25Checksum; use Picqer\Barcode\Types\TypeITF14; @@ -101,14 +102,17 @@ abstract class BarcodeGenerator const TYPE_PHARMA_CODE = 'PHARMA'; const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T'; + /** + * @throws UnknownTypeException + */ protected function getBarcodeData(string $code, string $type): Barcode { $barcodeDataBuilder = $this->createDataBuilderForType($type); - return $barcodeDataBuilder->getBarcodeData($code); + return $barcodeDataBuilder->getBarcode($code); } - protected function createDataBuilderForType(string $type) + protected function createDataBuilderForType(string $type): TypeInterface { switch (strtoupper($type)) { case self::TYPE_CODE_32: diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 57ce23ea..c08a4edf 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -2,10 +2,10 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorDynamicHTML extends BarcodeGenerator { - private const WIDTH_PRECISION = 6; - /** * Return an HTML representation of barcode. * This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result. @@ -14,31 +14,15 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator * @param BarcodeGenerator::TYPE_* $type (string) type of barcode * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); - $html = '
' . PHP_EOL; - - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = $bar->getWidth() / $barcodeData->getWidth() * 100; - $barHeight = round(($bar->getHeight() / $barcodeData->getHeight() * 100), 3); - - if ($bar->isBar() && $barWidth > 0) { - $positionVertical = round(($bar->getPositionVertical() / $barcodeData->getHeight() * 100), 3); - - // draw a vertical bar - $html .= '
 
' . PHP_EOL; - } - - $positionHorizontal += $barWidth; - } - - $html .= '
' . PHP_EOL; + $renderer = new \Picqer\Barcode\Renderers\DynamicHtmlRenderer(); + $renderer->setForegroundColor($foregroundColor); - return $html; + return $renderer->render($barcodeData); } } diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 75a98fe6..bc30f252 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorHTML extends BarcodeGenerator { /** @@ -14,31 +16,17 @@ class BarcodeGeneratorHTML extends BarcodeGenerator * @param int $height Height of a single bar element in pixels. * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. + * @throws UnknownTypeException */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string + public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); - $html = '
' . PHP_EOL; - - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = round(($bar->getWidth() * $widthFactor), 3); - $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); - - if ($bar->isBar() && $barWidth > 0) { - $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); - - // draw a vertical bar - $html .= '
 
' . PHP_EOL; - } - - $positionHorizontal += $barWidth; - } + $width = round(($barcodeData->getWidth() * $widthFactor), 3); - $html .= '
' . PHP_EOL; + $renderer = new \Picqer\Barcode\Renderers\HtmlRenderer(); + $renderer->setForegroundColor($foregroundColor); - return $html; + return $renderer->render($barcodeData, $width, $height); } } diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index 0e33d700..2e270016 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -2,21 +2,54 @@ namespace Picqer\Barcode; -use Imagick; +use Picqer\Barcode\Exceptions\UnknownTypeException; -class BarcodeGeneratorJPG extends BarcodeGeneratorPNG +class BarcodeGeneratorJPG extends BarcodeGenerator { - protected function createImagickImageObject(int $width, int $height): Imagick + protected ?bool $useImagick = null; + + /** + * Return a PNG image representation of barcode (requires GD or Imagick library). + * + * @param string $barcode code to print + * @param BarcodeGenerator::TYPE_* $type (string) type of barcode + * @param int $widthFactor Width of a single bar element in pixels. + * @param int $height Height of a single bar element in pixels. + * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). + * @return string image data or false in case of error. + * @throws UnknownTypeException + */ + public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { - $image = new Imagick(); - $image->newImage($width, $height, 'white', 'JPG'); + $barcodeData = $this->getBarcodeData($barcode, $type); + + $renderer = new \Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->setForegroundColor($foregroundColor); + + if (! is_null($this->useImagick)) { + if ($this->useImagick) { + $renderer->useImagick(); + } else { + $renderer->useGd(); + } + } - return $image; + return $renderer->render($barcodeData, $widthFactor, $height); + } + + /** + * Force the use of Imagick image extension + */ + public function useImagick(): void + { + $this->useImagick = true; } - protected function generateGdImage($image) + /** + * Force the use of the GD image library + */ + public function useGd(): void { - imagejpeg($image); - imagedestroy($image); + $this->useImagick = false; } } diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 150e1e45..31edc567 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -2,45 +2,11 @@ namespace Picqer\Barcode; -use Imagick; -use imagickdraw; -use imagickpixel; -use Picqer\Barcode\Exceptions\BarcodeException; +use Picqer\Barcode\Exceptions\UnknownTypeException; class BarcodeGeneratorPNG extends BarcodeGenerator { - protected $useImagick = true; - - /** - * @throws BarcodeException - */ - public function __construct() - { - // Auto switch between GD and Imagick based on what is installed - if (extension_loaded('imagick')) { - $this->useImagick = true; - } elseif (function_exists('imagecreate')) { - $this->useImagick = false; - } else { - throw new BarcodeException('Neither gd-lib or imagick are installed!'); - } - } - - /** - * Force the use of Imagick image extension - */ - public function useImagick() - { - $this->useImagick = true; - } - - /** - * Force the use of the GD image library - */ - public function useGd() - { - $this->useImagick = false; - } + protected ?bool $useImagick = null; /** * Return a PNG image representation of barcode (requires GD or Imagick library). @@ -51,71 +17,39 @@ public function useGd() * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { $barcodeData = $this->getBarcodeData($barcode, $type); - $width = round($barcodeData->getWidth() * $widthFactor); - - if ($this->useImagick) { - $imagickBarsShape = new imagickdraw(); - $imagickBarsShape->setFillColor(new imagickpixel('rgb(' . implode(',', $foregroundColor) .')')); - } else { - $image = $this->createGdImageObject($width, $height); - $gdForegroundColor = imagecolorallocate($image, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); - } - // print bars - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $renderer = new \Picqer\Barcode\Renderers\PngRenderer(); + $renderer->setForegroundColor($foregroundColor); - if ($bar->isBar() && $barWidth > 0) { - $y = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); - $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); - - // draw a vertical bar - if ($this->useImagick) { - $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); - } else { - imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); - } + if (! is_null($this->useImagick)) { + if ($this->useImagick) { + $renderer->useImagick(); + } else { + $renderer->useGd(); } - $positionHorizontal += $barWidth; - } - - if ($this->useImagick) { - $image = $this->createImagickImageObject($width, $height); - $image->drawImage($imagickBarsShape); - return $image->getImageBlob(); } - ob_start(); - $this->generateGdImage($image); - return ob_get_clean(); - } - - protected function createGdImageObject(int $width, int $height) - { - $image = imagecreate($width, $height); - $colorBackground = imagecolorallocate($image, 255, 255, 255); - imagecolortransparent($image, $colorBackground); - - return $image; + return $renderer->render($barcodeData, $widthFactor, $height); } - protected function createImagickImageObject(int $width, int $height): Imagick + /** + * Force the use of Imagick image extension + */ + public function useImagick(): void { - $image = new Imagick(); - $image->newImage($width, $height, 'none', 'PNG'); - - return $image; + $this->useImagick = true; } - protected function generateGdImage($image) + /** + * Force the use of the GD image library + */ + public function useGd(): void { - imagepng($image); - imagedestroy($image); + $this->useImagick = false; } } diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 4d0d4775..ef7d63f6 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorSVG extends BarcodeGenerator { /** @@ -14,46 +16,17 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); - // replace table for special characters - $repstr = [ - "\0" => '', - '&' => '&', - '<' => '<', - '>' => '>', - ]; - $width = round(($barcodeData->getWidth() * $widthFactor), 3); - $svg = '' . PHP_EOL; - $svg .= '' . PHP_EOL; - $svg .= '' . PHP_EOL; - $svg .= "\t" . '' . strtr($barcodeData->getBarcode(), $repstr) . '' . PHP_EOL; - $svg .= "\t" . '' . PHP_EOL; - - // print bars - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = round(($bar->getWidth() * $widthFactor), 3); - $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); - - if ($bar->isBar() && $barWidth > 0) { - $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); - // draw a vertical bar - $svg .= "\t\t" . '' . PHP_EOL; - } - - $positionHorizontal += $barWidth; - } - - $svg .= "\t" . PHP_EOL; - $svg .= '' . PHP_EOL; + $renderer = new \Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setForegroundColor($foregroundColor); - return $svg; + return $renderer->render($barcodeData, $width, $height); } } diff --git a/src/Exceptions/InvalidOptionException.php b/src/Exceptions/InvalidOptionException.php new file mode 100644 index 00000000..6a9b5ad8 --- /dev/null +++ b/src/Exceptions/InvalidOptionException.php @@ -0,0 +1,5 @@ +' . PHP_EOL; + + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = $bar->getWidth() / $barcode->getWidth() * 100; + $barHeight = round(($bar->getHeight() / $barcode->getHeight() * 100), 3); + + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() / $barcode->getHeight() * 100), 3); + + // draw a vertical bar + $html .= '
 
' . PHP_EOL; + } + + $positionHorizontal += $barWidth; + } + + $html .= '' . PHP_EOL; + + return $html; + } + + public function setForegroundColor(string $color): self + { + $this->foregroundColor = $color; + + return $this; + } +} diff --git a/src/Renderers/HtmlRenderer.php b/src/Renderers/HtmlRenderer.php new file mode 100644 index 00000000..77ba389b --- /dev/null +++ b/src/Renderers/HtmlRenderer.php @@ -0,0 +1,45 @@ +getWidth(); + + $html = '
' . PHP_EOL; + + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3); + + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); + + // draw a vertical bar + $html .= '
 
' . PHP_EOL; + } + + $positionHorizontal += $barWidth; + } + + $html .= '
' . PHP_EOL; + + return $html; + } + + public function setForegroundColor(string $color): self + { + $this->foregroundColor = $color; + + return $this; + } +} diff --git a/src/Renderers/JpgRenderer.php b/src/Renderers/JpgRenderer.php new file mode 100644 index 00000000..132ec102 --- /dev/null +++ b/src/Renderers/JpgRenderer.php @@ -0,0 +1,22 @@ +newImage($width, $height, 'none', 'JPG'); + + return $image; + } + + protected function generateGdImage($image): void + { + \imagejpeg($image); + \imagedestroy($image); + } +} diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php new file mode 100644 index 00000000..8297d88e --- /dev/null +++ b/src/Renderers/PngRenderer.php @@ -0,0 +1,122 @@ +useImagick = true; + } elseif (function_exists('imagecreate')) { + $this->useImagick = false; + } else { + throw new BarcodeException('Neither gd-lib or imagick are installed!'); + } + } + + /** + * Force the use of Imagick image extension + */ + public function useImagick(): self + { + $this->useImagick = true; + return $this; + } + + /** + * Force the use of the GD image library + */ + public function useGd(): self + { + $this->useImagick = false; + return $this; + } + + public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string + { + $width = (int)round($barcode->getWidth() * $widthFactor); + + if ($this->useImagick) { + $imagickBarsShape = new ImagickDraw(); + $imagickBarsShape->setFillColor(new ImagickPixel('rgb(' . implode(',', $this->foregroundColor) .')')); + } else { + $image = $this->createGdImageObject($width, $height); + $gdForegroundColor = \imagecolorallocate($image, $this->foregroundColor[0], $this->foregroundColor[1], $this->foregroundColor[2]); + } + + // print bars + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = (int)round(($bar->getWidth() * $widthFactor)); + + if ($bar->isBar() && $barWidth > 0) { + $y = (int)round(($bar->getPositionVertical() * $height / $barcode->getHeight())); + $barHeight = (int)round(($bar->getHeight() * $height / $barcode->getHeight())); + + // draw a vertical bar + if ($this->useImagick) { + $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); + } else { + \imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); + } + } + $positionHorizontal += $barWidth; + } + + if ($this->useImagick) { + $image = $this->createImagickImageObject($width, $height); + $image->drawImage($imagickBarsShape); + return $image->getImageBlob(); + } else { + ob_start(); + $this->generateGdImage($image); + return ob_get_clean(); + } + } + + public function setForegroundColor(array $color): self + { + $this->foregroundColor = $color; + + return $this; + } + + protected function createGdImageObject(int $width, int $height) + { + $image = \imagecreate($width, $height); + $colorBackground = \imagecolorallocate($image, 255, 255, 255); + \imagecolortransparent($image, $colorBackground); + + return $image; + } + + protected function createImagickImageObject(int $width, int $height): Imagick + { + $image = new Imagick(); + $image->newImage($width, $height, 'none', 'PNG'); + + return $image; + } + + protected function generateGdImage($image): void + { + \imagepng($image); + \imagedestroy($image); + } +} diff --git a/src/Renderers/SvgRenderer.php b/src/Renderers/SvgRenderer.php new file mode 100644 index 00000000..6af44b37 --- /dev/null +++ b/src/Renderers/SvgRenderer.php @@ -0,0 +1,67 @@ +getWidth(); + + $svg = ''; + if ($this->svgType === self::TYPE_SVG_STANDALONE) { + $svg .= '' . PHP_EOL; + $svg .= '' . PHP_EOL; + } + $svg .= '' . PHP_EOL; + $svg .= "\t" . '' . htmlspecialchars($barcode->getBarcode()) . '' . PHP_EOL; + $svg .= "\t" . '' . PHP_EOL; + + // print bars + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3); + + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); + // draw a vertical bar + $svg .= "\t\t" . '' . PHP_EOL; + } + + $positionHorizontal += $barWidth; + } + + $svg .= "\t" . PHP_EOL; + $svg .= '' . PHP_EOL; + + return $svg; + } + + public function setForegroundColor(string $color): self + { + $this->foregroundColor = $color; + return $this; + } + + public function setSvgType(string $svgType): self + { + if (! in_array($svgType, [self::TYPE_SVG_INLINE, self::TYPE_SVG_STANDALONE])) { + throw new InvalidOptionException(); + } + + $this->svgType = $svgType; + return $this; + } +} diff --git a/src/Types/TypeCodabar.php b/src/Types/TypeCodabar.php index 0bf40e1d..e782dc4e 100644 --- a/src/Types/TypeCodabar.php +++ b/src/Types/TypeCodabar.php @@ -13,7 +13,7 @@ class TypeCodabar implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '11111221', '1' => '11112211', '2' => '11121121', @@ -36,7 +36,7 @@ class TypeCodabar implements TypeInterface 'D' => '11122211' ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $barcode = new Barcode($code); diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 2857cb39..d70cf5f9 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -13,7 +13,7 @@ class TypeCode11 implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '111121', '1' => '211121', '2' => '121121', @@ -28,7 +28,7 @@ class TypeCode11 implements TypeInterface 'S' => '112211', ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $barcode = new Barcode($code); @@ -60,27 +60,27 @@ public function getBarcodeData(string $code): Barcode private function getCheckDigitC(string $code): string { - $p = 1; - $check = 0; + $weight = 1; + $checksum = 0; for ($i = (strlen($code) - 1); $i >= 0; --$i) { $digit = $code[$i]; if ($digit == '-') { - $dval = 10; + $digitValue = 10; } else { - $dval = intval($digit); + $digitValue = intval($digit); } - $check += ($dval * $p); - ++$p; - if ($p > 10) { - $p = 1; + $checksum += ($digitValue * $weight); + ++$weight; + if ($weight > 10) { + $weight = 1; } } - $check %= 11; - if ($check == 10) { - $check = '-'; + $checksum %= 11; + if ($checksum == 10) { + $checksum = '-'; } - return $check; + return $checksum; } private function getCheckDigitK(string $code): string @@ -89,23 +89,23 @@ private function getCheckDigitK(string $code): string return ''; } - $p = 1; - $check = 0; + $weight = 1; + $checksum = 0; for ($i = (strlen($code) - 1); $i >= 0; --$i) { $digit = $code[$i]; if ($digit == '-') { - $dval = 10; + $digitValue = 10; } else { - $dval = intval($digit); + $digitValue = intval($digit); } - $check += ($dval * $p); - ++$p; - if ($p > 9) { - $p = 1; + $checksum += ($digitValue * $weight); + ++$weight; + if ($weight > 9) { + $weight = 1; } } - $check %= 11; + $checksum %= 11; - return (string)$check; + return (string)$checksum; } } diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index c8b8253c..96184052 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -18,9 +18,9 @@ class TypeCode128 implements TypeInterface { - protected $type = null; + protected ?string $type = null; - protected $conversionTable = [ + protected array $conversionTable = [ '212222', /* 00 */ '222122', /* 01 */ '222221', /* 02 */ @@ -131,7 +131,7 @@ class TypeCode128 implements TypeInterface '200000' /* END */ ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); @@ -383,7 +383,7 @@ public function getBarcodeData(string $code): Barcode * @return array sequence * @protected */ - protected function get128ABsequence($code) + protected function get128ABsequence($code): array { $len = strlen($code); $sequence = []; diff --git a/src/Types/TypeCode128A.php b/src/Types/TypeCode128A.php index ff2fe0cc..78bb46e5 100644 --- a/src/Types/TypeCode128A.php +++ b/src/Types/TypeCode128A.php @@ -12,5 +12,5 @@ class TypeCode128A extends TypeCode128 { - protected $type = 'A'; + protected ?string $type = 'A'; } diff --git a/src/Types/TypeCode128B.php b/src/Types/TypeCode128B.php index b462863f..6dbc0d88 100644 --- a/src/Types/TypeCode128B.php +++ b/src/Types/TypeCode128B.php @@ -12,5 +12,5 @@ class TypeCode128B extends TypeCode128 { - protected $type = 'B'; + protected ?string $type = 'B'; } diff --git a/src/Types/TypeCode128C.php b/src/Types/TypeCode128C.php index f4d44608..82566a22 100644 --- a/src/Types/TypeCode128C.php +++ b/src/Types/TypeCode128C.php @@ -12,5 +12,5 @@ class TypeCode128C extends TypeCode128 { - protected $type = 'C'; + protected ?string $type = 'C'; } diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index 6ca6816b..0f8a550b 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -13,7 +13,7 @@ */ class TypeCode32 extends TypeCode39 { - protected $conversionTable32 = [ + protected array $conversionTable32 = [ '0' => '0', '1' => '1', '2' => '2', @@ -48,7 +48,7 @@ class TypeCode32 extends TypeCode39 '31' => 'Z' ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { // Validate code 32. $stringLength = strlen($code); @@ -85,7 +85,7 @@ public function getBarcodeData(string $code): Barcode } // Return barcode data for code 39. - return parent::getBarcodeData($code39); + return parent::getBarcode($code39); } @@ -98,17 +98,17 @@ public function getBarcodeData(string $code): Barcode */ protected function checksum_code32(string $code): string { - $s = 0; + $checksum = 0; foreach (str_split($code) as $i => $c) { if (0 === $i % 2) { - $s += (int)$c; + $checksum += (int)$c; } else { $c = 2 * (int)$c; - $s += (int)floor($c / 10) + ($c % 10); + $checksum += (int)floor($c / 10) + ($c % 10); } } - return (string)($s % 10); + return (string)($checksum % 10); } } diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 9ec689e9..bf169944 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -14,10 +14,10 @@ class TypeCode39 implements TypeInterface { - protected $extended = false; - protected $checksum = false; + protected bool $extended = false; + protected bool $checksum = false; - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '111331311', '1' => '311311113', '2' => '113311113', @@ -64,7 +64,7 @@ class TypeCode39 implements TypeInterface '*' => '131131311', ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); @@ -115,8 +115,9 @@ public function getBarcodeData(string $code): Barcode * @param string $code code to represent. * @return string encoded string. * @protected + * @throws InvalidCharacterException */ - protected function encode_code39_ext($code) + protected function encode_code39_ext(string $code): string { $encode = [ chr(0) => '%U', @@ -317,13 +318,13 @@ protected function checksum_code39($code) '%' ]; - $sum = 0; - for ($i = 0; $i < strlen($code); ++$i) { - $k = array_keys($chars, $code[$i]); - $sum += $k[0]; + $checksum = 0; + for ($index = 0; $index < strlen($code); ++$index) { + $charPosition = array_keys($chars, $code[$index]); + $checksum += $charPosition[0]; } - $j = ($sum % 43); + $checksumIndex = ($checksum % 43); - return $chars[$j]; + return $chars[$checksumIndex]; } } diff --git a/src/Types/TypeCode39Checksum.php b/src/Types/TypeCode39Checksum.php index b129f284..7a47bb67 100644 --- a/src/Types/TypeCode39Checksum.php +++ b/src/Types/TypeCode39Checksum.php @@ -9,6 +9,6 @@ class TypeCode39Checksum extends TypeCode39 { - protected $extended = false; - protected $checksum = true; + protected bool $extended = false; + protected bool $checksum = true; } diff --git a/src/Types/TypeCode39Extended.php b/src/Types/TypeCode39Extended.php index d16cfd67..0d850433 100644 --- a/src/Types/TypeCode39Extended.php +++ b/src/Types/TypeCode39Extended.php @@ -9,6 +9,6 @@ class TypeCode39Extended extends TypeCode39 { - protected $extended = true; - protected $checksum = false; + protected bool $extended = true; + protected bool $checksum = false; } diff --git a/src/Types/TypeCode39ExtendedChecksum.php b/src/Types/TypeCode39ExtendedChecksum.php index 1d28a885..9e5292cd 100644 --- a/src/Types/TypeCode39ExtendedChecksum.php +++ b/src/Types/TypeCode39ExtendedChecksum.php @@ -9,6 +9,6 @@ class TypeCode39ExtendedChecksum extends TypeCode39 { - protected $extended = true; - protected $checksum = true; + protected bool $extended = true; + protected bool $checksum = true; } diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index bca30be4..5ff4a678 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -15,7 +15,7 @@ class TypeCode93 implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ 48 => '131112', // 0 49 => '111213', // 1 50 => '111312', // 2 @@ -66,7 +66,7 @@ class TypeCode93 implements TypeInterface 42 => '111141', // start-stop ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $encode = [ chr(0) => 'bU', @@ -246,42 +246,40 @@ public function getBarcodeData(string $code): Barcode * @return string checksum code. * @protected */ - protected function checksum_code93($code) + protected function checksum_code93(string $code): string { $chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', 'a', 'b', 'c', 'd']; // calculate check digit C - $len = strlen($code); - $p = 1; - $check = 0; - for ($i = ($len - 1); $i >= 0; --$i) { - $k = array_keys($chars, $code[$i]); - $check += ($k[0] * $p); - ++$p; - if ($p > 20) { - $p = 1; + $codeLength = strlen($code); + $weight = 1; + $checksum = 0; + for ($i = ($codeLength - 1); $i >= 0; --$i) { + $charIndex = array_keys($chars, $code[$i]); + $checksum += ($charIndex[0] * $weight); + ++$weight; + if ($weight > 20) { + $weight = 1; } } - $check %= 47; - $c = $chars[$check]; - $code .= $c; + $checksumC = $checksum % 47; + $checkDigitC = $chars[$checksumC]; + $codeWithC = $code . $checkDigitC; // calculate check digit K - $p = 1; - $check = 0; - for ($i = $len; $i >= 0; --$i) { - $k = array_keys($chars, $code[$i]); - $check += ($k[0] * $p); - ++$p; - if ($p > 15) { - $p = 1; + $weight = 1; + $checksum = 0; + for ($i = $codeLength; $i >= 0; --$i) { + $charIndex = array_keys($chars, $codeWithC[$i]); + $checksum += ($charIndex[0] * $weight); + ++$weight; + if ($weight > 15) { + $weight = 1; } } - $check %= 47; - $k = $chars[$check]; + $checksumK = $checksum % 47; + $checkDigitK = $chars[$checksumK]; - $checksum = $c . $k; - - return $checksum; + return $checkDigitC . $checkDigitK; } } diff --git a/src/Types/TypeEan13.php b/src/Types/TypeEan13.php index 194d3eee..3f2fe75d 100644 --- a/src/Types/TypeEan13.php +++ b/src/Types/TypeEan13.php @@ -14,7 +14,7 @@ class TypeEan13 extends TypeEanUpcBase { - protected $length = 13; - protected $upca = false; - protected $upce = false; + protected int $length = 13; + protected bool $upca = false; + protected bool $upce = false; } diff --git a/src/Types/TypeEan8.php b/src/Types/TypeEan8.php index 60207618..e605e34c 100644 --- a/src/Types/TypeEan8.php +++ b/src/Types/TypeEan8.php @@ -14,7 +14,7 @@ class TypeEan8 extends TypeEanUpcBase { - protected $length = 8; - protected $upca = false; - protected $upce = false; + protected int $length = 8; + protected bool $upca = false; + protected bool $upce = false; } diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index 2ad218e2..fba01214 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -20,11 +20,11 @@ abstract class TypeEanUpcBase implements TypeInterface { - protected $length = 13; - protected $upca = false; - protected $upce = false; + protected int $length = 13; + protected bool $upca = false; + protected bool $upce = false; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); @@ -206,12 +206,12 @@ public function getBarcodeData(string $code): Barcode return $barcode; } - protected function calculateChecksumDigit(string $code) + protected function calculateChecksumDigit(string $code): int { // calculate check digit $sum_a = 0; for ($i = 1; $i < $this->length - 1; $i += 2) { - $sum_a += $code[$i]; + $sum_a += intval($code[$i]); } if ($this->length > 12) { $sum_a *= 3; diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index d2f87bb1..d0fedc44 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -15,7 +15,7 @@ class TypeITF14 implements TypeInterface * @throws InvalidCharacterException * @throws InvalidCheckDigitException */ - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr = []; $chr['0'] = '11221'; @@ -66,7 +66,7 @@ public function getBarcodeData(string $code): Barcode } foreach (str_split($pmixed) as $width) { - $barcode->addBar(new BarcodeBar($width, 1, $drawBar)); + $barcode->addBar(new BarcodeBar(intval($width), 1, $drawBar)); $drawBar = ! $drawBar; } } diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index e574cf7f..9160e142 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -34,7 +34,7 @@ class TypeIntelligentMailBarcode implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $asc_chr = [ 4, @@ -333,9 +333,9 @@ public function getBarcodeData(string $code): Barcode throw new BarcodeException('Routing code unknown'); } - $binary_code = bcmul($binary_code, 10); + $binary_code = bcmul($binary_code, strval(10)); $binary_code = bcadd($binary_code, $tracking_number[0]); - $binary_code = bcmul($binary_code, 5); + $binary_code = bcmul($binary_code, strval(5)); $binary_code = bcadd($binary_code, $tracking_number[1]); $binary_code .= substr($tracking_number, 2, 18); @@ -360,11 +360,11 @@ public function getBarcodeData(string $code): Barcode // convert binary data to codewords $codewords = []; $data = $this->hex_to_dec($binary_code_102bit); - $codewords[0] = bcmod($data, 636) * 2; - $data = bcdiv($data, 636); + $codewords[0] = bcmod($data, strval(636)) * 2; + $data = bcdiv($data, strval(636)); for ($i = 1; $i < 9; ++$i) { - $codewords[$i] = bcmod($data, 1365); - $data = bcdiv($data, 1365); + $codewords[$i] = bcmod($data, strval(1365)); + $data = bcdiv($data, strval(1365)); } $codewords[9] = $data; if (($fcs >> 10) == 1) { @@ -440,7 +440,7 @@ protected function dec_to_hex($number) $hex = []; while ($number > 0) { - array_push($hex, strtoupper(dechex(bcmod($number, '16')))); + $hex[] = strtoupper(dechex(intval(bcmod(strval($number), '16')))); $number = bcdiv($number, '16', 0); } $hex = array_reverse($hex); @@ -501,8 +501,8 @@ protected function hex_to_dec($hex) $bitval = 1; $len = strlen($hex); for ($pos = ($len - 1); $pos >= 0; --$pos) { - $dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval)); - $bitval = bcmul($bitval, 16); + $dec = bcadd($dec, bcmul(strval(hexdec($hex[$pos])), strval($bitval))); + $bitval = bcmul($bitval, '16'); } return $dec; diff --git a/src/Types/TypeInterface.php b/src/Types/TypeInterface.php index f380c38b..1fa946db 100644 --- a/src/Types/TypeInterface.php +++ b/src/Types/TypeInterface.php @@ -6,5 +6,5 @@ interface TypeInterface { - public function getBarcodeData(string $code): Barcode; + public function getBarcode(string $code): Barcode; } diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 61921266..10727e6e 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -14,7 +14,7 @@ class TypeInterleaved25Checksum implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr = []; $chr['0'] = '11221'; @@ -62,7 +62,7 @@ public function getBarcodeData(string $code): Barcode } else { $t = false; // space } - $w = $seq[$j]; + $w = intval($seq[$j]); $barcode->addBar(new BarcodeBar($w, 1, $t)); } } @@ -75,11 +75,11 @@ protected function getChecksum(string $code): string $len = strlen($code); $sum = 0; for ($i = 0; $i < $len; $i += 2) { - $sum += $code[$i]; + $sum += intval($code[$i]); } $sum *= 3; for ($i = 1; $i < $len; $i += 2) { - $sum += ($code[$i]); + $sum += intval($code[$i]); } $r = $sum % 10; if ($r > 0) { diff --git a/src/Types/TypeKix.php b/src/Types/TypeKix.php index 08595a56..de20cf8b 100644 --- a/src/Types/TypeKix.php +++ b/src/Types/TypeKix.php @@ -12,5 +12,5 @@ class TypeKix extends TypeRms4cc { - protected $kix = true; + protected bool $kix = true; } diff --git a/src/Types/TypeMsi.php b/src/Types/TypeMsi.php index d6e91cbc..12f6ee66 100644 --- a/src/Types/TypeMsi.php +++ b/src/Types/TypeMsi.php @@ -13,5 +13,5 @@ class TypeMsi extends TypeMsiChecksum { - protected $checksum = false; + protected bool $checksum = false; } diff --git a/src/Types/TypeMsiChecksum.php b/src/Types/TypeMsiChecksum.php index cb3a0ff0..3d06f07d 100644 --- a/src/Types/TypeMsiChecksum.php +++ b/src/Types/TypeMsiChecksum.php @@ -17,9 +17,9 @@ class TypeMsiChecksum implements TypeInterface { - protected $checksum = true; + protected bool $checksum = true; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr['0'] = '100100100100'; $chr['1'] = '100100100110'; diff --git a/src/Types/TypePharmacode.php b/src/Types/TypePharmacode.php index 5f1f128f..9054967f 100644 --- a/src/Types/TypePharmacode.php +++ b/src/Types/TypePharmacode.php @@ -12,8 +12,10 @@ class TypePharmacode implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { + $originalCode = $code; + $code = intval($code); $seq = ''; @@ -31,6 +33,6 @@ public function getBarcodeData(string $code): Barcode $seq = substr($seq, 0, -2); $seq = strrev($seq); - return BinarySequenceConverter::convert($code, $seq); + return BinarySequenceConverter::convert($originalCode, $seq); } } diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index 3d7c2fad..f71d2a16 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -9,14 +9,15 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; -use Picqer\Barcode\Exceptions\BarcodeException; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; class TypePharmacodeTwoCode implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { + $originalCode = $code; + $code = intval($code); if ($code < 1) { @@ -46,7 +47,7 @@ public function getBarcodeData(string $code): Barcode $seq = strrev($seq); - $barcode = new Barcode($code); + $barcode = new Barcode($originalCode); for ($i = 0; $i < strlen($seq); ++$i) { switch ($seq[$i]) { @@ -69,9 +70,9 @@ public function getBarcodeData(string $code): Barcode throw new InvalidCharacterException('Could not find bar for char.'); } - $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); + $barcode->addBar(new BarcodeBar(1, $h, true, $p)); if ($i < (strlen($seq) - 1)) { - $barcode->addBar(new BarcodeBar(1, 2, 0, 0)); + $barcode->addBar(new BarcodeBar(1, 2, false, 0)); } } diff --git a/src/Types/TypePlanet.php b/src/Types/TypePlanet.php index 07523869..c3b7f5ed 100644 --- a/src/Types/TypePlanet.php +++ b/src/Types/TypePlanet.php @@ -13,7 +13,7 @@ class TypePlanet extends TypePostnet { - protected $barlen = [ + protected array $barlen = [ 0 => [1, 1, 2, 2, 2], 1 => [2, 2, 2, 1, 1], 2 => [2, 2, 1, 2, 1], diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php index 1f99ae2c..0d235d86 100644 --- a/src/Types/TypePostnet.php +++ b/src/Types/TypePostnet.php @@ -16,7 +16,7 @@ class TypePostnet implements TypeInterface { - protected $barlen = [ + protected array $barlen = [ 0 => [2, 2, 1, 1, 1], 1 => [1, 1, 1, 2, 2], 2 => [1, 1, 2, 1, 2], @@ -29,7 +29,7 @@ class TypePostnet implements TypeInterface 9 => [2, 1, 2, 1, 1] ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $code = str_replace(['-', ' '], '', $code); $len = strlen($code); @@ -49,20 +49,20 @@ public function getBarcodeData(string $code): Barcode $len = strlen($code); // start bar - $barcode->addBar(new BarcodeBar(1, 2, 1)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $barcode->addBar(new BarcodeBar(1, 2, true)); + $barcode->addBar(new BarcodeBar(1, 2, false)); for ($i = 0; $i < $len; ++$i) { for ($j = 0; $j < 5; ++$j) { $h = $this->barlen[$code[$i]][$j]; - $p = floor(1 / $h); - $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $p = (int)floor(1 / $h); + $barcode->addBar(new BarcodeBar(1, (int)$h, true, $p)); + $barcode->addBar(new BarcodeBar(1, 2, false)); } } // end bar - $barcode->addBar(new BarcodeBar(1, 2, 1)); + $barcode->addBar(new BarcodeBar(1, 2, true)); return $barcode; } diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php index c728bcbf..684a6248 100644 --- a/src/Types/TypeRms4cc.php +++ b/src/Types/TypeRms4cc.php @@ -15,9 +15,9 @@ class TypeRms4cc implements TypeInterface { - protected $kix = false; + protected bool $kix = false; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { // bar mode // 1 = pos 1, length 2 @@ -122,8 +122,8 @@ public function getBarcodeData(string $code): Barcode ++$len; // start bar - $barcode->addBar(new BarcodeBar(1, 2, 1)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $barcode->addBar(new BarcodeBar(1, 2, true)); + $barcode->addBar(new BarcodeBar(1, 2, false)); } for ($i = 0; $i < $len; ++$i) { @@ -150,14 +150,14 @@ public function getBarcodeData(string $code): Barcode break; } - $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $barcode->addBar(new BarcodeBar(1, $h, true, $p)); + $barcode->addBar(new BarcodeBar(1, 2, false)); } } if (! $this->kix) { // stop bar - $barcode->addBar(new BarcodeBar(1, 3, 1)); + $barcode->addBar(new BarcodeBar(1, 3, true)); } return $barcode; diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php index 592f59ae..1445a3ca 100644 --- a/src/Types/TypeStandard2of5.php +++ b/src/Types/TypeStandard2of5.php @@ -14,9 +14,9 @@ class TypeStandard2of5 implements TypeInterface { - protected $checksum = false; + protected bool $checksum = false; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr['0'] = '10101110111010'; $chr['1'] = '11101010101110'; @@ -50,25 +50,25 @@ public function getBarcodeData(string $code): Barcode * Checksum for standard 2 of 5 barcodes. * * @param $code (string) code to process. - * @return int checksum. + * @return string checksum. * @protected */ - protected function checksum_s25($code) + protected function checksum_s25(string $code): string { $len = strlen($code); $sum = 0; for ($i = 0; $i < $len; $i += 2) { - $sum += $code[$i]; + $sum += intval($code[$i]); } $sum *= 3; for ($i = 1; $i < $len; $i += 2) { - $sum += ($code[$i]); + $sum += intval($code[$i]); } $r = $sum % 10; if ($r > 0) { $r = (10 - $r); } - return $r; + return strval($r); } } diff --git a/src/Types/TypeStandard2of5Checksum.php b/src/Types/TypeStandard2of5Checksum.php index 25bc4a0e..c1b6fc52 100644 --- a/src/Types/TypeStandard2of5Checksum.php +++ b/src/Types/TypeStandard2of5Checksum.php @@ -10,5 +10,5 @@ class TypeStandard2of5Checksum extends TypeStandard2of5 { - protected $checksum = true; + protected bool $checksum = true; } diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php index bf431705..477d4b93 100644 --- a/src/Types/TypeTelepen.php +++ b/src/Types/TypeTelepen.php @@ -23,11 +23,12 @@ class TypeTelepen implements TypeInterface { private const TELEPEN_START_CHAR = '_'; private const TELEPEN_STOP_CHAR = 'z'; + private const TELEPEN_ALPHA = 'alpha'; private const TELEPEN_NUMERIC = 'numeric'; - private $telepen_lookup_table; - private $mode; + private array $telepen_lookup_table; + private string $mode; public function __construct($m = 'alpha') { @@ -38,7 +39,7 @@ public function __construct($m = 'alpha') $this->createTelepenConversionTable(); } - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { /* The stream we get from the telepen output gives us the * width of alternating black/white stripes @@ -50,7 +51,7 @@ public function getBarcodeData(string $code): Barcode $drawBar = true; for ($i = 0; $i < strlen($encoded); ++$i) { $barWidth = $encoded[$i]; - $barcode->addBar(new BarcodeBar($barWidth, 250, $drawBar)); + $barcode->addBar(new BarcodeBar(intval($barWidth), 250, $drawBar)); $drawBar = !$drawBar; //flip to other colour } @@ -59,7 +60,6 @@ public function getBarcodeData(string $code): Barcode protected function encode($code) : string { - $result = null; if ($this->mode == self::TELEPEN_ALPHA) { $result = $this->encodeAlpha($code); } else { @@ -101,7 +101,7 @@ protected function encodeAlpha($code) : string $check_digit = 0; } - $dest .= $this->telepen_lookup_table[ord($check_digit)]; + $dest .= $this->telepen_lookup_table[ord(strval($check_digit))]; $dest .= $this->telepen_lookup_table[ord(self::TELEPEN_STOP_CHAR)]; // Stop return $dest; @@ -160,7 +160,7 @@ private function encodeNumeric(string $code) : string * More information about Telepen symbology is available from * https://v4l237.n3cdn1.secureserver.net/wp-content/uploads/2022/05/Barcode-Symbology-information-and-History.pdf */ - private function createTelepenConversionTable() + private function createTelepenConversionTable(): void { $this->telepen_lookup_table = [ "1111111111111111", "1131313111", "33313111", "1111313131", diff --git a/src/Types/TypeTelepenNumeric.php b/src/Types/TypeTelepenNumeric.php new file mode 100644 index 00000000..519d077e --- /dev/null +++ b/src/Types/TypeTelepenNumeric.php @@ -0,0 +1,24 @@ + + * from Java implementation of Telepen by Robin Stuart + * at https://github.com/woo-j/OkapiBarcode which uses the + * Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + * + * Implements Telepen (also known as Telepen Alpha), and Telepen Numeric. + * + * Telepen can encode ASCII text input and includes a modulo-127 check digit. + * Telepen Numeric allows compression of numeric data into a Telepen symbol. Data + * can consist of pairs of numbers or pairs consisting of a numerical digit followed + * by an X character. Telepen Numeric also includes a mod-127 check digit. + */ + +namespace Picqer\Barcode\Types; + +class TypeTelepenNumeric extends TypeTelepen +{ + public function __construct() + { + parent::__construct('numeric'); + } +} diff --git a/src/Types/TypeUpcA.php b/src/Types/TypeUpcA.php index 11d45b96..11c21a8c 100644 --- a/src/Types/TypeUpcA.php +++ b/src/Types/TypeUpcA.php @@ -14,7 +14,7 @@ class TypeUpcA extends TypeEanUpcBase { - protected $length = 12; - protected $upca = true; - protected $upce = false; + protected int $length = 12; + protected bool $upca = true; + protected bool $upce = false; } diff --git a/src/Types/TypeUpcE.php b/src/Types/TypeUpcE.php index 4fd8a7e9..95ada117 100644 --- a/src/Types/TypeUpcE.php +++ b/src/Types/TypeUpcE.php @@ -14,7 +14,7 @@ class TypeUpcE extends TypeEanUpcBase { - protected $length = 12; - protected $upca = false; - protected $upce = true; + protected int $length = 12; + protected bool $upca = false; + protected bool $upce = true; } diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index f8416f0b..ef2c9a0f 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -14,9 +14,9 @@ class TypeUpcExtension2 implements TypeInterface { - protected $length = 2; + protected int $length = 2; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $len = $this->length; diff --git a/src/Types/TypeUpcExtension5.php b/src/Types/TypeUpcExtension5.php index b1d34248..c9e3065a 100644 --- a/src/Types/TypeUpcExtension5.php +++ b/src/Types/TypeUpcExtension5.php @@ -10,5 +10,5 @@ class TypeUpcExtension5 extends TypeUpcExtension2 { - protected $length = 5; + protected int $length = 5; } diff --git a/tests/ChecksumBarcodeTest.php b/tests/ChecksumBarcodeTest.php index eff35ab7..c9daf2d2 100644 --- a/tests/ChecksumBarcodeTest.php +++ b/tests/ChecksumBarcodeTest.php @@ -30,7 +30,7 @@ public function testAllValidBarcodeTypes() $barcodeType = $this->getBarcodeType($barcodeTestSet['type']); foreach ($barcodeTestSet['barcodes'] as $testBarcode => $validBarcode) { - $this->assertEquals($validBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode()); + $this->assertEquals($validBarcode, $barcodeType->getBarcode($testBarcode)->getBarcode()); } } } @@ -42,7 +42,7 @@ public function testAllInvalidBarcodeTypes() foreach ($barcodeTestSet['barcodes'] as $invalidBarcode) { try { - $barcodeType->getBarcodeData($invalidBarcode)->getBarcode(); + $barcodeType->getBarcode($invalidBarcode)->getBarcode(); } catch (BarcodeException $exception) { $this->assertInstanceOf(InvalidCheckDigitException::class, $exception); continue; @@ -53,7 +53,6 @@ public function testAllInvalidBarcodeTypes() } } - private function getBarcodeType(string $typeClass): TypeInterface { return new $typeClass; diff --git a/tests/DynamicHtmlRendererTest.php b/tests/DynamicHtmlRendererTest.php new file mode 100644 index 00000000..9597874d --- /dev/null +++ b/tests/DynamicHtmlRendererTest.php @@ -0,0 +1,26 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); + $generated = $renderer->render($barcode); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-dynamic-code128.html', $generated); + } + + public function test_dynamic_html_barcode_generator_can_generate_imb_barcode_to_test_heights() + { + $barcode = (new Picqer\Barcode\Types\TypeIntelligentMailBarcode())->getBarcode('12345678903'); + + $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); + $generated = $renderer->render($barcode); + + $this->assertStringEqualsFile('tests/verified-files/12345678903-dynamic-imb.html', $generated); + } +} diff --git a/tests/HtmlRendererTest.php b/tests/HtmlRendererTest.php new file mode 100644 index 00000000..803d2c00 --- /dev/null +++ b/tests/HtmlRendererTest.php @@ -0,0 +1,26 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-code128.html', $generated); + } + + public function test_html_barcode_generator_can_generate_imb_barcode_to_test_heights() + { + $barcode = (new Picqer\Barcode\Types\TypeIntelligentMailBarcode())->getBarcode('12345678903'); + + $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); + + $this->assertStringEqualsFile('tests/verified-files/12345678903-imb.html', $generated); + } +} diff --git a/tests/JpgRendererTest.php b/tests/JpgRendererTest.php new file mode 100644 index 00000000..46aa939d --- /dev/null +++ b/tests/JpgRendererTest.php @@ -0,0 +1,152 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + + public function test_jpg_barcode_generator_can_generate_code_39_barcode() + { + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_height() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_width_factor() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + + // Copied as Imagick + + public function test_jpg_barcode_generator_can_generate_code_128_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_generate_code_39_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_height_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_width_factor_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } +} diff --git a/tests/PharmacodeTest.php b/tests/PharmacodeTest.php index c068ffb6..e2f4ed5d 100644 --- a/tests/PharmacodeTest.php +++ b/tests/PharmacodeTest.php @@ -10,6 +10,6 @@ public function test_validation_triggerd_when_generating_zero_code() $this->expectException(Picqer\Barcode\Exceptions\InvalidLengthException::class); - $pharmacode->getBarcodeData('0'); + $pharmacode->getBarcode('0'); } } diff --git a/tests/PngRendererTest.php b/tests/PngRendererTest.php new file mode 100644 index 00000000..db253ce2 --- /dev/null +++ b/tests/PngRendererTest.php @@ -0,0 +1,142 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode); + + $this->assertEquals('PNG', substr($generated, 1, 3)); + } + + + public function test_png_barcode_generator_can_generate_code_39_barcode() + { + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_height() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_width_factor() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/png', $imageInfo['mime']); + } + + + // Copied as Imagick + + public function test_png_barcode_generator_can_generate_code_128_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode); + + $this->assertEquals('PNG', substr($generated, 1, 3)); + } + + public function test_png_barcode_generator_can_generate_code_39_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_height_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_width_factor_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/png', $imageInfo['mime']); + } +} diff --git a/tests/SvgRendererTest.php b/tests/SvgRendererTest.php new file mode 100644 index 00000000..aa1e97f7 --- /dev/null +++ b/tests/SvgRendererTest.php @@ -0,0 +1,59 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $generated = $renderer->render($barcode, 190); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); + } + + public function test_svg_barcode_generator_can_generate_ean_13_barcode_with_fractional_width() + { + $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $generated = $renderer->render($barcode, 23.75, 25.75); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-fractional-width.svg', $generated); + } + + public function test_svg_barcode_generator_as_standalone() + { + $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setSvgType(\Picqer\Barcode\Renderers\SvgRenderer::TYPE_SVG_STANDALONE); + $generated = $renderer->render($barcode); + + $this->assertStringStartsWith('assertStringContainsString('getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setSvgType(\Picqer\Barcode\Renderers\SvgRenderer::TYPE_SVG_INLINE); + $generated = $renderer->render($barcode); + + $this->assertStringStartsWith('assertStringNotContainsString('expectException(\Picqer\Barcode\Exceptions\InvalidOptionException::class); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setSvgType('other'); + } +} diff --git a/tests/TypesTest.php b/tests/TypesTest.php index aaca1add..2c6ef81e 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -1,31 +1,38 @@ getBarcode('1234567890ABC', $generator::TYPE_CODE_39); + $barcode = (new TypeCode39())->getBarcode('1234567890ABC'); + $renderer = new SvgRenderer(); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); - $this->assertStringEqualsFile('tests/verified-files/C39-1234567890ABC.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode39-1234567890ABC.svg', $result); } public function test_generator_can_generate_code_39_checksum_barcode() { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39_CHECKSUM); + $barcode = (new TypeCode39Checksum())->getBarcode('1234567890ABC'); + $renderer = new SvgRenderer(); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertGreaterThan(100, strlen($result)); } public function test_generator_can_generate_code_39_extended_barcode() { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39E); + $barcode = (new TypeCode39Extended())->getBarcode('1234567890abcABC'); + $renderer = new SvgRenderer(); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); - $this->assertStringEqualsFile('tests/verified-files/C39E-1234567890abcABC.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode39Extended-1234567890abcABC.svg', $result); } public function test_generator_can_generate_code_39_extended_checksum_barcode() @@ -81,7 +88,7 @@ public function test_generator_can_generate_code_128_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128); - $this->assertStringEqualsFile('tests/verified-files/C128-1234567890abcABC-283-33.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg', $result); } public function test_generator_can_generate_code_128_a_barcode() @@ -89,7 +96,7 @@ public function test_generator_can_generate_code_128_a_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890', $generator::TYPE_CODE_128_A); - $this->assertStringEqualsFile('tests/verified-files/C128A-1234567890.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode128A-1234567890.svg', $result); } public function test_generator_can_generate_code_128_b_barcode() @@ -97,7 +104,7 @@ public function test_generator_can_generate_code_128_b_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128_B); - $this->assertStringEqualsFile('tests/verified-files/C128B-1234567890abcABC-283-33.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg', $result); } public function test_generator_can_generate_ean_2_barcode() @@ -251,4 +258,4 @@ public function test_generator_can_generate_telepen_numeric_barcode() $this->assertGreaterThan(100, strlen($result)); } -} \ No newline at end of file +} diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index bc7f9f48..0e0267e4 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -1,7 +1,7 @@ BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890ABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890ABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39E, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_STANDARD_2_5_CHECKSUM, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], - ['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012', '05400141288766']], - ['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], - ['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], - ['type' => BarcodeGenerator::TYPE_EAN_2, 'barcodes' => ['22']], - ['type' => BarcodeGenerator::TYPE_EAN_5, 'barcodes' => ['1234567890abcABC-283*33']], - ['type' => BarcodeGenerator::TYPE_EAN_8, 'barcodes' => ['1234568']], - ['type' => BarcodeGenerator::TYPE_UPC_A, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_UPC_E, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_MSI, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_MSI_CHECKSUM, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_POSTNET, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_PLANET, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_RMS4CC, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_KIX, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_IMB, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_CODABAR, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_CODE_11, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_PHARMA_CODE, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_PHARMA_CODE_TWO_TRACKS, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_TELEPEN_ALPHA, 'barcodes' => ['1234567890ASCD']], - ['type' => BarcodeGenerator::TYPE_TELEPEN_NUMERIC, 'barcodes' => ['1234567890']] + public static array $supportedBarcodes = [ + ['type' => \Picqer\Barcode\Types\TypeCode39::class, 'barcodes' => ['1234567890ABC']], + ['type' => \Picqer\Barcode\Types\TypeCode39Checksum::class, 'barcodes' => ['1234567890ABC']], + ['type' => \Picqer\Barcode\Types\TypeCode39Extended::class, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\Types\TypeCode39ExtendedChecksum::class, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\Types\TypeCode93::class, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\Types\TypeStandard2of5::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeStandard2of5Checksum::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeInterleaved25::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeInterleaved25Checksum::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeEan13::class, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], + ['type' => \Picqer\Barcode\Types\TypeITF14::class, 'barcodes' => ['00012345600012', '05400141288766']], + ['type' => \Picqer\Barcode\Types\TypeCode128::class, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\Types\TypeCode128A::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeCode128B::class, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\Types\TypeUpcExtension2::class, 'barcodes' => ['22']], + ['type' => \Picqer\Barcode\Types\TypeUpcExtension5::class, 'barcodes' => ['1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\Types\TypeEan8::class, 'barcodes' => ['1234568']], + ['type' => \Picqer\Barcode\Types\TypeUpcA::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeUpcE::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeMsi::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeMsiChecksum::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePostnet::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePlanet::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeRms4cc::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeKix::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeIntelligentMailBarcode::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeCodabar::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeCode11::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePharmacode::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePharmacodeTwoCode::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeTelepen::class, 'barcodes' => ['1234567890ASCD']], + ['type' => \Picqer\Barcode\Types\TypeTelepenNumeric::class, 'barcodes' => ['1234567890']] ]; public function testAllSupportedBarcodeTypes() { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); foreach ($this::$supportedBarcodes as $barcodeTestSet) { - foreach ($barcodeTestSet['barcodes'] as $barcode) { - $result = $generator->getBarcode($barcode, $barcodeTestSet['type']); + foreach ($barcodeTestSet['barcodes'] as $barcodeText) { + $barcode = (new $barcodeTestSet['type']())->getBarcode($barcodeText); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertStringEqualsFile( - sprintf('tests/verified-files/%s.svg', $this->getSaveFilename($barcodeTestSet['type'] . '-' . $barcode)), + sprintf('tests/verified-files/%s.svg', StringHelpers::getSafeFilenameFrom($barcodeTestSet['type'] . '-' . $barcodeText)), $result, - sprintf('%s x %s dynamic test failed', $barcodeTestSet['type'], $barcode) + sprintf('%s x %s dynamic test failed', $barcodeTestSet['type'], $barcodeText) ); } } } - - protected function getSaveFilename($value) - { - return preg_replace('/[^a-zA-Z0-9_ \-+]/s', '-', $value); - } } diff --git a/tests/verified-files/CODABAR-123456789.svg b/tests/verified-files/TypeCodabar-123456789.svg similarity index 100% rename from tests/verified-files/CODABAR-123456789.svg rename to tests/verified-files/TypeCodabar-123456789.svg diff --git a/tests/verified-files/CODE11-123456789.svg b/tests/verified-files/TypeCode11-123456789.svg similarity index 100% rename from tests/verified-files/CODE11-123456789.svg rename to tests/verified-files/TypeCode11-123456789.svg diff --git a/tests/verified-files/C128-081231723897.svg b/tests/verified-files/TypeCode128-081231723897.svg similarity index 100% rename from tests/verified-files/C128-081231723897.svg rename to tests/verified-files/TypeCode128-081231723897.svg diff --git a/tests/verified-files/C128-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/C128-1234567890abcABC-283-33.svg rename to tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg diff --git a/tests/verified-files/C128A-1234567890.svg b/tests/verified-files/TypeCode128A-1234567890.svg similarity index 100% rename from tests/verified-files/C128A-1234567890.svg rename to tests/verified-files/TypeCode128A-1234567890.svg diff --git a/tests/verified-files/C128B-081231723897.svg b/tests/verified-files/TypeCode128B-081231723897.svg similarity index 100% rename from tests/verified-files/C128B-081231723897.svg rename to tests/verified-files/TypeCode128B-081231723897.svg diff --git a/tests/verified-files/C128B-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/C128B-1234567890abcABC-283-33.svg rename to tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg diff --git a/tests/verified-files/C39-1234567890ABC.svg b/tests/verified-files/TypeCode39-1234567890ABC.svg similarity index 100% rename from tests/verified-files/C39-1234567890ABC.svg rename to tests/verified-files/TypeCode39-1234567890ABC.svg diff --git a/tests/verified-files/C39+-1234567890ABC.svg b/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg similarity index 100% rename from tests/verified-files/C39+-1234567890ABC.svg rename to tests/verified-files/TypeCode39Checksum-1234567890ABC.svg diff --git a/tests/verified-files/C39E-1234567890abcABC.svg b/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C39E-1234567890abcABC.svg rename to tests/verified-files/TypeCode39Extended-1234567890abcABC.svg diff --git a/tests/verified-files/C39E+-1234567890abcABC.svg b/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C39E+-1234567890abcABC.svg rename to tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg diff --git a/tests/verified-files/C93-1234567890abcABC.svg b/tests/verified-files/TypeCode93-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C93-1234567890abcABC.svg rename to tests/verified-files/TypeCode93-1234567890abcABC.svg diff --git a/tests/verified-files/EAN13-004900000463.svg b/tests/verified-files/TypeEan13-004900000463.svg similarity index 100% rename from tests/verified-files/EAN13-004900000463.svg rename to tests/verified-files/TypeEan13-004900000463.svg diff --git a/tests/verified-files/EAN13-0049000004632.svg b/tests/verified-files/TypeEan13-0049000004632.svg similarity index 100% rename from tests/verified-files/EAN13-0049000004632.svg rename to tests/verified-files/TypeEan13-0049000004632.svg diff --git a/tests/verified-files/EAN13-081231723897.svg b/tests/verified-files/TypeEan13-081231723897.svg similarity index 100% rename from tests/verified-files/EAN13-081231723897.svg rename to tests/verified-files/TypeEan13-081231723897.svg diff --git a/tests/verified-files/EAN8-1234568.svg b/tests/verified-files/TypeEan8-1234568.svg similarity index 100% rename from tests/verified-files/EAN8-1234568.svg rename to tests/verified-files/TypeEan8-1234568.svg diff --git a/tests/verified-files/ITF14-00012345600012.svg b/tests/verified-files/TypeITF14-00012345600012.svg similarity index 100% rename from tests/verified-files/ITF14-00012345600012.svg rename to tests/verified-files/TypeITF14-00012345600012.svg diff --git a/tests/verified-files/ITF14-05400141288766.svg b/tests/verified-files/TypeITF14-05400141288766.svg similarity index 100% rename from tests/verified-files/ITF14-05400141288766.svg rename to tests/verified-files/TypeITF14-05400141288766.svg diff --git a/tests/verified-files/IMB-123456789.svg b/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg similarity index 100% rename from tests/verified-files/IMB-123456789.svg rename to tests/verified-files/TypeIntelligentMailBarcode-123456789.svg diff --git a/tests/verified-files/I25-1234567890.svg b/tests/verified-files/TypeInterleaved25-1234567890.svg similarity index 100% rename from tests/verified-files/I25-1234567890.svg rename to tests/verified-files/TypeInterleaved25-1234567890.svg diff --git a/tests/verified-files/I25+-1234567890.svg b/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg similarity index 100% rename from tests/verified-files/I25+-1234567890.svg rename to tests/verified-files/TypeInterleaved25Checksum-1234567890.svg diff --git a/tests/verified-files/KIX-123456789.svg b/tests/verified-files/TypeKix-123456789.svg similarity index 100% rename from tests/verified-files/KIX-123456789.svg rename to tests/verified-files/TypeKix-123456789.svg diff --git a/tests/verified-files/MSI-123456789.svg b/tests/verified-files/TypeMsi-123456789.svg similarity index 100% rename from tests/verified-files/MSI-123456789.svg rename to tests/verified-files/TypeMsi-123456789.svg diff --git a/tests/verified-files/MSI+-123456789.svg b/tests/verified-files/TypeMsiChecksum-123456789.svg similarity index 100% rename from tests/verified-files/MSI+-123456789.svg rename to tests/verified-files/TypeMsiChecksum-123456789.svg diff --git a/tests/verified-files/PHARMA-123456789.svg b/tests/verified-files/TypePharmacode-123456789.svg similarity index 98% rename from tests/verified-files/PHARMA-123456789.svg rename to tests/verified-files/TypePharmacode-123456789.svg index f3561bb0..46044b0a 100644 --- a/tests/verified-files/PHARMA-123456789.svg +++ b/tests/verified-files/TypePharmacode-123456789.svg @@ -1,7 +1,7 @@ - 0 + 123456789 diff --git a/tests/verified-files/PHARMA2T-123456789.svg b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg similarity index 97% rename from tests/verified-files/PHARMA2T-123456789.svg rename to tests/verified-files/TypePharmacodeTwoCode-123456789.svg index 6f9368ff..39975d48 100644 --- a/tests/verified-files/PHARMA2T-123456789.svg +++ b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg @@ -1,7 +1,7 @@ - 0 + 123456789 diff --git a/tests/verified-files/PLANET-123456789.svg b/tests/verified-files/TypePlanet-123456789.svg similarity index 100% rename from tests/verified-files/PLANET-123456789.svg rename to tests/verified-files/TypePlanet-123456789.svg diff --git a/tests/verified-files/POSTNET-123456789.svg b/tests/verified-files/TypePostnet-123456789.svg similarity index 100% rename from tests/verified-files/POSTNET-123456789.svg rename to tests/verified-files/TypePostnet-123456789.svg diff --git a/tests/verified-files/RMS4CC-123456789.svg b/tests/verified-files/TypeRms4cc-123456789.svg similarity index 100% rename from tests/verified-files/RMS4CC-123456789.svg rename to tests/verified-files/TypeRms4cc-123456789.svg diff --git a/tests/verified-files/S25-1234567890.svg b/tests/verified-files/TypeStandard2of5-1234567890.svg similarity index 100% rename from tests/verified-files/S25-1234567890.svg rename to tests/verified-files/TypeStandard2of5-1234567890.svg diff --git a/tests/verified-files/S25+-1234567890.svg b/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg similarity index 100% rename from tests/verified-files/S25+-1234567890.svg rename to tests/verified-files/TypeStandard2of5Checksum-1234567890.svg diff --git a/tests/verified-files/TELEPENALPHA-1234567890ASCD.svg b/tests/verified-files/TypeTelepen-1234567890ASCD.svg similarity index 100% rename from tests/verified-files/TELEPENALPHA-1234567890ASCD.svg rename to tests/verified-files/TypeTelepen-1234567890ASCD.svg diff --git a/tests/verified-files/TELEPENNUMERIC-1234567890.svg b/tests/verified-files/TypeTelepenNumeric-1234567890.svg similarity index 100% rename from tests/verified-files/TELEPENNUMERIC-1234567890.svg rename to tests/verified-files/TypeTelepenNumeric-1234567890.svg diff --git a/tests/verified-files/UPCA-123456789.svg b/tests/verified-files/TypeUpcA-123456789.svg similarity index 100% rename from tests/verified-files/UPCA-123456789.svg rename to tests/verified-files/TypeUpcA-123456789.svg diff --git a/tests/verified-files/UPCE-123456789.svg b/tests/verified-files/TypeUpcE-123456789.svg similarity index 100% rename from tests/verified-files/UPCE-123456789.svg rename to tests/verified-files/TypeUpcE-123456789.svg diff --git a/tests/verified-files/EAN2-22.svg b/tests/verified-files/TypeUpcExtension2-22.svg similarity index 100% rename from tests/verified-files/EAN2-22.svg rename to tests/verified-files/TypeUpcExtension2-22.svg diff --git a/tests/verified-files/EAN5-1234567890abcABC-283-33.svg b/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/EAN5-1234567890abcABC-283-33.svg rename to tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg