Skip to content

Commit

Permalink
Merge pull request #2 from brewerwall/feature/generator-refactor
Browse files Browse the repository at this point in the history
Refactor to inject code to be generated into the generate() method.
  • Loading branch information
griffithben authored Sep 20, 2019
2 parents 326b886 + 5370dce commit c2ef18c
Show file tree
Hide file tree
Showing 35 changed files with 370 additions and 356 deletions.
23 changes: 12 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PHP Barcode Generator [![Build Status](https://travis-ci.org/brewerwall/php-barcode-generator.svg?branch=master)](https://travis-ci.org/brewerwall/php-barcode-generator) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/767119e854d7443b87a37ebc93697a42)](https://www.codacy.com/manual/Brewerwall/php-barcode-generator?utm_source=github.com&utm_medium=referral&utm_content=brewerwall/php-barcode-generator&utm_campaign=Badge_Grade)
# <img src="https://user-images.githubusercontent.com/632330/57006043-fb823800-6ba2-11e9-8fa9-eba94b8011b5.png" width="30px"/> PHP Barcode Generator<br/>[![Build Status](https://travis-ci.org/brewerwall/php-barcode-generator.svg?branch=master)](https://travis-ci.org/brewerwall/php-barcode-generator) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/767119e854d7443b87a37ebc93697a42)](https://www.codacy.com/manual/Brewerwall/php-barcode-generator?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=brewerwall/php-barcode-generator&amp;utm_campaign=Badge_Grade)
This is an easy to use, non-bloated, framework independent, barcode generator in PHP. This version has been updated to follow PSR-4 and work with PHP 7.1+.

It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards.
Expand All @@ -22,19 +22,20 @@ Using IMB Barcodes require `bcmath` extension to be installed as well.
Create a new barcode generator. This will declare the code, the type of barcode and what format the code will be rendered.

```php
$generator = new BarcodeGenerator('012345678', BarcodeType::TYPE_CODE_128, BarcodeRender::RENDER_JPG);
$generator = new BarcodeGenerator(BarcodeType::TYPE_CODE_128, BarcodeRender::RENDER_JPG);

// Generates our default barcode with width=2, height=30, color=#000000
$generated = $generator->generate();
// Generate our code
$generated = $generator->generate('012345678');

// Generates the same code with style updates
$generated = $generator->generate(4, 50, '#FFCC33');
$generated = $generator->generate('012345678', 4, 50, '#FFCC33');
```

The `$generator->generate()` method accepts the following:
- $widthFactor (default: 2) Width is based on the length of the data, with this factor you can make the barcode bars wider than default
- $totalHeight (default: 30) The total height of the barcode
- $color (default: #000000) Hex code of the foreground color
The `$generator->generate()` method accepts the following parameters:
- `$code` Barcode value we need to generate.
- `$widthFactor` (default: 2) Width is based on the length of the data, with this factor you can make the barcode bars wider than default
- `$totalHeight` (default: 30) The total height of the barcode
- `$color` (default: #000000) Hex code of the foreground color

## Image types
```php
Expand Down Expand Up @@ -86,6 +87,6 @@ BarcodeType::TYPE_PHARMA_CODE_TWO_TRACKS
Embedded PNG image in HTML:

```php
$generator = new BarcodeGenerator('012345678', BarcodeType::TYPE_CODE_128, BarcodeRender::RENDER_JPG);
echo '<img src="data:image/png;base64,' . base64_encode($generator->generate()) . '">';
$generator = new BarcodeGenerator(BarcodeType::TYPE_CODE_128, BarcodeRender::RENDER_PNG);
echo '<img src="data:image/png;base64,' . base64_encode($generator->generate('012345678')) . '">';
```
8 changes: 4 additions & 4 deletions src/Barcode/BarcodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ class BarcodeGenerator
/** @var BarcodeRenderInterface */
private $barcodeRender;

public function __construct(string $code, string $barcodeType, string $barcodeRenderType)
public function __construct(string $barcodeType, string $barcodeRenderType)
{
$barcodeTypeFactory = new BarcodeTypeFactory();
$this->barcodeType = $barcodeTypeFactory->generateBarcodeType($code, $barcodeType);
$this->barcodeType = $barcodeTypeFactory->generateBarcodeType($barcodeType);

$barcodeRenderFactory = new BarcodeRenderFactory();
$this->barcodeRender = $barcodeRenderFactory->generateBarcodeRender($barcodeRenderType);
}

public function generate(int $widthFactor = 2, int $totalHeight = 30, $color = '#000000')
public function generate(string $code, int $widthFactor = 2, int $totalHeight = 30, $color = '#000000')
{
return $this->barcodeRender->render($this->barcodeType->generate(), $widthFactor, $totalHeight, $color);
return $this->barcodeRender->render($this->barcodeType->generate($code), $widthFactor, $totalHeight, $color);
}
}
13 changes: 0 additions & 13 deletions src/Barcode/Types/BarcodeTypeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@

abstract class BarcodeTypeAbstract
{
/** @var string */
protected $code;

/**
* Constructor.
*
* @param string $code
*/
public function __construct(string $code)
{
$this->code = $code;
}

/**
* Checksum for standard 2 of 5 barcodes.
*
Expand Down
62 changes: 31 additions & 31 deletions src/Barcode/Types/BarcodeTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,127 +15,127 @@ class BarcodeTypeFactory
*
* @return BarcodeTypeInterface
*/
public function generateBarcodeType(string $code, string $type): BarcodeTypeInterface
public function generateBarcodeType(string $type): BarcodeTypeInterface
{
switch (strtoupper($type)) {
case BarcodeType::TYPE_CODE_39: // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
return new Code39($code, false, false);
return new Code39(false, false);
break;

case BarcodeType::TYPE_CODE_39_CHECKSUM: // CODE 39 with checksum
return new Code39($code, true, false);
return new Code39(true, false);
break;

case BarcodeType::TYPE_CODE_39E: // CODE 39 EXTENDED
return new Code39($code, false, true);
return new Code39(false, true);
break;

case BarcodeType::TYPE_CODE_39E_CHECKSUM: // CODE 39 EXTENDED + CHECKSUM
return new Code39($code, true, true);
return new Code39(true, true);
break;

case BarcodeType::TYPE_CODE_93: // CODE 93 - USS-93
return new Code93($code);
return new Code93();
break;

case BarcodeType::TYPE_STANDARD_2_5: // Standard 2 of 5
return new S25($code, false);
return new S25(false);
break;

case BarcodeType::TYPE_STANDARD_2_5_CHECKSUM: // Standard 2 of 5 + CHECKSUM
return new S25($code, true);
return new S25(true);
break;

case BarcodeType::TYPE_INTERLEAVED_2_5: // Interleaved 2 of 5
return new I25($code, false);
return new I25(false);
break;

case BarcodeType::TYPE_INTERLEAVED_2_5_CHECKSUM: // Interleaved 2 of 5 + CHECKSUM
return new I25($code, true);
return new I25(true);
break;

case BarcodeType::TYPE_CODE_128: // CODE 128
return new C128($code);
return new C128();
break;

case BarcodeType::TYPE_CODE_128_A: // CODE 128 A
return new C128($code, 'A');
return new C128('A');
break;

case BarcodeType::TYPE_CODE_128_B: // CODE 128 B
return new C128($code, 'B');
return new C128('B');
break;

case BarcodeType::TYPE_CODE_128_C: // CODE 128 C
return new C128($code, 'C');
return new C128('C');
break;

case BarcodeType::TYPE_EAN_2: // 2-Digits UPC-Based Extention
return new EanExt($code, 2);
return new EanExt(2);
break;

case BarcodeType::TYPE_EAN_5: // 5-Digits UPC-Based Extention
return new EanExt($code, 5);
return new EanExt(5);
break;

case BarcodeType::TYPE_EAN_8: // EAN 8
return new EanUpc($code, 8);
return new EanUpc(8);
break;

case BarcodeType::TYPE_EAN_13: // EAN 13
return new EanUpc($code, 13);
return new EanUpc(13);
break;

case BarcodeType::TYPE_UPC_A: // UPC-A
return new EanUpc($code, 12);
return new EanUpc(12);
break;

case BarcodeType::TYPE_UPC_E: // UPC-E
return new EanUpc($code, 6);
return new EanUpc(6);
break;

case BarcodeType::TYPE_MSI: // MSI (Variation of Plessey code)
return new MSI($code);
return new MSI();
break;

case BarcodeType::TYPE_MSI_CHECKSUM: // MSI + CHECKSUM (modulo 11)
return new MSI($code, true);
return new MSI(true);
break;

case BarcodeType::TYPE_POSTNET: // POSTNET
return new PostnetPlanet($code);
return new PostnetPlanet();
break;

case BarcodeType::TYPE_PLANET: // PLANET
return new PostnetPlanet($code, true);
return new PostnetPlanet(true);
break;

case BarcodeType::TYPE_RMS4CC: // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
return new RMS4CC($code);
return new RMS4CC();
break;

case BarcodeType::TYPE_KIX: // KIX (Klant index - Customer index)
return new RMS4CC($code, true);
return new RMS4CC(true);
break;

case BarcodeType::TYPE_IMB: // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
return new IMB($code);
return new IMB();
break;

case BarcodeType::TYPE_CODABAR: // CODABAR
return new Codabar($code);
return new Codabar();
break;

case BarcodeType::TYPE_CODE_11: // CODE 11
return new Code11($code);
return new Code11();
break;

case BarcodeType::TYPE_PHARMA_CODE: // PHARMACODE
return new PharmaCode($code);
return new PharmaCode();
break;

case BarcodeType::TYPE_PHARMA_CODE_TWO_TRACKS: // PHARMACODE TWO-TRACKS
return new PharmaCodeTwoTracks($code);
return new PharmaCodeTwoTracks();
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion src/Barcode/Types/BarcodeTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface BarcodeTypeInterface
{
public function generate(): array;
public function generate(string $code): array;
}
16 changes: 11 additions & 5 deletions src/Barcode/Types/C128.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

namespace Brewerwall\Barcode\Types;

use Brewerwall\Barcode\Exceptions\InvalidLengthException;
use Brewerwall\Barcode\Exceptions\InvalidCharacterException;
use Brewerwall\Barcode\Exceptions\InvalidLengthException;

class C128 extends BarcodeTypeAbstract implements BarcodeTypeInterface
{
/** @var string */
protected $codeType;

public function __construct(string $code, string $codeType = '')
public function __construct(string $codeType = '')
{
parent::__construct($code);
$this->codeType = $codeType;
}

public function generate(): array
/**
* Generatethe C128 data.
*
* @param string $code
*
* @return array
*/
public function generate(string $code): array
{
return $this->convertBarcodeArrayToNewStyle($this->barcode_c128($this->code, $this->codeType));
return $this->convertBarcodeArrayToNewStyle($this->barcode_c128($code, $this->codeType));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Barcode/Types/Codabar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Codabar extends BarcodeTypeAbstract implements BarcodeTypeInterface
/**
* Generate the Codabar data.
*
* @param string $code
*
* @return array
*/
public function generate(): array
public function generate(string $code): array
{
return $this->convertBarcodeArrayToNewStyle($this->barcode_codabar($this->code));
return $this->convertBarcodeArrayToNewStyle($this->barcode_codabar($code));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Barcode/Types/Code11.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Code11 extends BarcodeTypeAbstract implements BarcodeTypeInterface
/**
* Generate the Code11 data.
*
* @param string $code
*
* @return array
*/
public function generate(): array
public function generate(string $code): array
{
return $this->convertBarcodeArrayToNewStyle($this->barcode_code11($this->code));
return $this->convertBarcodeArrayToNewStyle($this->barcode_code11($code));
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/Barcode/Types/Code39.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ class Code39 extends BarcodeTypeAbstract implements BarcodeTypeInterface
* @param bool $hasChecksum
* @param bool $isExtended
*/
public function __construct(string $code, bool $hasChecksum, bool $isExtended)
public function __construct(bool $hasChecksum, bool $isExtended)
{
parent::__construct($code);
$this->hasChecksum = $hasChecksum;
$this->isExtended = $isExtended;
}

/**
* Generate the Code39 data.
*
* @param string $code
*
* @return array
*/
public function generate(): array
public function generate(string $code): array
{
return $this->convertBarcodeArrayToNewStyle($this->barcode_code39($this->code, $this->isExtended, $this->hasChecksum));
return $this->convertBarcodeArrayToNewStyle($this->barcode_code39($code, $this->isExtended, $this->hasChecksum));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Barcode/Types/Code93.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Code93 extends BarcodeTypeAbstract implements BarcodeTypeInterface
/**
* Generate the Code93 data.
*
* @param string $code
*
* @return array
*/
public function generate(): array
public function generate(string $code): array
{
return $this->convertBarcodeArrayToNewStyle($this->barcode_code93($this->code));
return $this->convertBarcodeArrayToNewStyle($this->barcode_code93($code));
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/Barcode/Types/EanExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ class EanExt extends BarcodeTypeAbstract implements BarcodeTypeInterface
/** @var int */
protected $length;

public function __construct(string $code, int $length = 2)
public function __construct(int $length = 2)
{
parent::__construct($code);
$this->length = $length;
}

/**
* Generate the EanExt data.
*
* @param string $code
*
* @return array
*/
public function generate(): array
public function generate(string $code): array
{
return $this->convertBarcodeArrayToNewStyle($this->barcode_eanext($this->code, $this->length));
return $this->convertBarcodeArrayToNewStyle($this->barcode_eanext($code, $this->length));
}

/**
Expand Down
Loading

0 comments on commit c2ef18c

Please sign in to comment.