Skip to content

Commit

Permalink
refactor: replace deprecated Type enum with BarcodeType in codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tintnaingwin-genie committed Mar 2, 2025
1 parent 00bcd4c commit 104f47e
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 118 deletions.
99 changes: 53 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
<h1 align="center">Laravel Barcode</h1>

[![Latest Version on Packagist](https://img.shields.io/packagist/v/ageekdev/laravel-barcode.svg?style=flat-square&logo=Packagist)](https://packagist.org/packages/ageekdev/laravel-barcode)
[![Laravel 9.x](https://img.shields.io/badge/Laravel-9.x-red.svg?style=flat-square)](https://laravel.com/docs/9.x)
[![Laravel 10.x](https://img.shields.io/badge/Laravel-10.x-red.svg?style=flat-square)](http://laravel.com/docs/10.x)
[![Laravel 11.x](https://img.shields.io/badge/Laravel-11.x-red.svg?style=flat-square)](http://laravel.com/docs/11.x)
[![Laravel 12.x](https://img.shields.io/badge/Laravel-12.x-red.svg?style=flat-square)](http://laravel.com/docs/12.x)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/ageekdev/laravel-barcode/run-tests.yml?style=flat-square)](https://github.com/ageekdev/laravel-barcode/actions/workflows/run-tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/ageekdev/laravel-barcode.svg?style=flat-square&logo=Packagist)](https://packagist.org/packages/ageekdev/laravel-barcode)
<p align="center">
<img src="https://banners.beyondco.de/Laravel%20Barcode.png?theme=light&packageManager=composer+require&packageName=ageekdev%2Flaravel-barcode&pattern=architect&style=style_1&description=Generate+barcodes+easily+in+your+Laravel+application&md=1&showWatermark=0&fontSize=100px&images=barcode&widths=200&heights=200" width="650">
</p>

This package can generate SVG, PNG, JPG and HTML images from the most used 1D barcode standards. Here's how you can use it:
<p align="center">
<a href="https://packagist.org/packages/ageekdev/laravel-barcode"><img alt="Latest Version on Packagist" src="https://img.shields.io/packagist/v/ageekdev/laravel-barcode.svg?style=flat-square&logo=Packagist"></a>
<a href="https://github.com/ageekdev/laravel-barcode/actions/workflows/run-tests.yml"><img alt="GitHub Tests Action Status" src="https://img.shields.io/github/actions/workflow/status/ageekdev/laravel-barcode/run-tests.yml?style=flat-square"></a>
<a href="https://packagist.org/packages/ageekdev/laravel-barcode"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/ageekdev/laravel-barcode.svg?style=flat-square&logo=Packagist"></a>
<a href="https://laravel.com/docs/9.x"><img alt="Laravel 9.x" src="https://img.shields.io/badge/Laravel-9.x-red.svg?style=flat-square"></a>
<a href="https://laravel.com/docs/10.x"><img alt="Laravel 10.x" src="https://img.shields.io/badge/Laravel-10.x-red.svg?style=flat-square"></a>
<a href="https://laravel.com/docs/11.x"><img alt="Laravel 11.x" src="https://img.shields.io/badge/Laravel-11.x-red.svg?style=flat-square"></a>
<a href="https://laravel.com/docs/12.x"><img alt="Laravel 12.x" src="https://img.shields.io/badge/Laravel-12.x-red.svg?style=flat-square"></a>
</p>

## Installation
# Laravel Barcode Generator

You can install the package via composer:
A package to generate barcodes in various formats (SVG, PNG, JPG, HTML) for Laravel applications.

## ⚙️ Installation

You can install the package via Composer:

```bash
composer require ageekdev/laravel-barcode
Expand All @@ -28,52 +36,52 @@ php artisan vendor:publish --tag="laravel-barcode-config"

```php
use AgeekDev\Barcode\Facades\Barcode;
use AgeekDev\Barcode\Enums\Type;
use AgeekDev\Barcode\Enums\BarcodeType;

$barcode = Barcode::imageType("svg")
->foregroundColor("#000000")
->height(30)
->widthFactor(2)
->type(Type::TYPE_CODE_128)
->type(BarcodeType::CODE_128)
->generate("081231723897");
```

## 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.

Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported.

- TYPE_CODE_32 (italian pharmaceutical code 'MINSAN')
- TYPE_CODE_39
- TYPE_CODE_39_CHECKSUM
- TYPE_CODE_39E
- TYPE_CODE_39E_CHECKSUM
- TYPE_CODE_93
- TYPE_STANDARD_2_5
- TYPE_STANDARD_2_5_CHECKSUM
- TYPE_INTERLEAVED_2_5
- TYPE_INTERLEAVED_2_5_CHECKSUM
- TYPE_CODE_128
- TYPE_CODE_128_A
- TYPE_CODE_128_B
- TYPE_CODE_128_C
- TYPE_EAN_2
- TYPE_EAN_5
- TYPE_EAN_8
- TYPE_EAN_13
- TYPE_UPC_A
- TYPE_UPC_E
- TYPE_MSI
- TYPE_MSI_CHECKSUM
- TYPE_POSTNET
- TYPE_PLANET
- TYPE_RMS4CC
- TYPE_KIX
- TYPE_IMB
- TYPE_CODABAR
- TYPE_CODE_11
- TYPE_PHARMA_CODE
- TYPE_PHARMA_CODE_TWO_TRACKS
Most used types are CODE_128 and CODE_39. Because of the best scanner support, variable length and most chars supported.

- CODE_32 (italian pharmaceutical code 'MINSAN')
- CODE_39
- CODE_39_CHECKSUM
- CODE_39E
- CODE_39E_CHECKSUM
- CODE_93
- STANDARD_2_5
- STANDARD_2_5_CHECKSUM
- INTERLEAVED_2_5
- INTERLEAVED_2_5_CHECKSUM
- CODE_128
- CODE_128_A
- CODE_128_B
- CODE_128_C
- EAN_2
- EAN_5
- EAN_8
- EAN_13
- UPC_A
- UPC_E
- MSI
- MSI_CHECKSUM
- POSTNET
- PLANET
- RMS4CC
- KIX
- IMB
- CODABAR
- CODE_11
- PHARMA_CODE
- PHARMA_CODE_TWO_TRACKS

## Testing

Expand All @@ -95,7 +103,6 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [Tint Naing Win](https://github.com/tintnaingwinn)
- [All Contributors](../../contributors)

This package contains code copied from [PHP Barcode Generator](https://github.com/picqer/php-barcode-generator)
Expand Down
2 changes: 1 addition & 1 deletion config/barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
| TYPE_KIX, TYPE_IMB, TYPE_CODABAR, TYPE_CODE_11, TYPE_PHARMA_CODE, TYPE_PHARMA_CODE_TWO_TRACKS
|
*/
'type' => \AgeekDev\Barcode\Enums\Type::TYPE_CODE_128,
'type' => \AgeekDev\Barcode\Enums\BarcodeType::CODE_128,

/*
* Foreground color of the barcode
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ parameters:
- src
tmpDir: build/phpstan
checkOctaneCompatibility: true
ignoreErrors:
- '#Parameter \$type of method .* has typehint with deprecated enum AgeekDev\\Barcode\\Enums\\Type#'

9 changes: 5 additions & 4 deletions src/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AgeekDev\Barcode;

use AgeekDev\Barcode\Contracts\ImageType;
use AgeekDev\Barcode\Enums\BarcodeType;
use AgeekDev\Barcode\Enums\Type;
use AgeekDev\Barcode\Types\TypeInterface;

Expand All @@ -17,7 +18,7 @@ abstract class AbstractGenerator implements ImageType

protected int $widthFactor;

protected Type $type;
protected BarcodeType|Type $type;

public function __construct()
{
Expand All @@ -27,17 +28,17 @@ public function __construct()
$this->type = config('barcode.type');
}

protected function getBarcodeData(string $code, Type $type): Barcode
protected function getBarcodeData(string $code, BarcodeType|Type $type): Barcode
{
return $this->createDataBuilderForType($type)->getBarcodeData($code);
}

protected function createDataBuilderForType(Type $type): TypeInterface
protected function createDataBuilderForType(BarcodeType|Type $type): TypeInterface
{
return $type->class();
}

public function type(Type $type): static
public function type(BarcodeType|Type $type): static
{
$this->type = $type;

Expand Down
3 changes: 2 additions & 1 deletion src/BarcodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use AgeekDev\Barcode\Drivers\JPG;
use AgeekDev\Barcode\Drivers\PNG;
use AgeekDev\Barcode\Drivers\SVG;
use AgeekDev\Barcode\Enums\BarcodeType;
use AgeekDev\Barcode\Enums\Type;
use AgeekDev\Barcode\Exceptions\BarcodeException;
use Closure;
Expand Down Expand Up @@ -151,7 +152,7 @@ public function generate(string $text): string
/**
* Set the barcode type.
*/
public function type(Type $type): ImageType
public function type(BarcodeType|Type $type): ImageType
{
return $this->imageType()->type($type);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/ImageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace AgeekDev\Barcode\Contracts;

use AgeekDev\Barcode\Enums\BarcodeType;
use AgeekDev\Barcode\Enums\Type;

interface ImageType
{
public function generate(string $text): string;

public function type(Type $type): static;
public function type(BarcodeType|Type $type): static;

public function foregroundColor(string $foregroundColor): static;

Expand Down
168 changes: 168 additions & 0 deletions src/Enums/BarcodeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace AgeekDev\Barcode\Enums;

use AgeekDev\Barcode\Types\TypeCodabar;
use AgeekDev\Barcode\Types\TypeCode11;
use AgeekDev\Barcode\Types\TypeCode128;
use AgeekDev\Barcode\Types\TypeCode128A;
use AgeekDev\Barcode\Types\TypeCode128B;
use AgeekDev\Barcode\Types\TypeCode128C;
use AgeekDev\Barcode\Types\TypeCode32;
use AgeekDev\Barcode\Types\TypeCode39;
use AgeekDev\Barcode\Types\TypeCode39Checksum;
use AgeekDev\Barcode\Types\TypeCode39Extended;
use AgeekDev\Barcode\Types\TypeCode39ExtendedChecksum;
use AgeekDev\Barcode\Types\TypeCode93;
use AgeekDev\Barcode\Types\TypeEan13;
use AgeekDev\Barcode\Types\TypeEan8;
use AgeekDev\Barcode\Types\TypeIntelligentMailBarcode;
use AgeekDev\Barcode\Types\TypeInterface;
use AgeekDev\Barcode\Types\TypeInterleaved25;
use AgeekDev\Barcode\Types\TypeInterleaved25Checksum;
use AgeekDev\Barcode\Types\TypeKix;
use AgeekDev\Barcode\Types\TypeMsi;
use AgeekDev\Barcode\Types\TypeMsiChecksum;
use AgeekDev\Barcode\Types\TypePharmacode;
use AgeekDev\Barcode\Types\TypePharmacodeTwoCode;
use AgeekDev\Barcode\Types\TypePlanet;
use AgeekDev\Barcode\Types\TypePostnet;
use AgeekDev\Barcode\Types\TypeRms4cc;
use AgeekDev\Barcode\Types\TypeStandard2of5;
use AgeekDev\Barcode\Types\TypeStandard2of5Checksum;
use AgeekDev\Barcode\Types\TypeUpcA;
use AgeekDev\Barcode\Types\TypeUpcE;
use AgeekDev\Barcode\Types\TypeUpcExtension2;
use AgeekDev\Barcode\Types\TypeUpcExtension5;

enum BarcodeType: string
{
case CODE_32 = 'C32';

case CODE_39 = 'C39';

case CODE_39_CHECKSUM = 'C39+';

case CODE_39E = 'C39E'; // CODE 39 EXTENDED

case CODE_39E_CHECKSUM = 'C39E+'; // CODE 39 EXTENDED + CHECKSUM

case CODE_93 = 'C93';

case STANDARD_2_5 = 'S25';

case STANDARD_2_5_CHECKSUM = 'S25+';

case INTERLEAVED_2_5 = 'I25';

case INTERLEAVED_2_5_CHECKSUM = 'I25+';

case CODE_128 = 'C128';

case CODE_128_A = 'C128A';

case CODE_128_B = 'C128B';

case CODE_128_C = 'C128C';

case EAN_2 = 'EAN2'; // 2-Digits UPC-Based Extention

case EAN_5 = 'EAN5'; // 5-Digits UPC-Based Extention

case EAN_8 = 'EAN8';

case EAN_13 = 'EAN13';

case UPC_A = 'UPCA';

case UPC_E = 'UPCE';

case MSI = 'MSI'; // MSI (Variation of Plessey code)

case MSI_CHECKSUM = 'MSI+'; // MSI + CHECKSUM (modulo 11)

case POSTNET = 'POSTNET';

case PLANET = 'PLANET';

case RMS4CC = 'RMS4CC'; // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)

case KIX = 'KIX'; // KIX (Klant index - Customer index)

case IMB = 'IMB'; // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200

case CODABAR = 'CODABAR';

case CODE_11 = 'CODE11';

case PHARMA_CODE = 'PHARMA';

case PHARMA_CODE_TWO_TRACKS = 'PHARMA2T';

public function class(): TypeInterface
{
return match ($this) {
self::CODE_32 => new TypeCode32,

self::CODE_39 => new TypeCode39,

self::CODE_39_CHECKSUM => new TypeCode39Checksum,

self::CODE_39E => new TypeCode39Extended,

self::CODE_39E_CHECKSUM => new TypeCode39ExtendedChecksum,

self::CODE_93 => new TypeCode93,

self::STANDARD_2_5 => new TypeStandard2of5,

self::STANDARD_2_5_CHECKSUM => new TypeStandard2of5Checksum,

self::INTERLEAVED_2_5 => new TypeInterleaved25,

self::INTERLEAVED_2_5_CHECKSUM => new TypeInterleaved25Checksum,

self::CODE_128 => new TypeCode128,

self::CODE_128_A => new TypeCode128A,

self::CODE_128_B => new TypeCode128B,

self::CODE_128_C => new TypeCode128C,

self::EAN_2 => new TypeUpcExtension2,

self::EAN_5 => new TypeUpcExtension5,

self::EAN_8 => new TypeEan8,

self::EAN_13 => new TypeEan13,

self::UPC_A => new TypeUpcA,

self::UPC_E => new TypeUpcE,

self::MSI => new TypeMsi,

self::MSI_CHECKSUM => new TypeMsiChecksum,

self::POSTNET => new TypePostnet,

self::PLANET => new TypePlanet,

self::RMS4CC => new TypeRms4cc,

self::KIX => new TypeKix,

self::IMB => new TypeIntelligentMailBarcode,

self::CODABAR => new TypeCodabar,

self::CODE_11 => new TypeCode11,

self::PHARMA_CODE => new TypePharmacode,

self::PHARMA_CODE_TWO_TRACKS => new TypePharmacodeTwoCode,
};
}
}
3 changes: 3 additions & 0 deletions src/Enums/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
use AgeekDev\Barcode\Types\TypeUpcExtension2;
use AgeekDev\Barcode\Types\TypeUpcExtension5;

/**
* @deprecated see BarcodeType
*/
enum Type: string
{
case TYPE_CODE_32 = 'C32';
Expand Down
Loading

0 comments on commit 104f47e

Please sign in to comment.