Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode;
|
||||
|
||||
use Xentral\Components\Barcode\Exception\InvalidArgumentException;
|
||||
|
||||
final class BarcodeFactory
|
||||
{
|
||||
/**
|
||||
* @param string $codeText
|
||||
* @param string $ecLevel Error correction level [L|M|Q|H]
|
||||
*
|
||||
* @return Qrcode
|
||||
*/
|
||||
public function createQrCode($codeText, $ecLevel = 'L')
|
||||
{
|
||||
$codeType = 'QRCODE,' . $ecLevel;
|
||||
if (!in_array($codeType, Qrcode::$validTypes, true)) {
|
||||
throw new InvalidArgumentException('Invalid error correction level: ' . $ecLevel);
|
||||
}
|
||||
$barcode2d = new TcpdfBarcode2d($codeType, $codeText);
|
||||
|
||||
return new Qrcode($barcode2d);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode;
|
||||
|
||||
final class Bootstrap
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function registerServices()
|
||||
{
|
||||
return [
|
||||
'BarcodeFactory' => 'onInitBarcodeFactory',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BarcodeFactory
|
||||
*/
|
||||
public static function onInitBarcodeFactory()
|
||||
{
|
||||
return new BarcodeFactory();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class BarcodeCreationFailedException extends RuntimeException implements BarcodeExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode\Exception;
|
||||
|
||||
use Xentral\Core\Exception\ComponentExceptionInterface;
|
||||
|
||||
interface BarcodeExceptionInterface extends ComponentExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode\Exception;
|
||||
|
||||
use InvalidArgumentException as SplInvalidArgumentException;
|
||||
|
||||
class InvalidArgumentException extends SplInvalidArgumentException implements BarcodeExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class MissingPhpExtensionException extends RuntimeException implements BarcodeExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode;
|
||||
|
||||
final class Qrcode
|
||||
{
|
||||
/** @var string TYPE_QRCODE */
|
||||
const TYPE_DEFAULT = 'QRCODE';
|
||||
|
||||
/** @var string TYPE_EC_LOW Low error correction */
|
||||
const TYPE_EC_LOW = 'QRCODE,L';
|
||||
|
||||
/** @var string TYPE_EC_MEDIUM Medium error correction */
|
||||
const TYPE_EC_MEDIUM = 'QRCODE,M';
|
||||
|
||||
/** @var string TYPE_EC_QUARTILE Better error correction */
|
||||
const TYPE_EC_QUARTILE = 'QRCODE,Q';
|
||||
|
||||
/** @var string TYPE_EC_HIGH Best error correction */
|
||||
const TYPE_EC_HIGH = 'QRCODE,H';
|
||||
|
||||
/** @var array $validTypes */
|
||||
public static $validTypes = [
|
||||
self::TYPE_DEFAULT,
|
||||
self::TYPE_EC_LOW,
|
||||
self::TYPE_EC_MEDIUM,
|
||||
self::TYPE_EC_QUARTILE,
|
||||
self::TYPE_EC_HIGH,
|
||||
];
|
||||
|
||||
/** @var TcpdfBarcode2d $barcode */
|
||||
private $barcode;
|
||||
|
||||
/**
|
||||
* @param TcpdfBarcode2d $barcode
|
||||
*/
|
||||
public function __construct(TcpdfBarcode2d $barcode)
|
||||
{
|
||||
$this->barcode = $barcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
return $this->barcode->getText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the QR code as array representation
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->barcode->getBarcodeArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the QR code as HTML representation
|
||||
*
|
||||
* @param int $width Width of a single rectangle element in pixels.
|
||||
* @param int $height Height of a single rectangle element in pixels.
|
||||
* @param string $color Foreground color for bar elements (background is transparent).
|
||||
*
|
||||
* @return string HTML code
|
||||
*/
|
||||
public function toHtml($width = 10, $height = 10, $color = 'black')
|
||||
{
|
||||
return $this->barcode->getBarcodeHtml($width, $height, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the QR code as SVG document
|
||||
*
|
||||
* @param int $width Width of a single rectangle element in user units
|
||||
* @param int $height Height of a single rectangle element in user units
|
||||
* @param string $color Foreground color (in SVG format) for bar elements (background is transparent)
|
||||
*
|
||||
* @return string SVG document
|
||||
*/
|
||||
public function toSvg($width = 10, $height = 10, $color = 'black')
|
||||
{
|
||||
return $this->barcode->getBarcodeSvg($width, $height, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the QR code as PNG image (requires GD or Imagick library)
|
||||
*
|
||||
* @param int $width Width of a single rectangle element in pixels
|
||||
* @param int $height Height of a single rectangle element in pixels
|
||||
* @param array $color RGB-Array (0-255) foreground color for bar elements (background is transparent)
|
||||
*
|
||||
* @return string Image as string
|
||||
*/
|
||||
public function toPng($width = 10, $height = 10, $color = [0, 0, 0])
|
||||
{
|
||||
return $this->barcode->getBarcodePng($width, $height, $color);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Components\Barcode;
|
||||
|
||||
use TCPDF2DBarcode;
|
||||
use Xentral\Components\Barcode\Exception\BarcodeCreationFailedException;
|
||||
use Xentral\Components\Barcode\Exception\InvalidArgumentException;
|
||||
use Xentral\Components\Barcode\Exception\MissingPhpExtensionException;
|
||||
|
||||
/**
|
||||
* Anti-Corruption-Layer for TCPDF2DBarcode class
|
||||
*/
|
||||
final class TcpdfBarcode2d
|
||||
{
|
||||
/** @var array $validTypes */
|
||||
public static $validTypes = [
|
||||
Qrcode::TYPE_DEFAULT,
|
||||
Qrcode::TYPE_EC_LOW,
|
||||
Qrcode::TYPE_EC_MEDIUM,
|
||||
Qrcode::TYPE_EC_QUARTILE,
|
||||
Qrcode::TYPE_EC_HIGH,
|
||||
];
|
||||
|
||||
/** @var TCPDF2DBarcode $barcode */
|
||||
private $barcode;
|
||||
|
||||
/** @var string $codeType */
|
||||
private $type;
|
||||
|
||||
/** @var string $codeText */
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param string $text
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws BarcodeCreationFailedException
|
||||
*/
|
||||
public function __construct($type, $text)
|
||||
{
|
||||
if (empty($type)) {
|
||||
throw new InvalidArgumentException('Could not create barcode. Required parameter "type" is empty.');
|
||||
}
|
||||
if (empty($text)) {
|
||||
throw new InvalidArgumentException('Could not create barcode. Required parameter "text" is empty.');
|
||||
}
|
||||
if (!in_array($type, self::$validTypes, true)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Could not create barcode. Invalid Type: "%s". Valid types: [%s]',
|
||||
$type,
|
||||
implode('|', self::$validTypes)
|
||||
));
|
||||
}
|
||||
|
||||
$barcode = new TCPDF2DBarcode($text, $type);
|
||||
if ($barcode->getBarcodeArray() === false) {
|
||||
throw new BarcodeCreationFailedException(sprintf(
|
||||
'Could not create barcode. Type "%s" - Text "%s"', $type, $text
|
||||
));
|
||||
}
|
||||
|
||||
$this->barcode = $barcode;
|
||||
$this->text = $text;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBarcodeArray()
|
||||
{
|
||||
return $this->barcode->getBarcodeArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the QR code as HTML representation
|
||||
*
|
||||
* @param int $width Width of a single rectangle element in pixels.
|
||||
* @param int $height Height of a single rectangle element in pixels.
|
||||
* @param string $color Foreground color for bar elements (background is transparent).
|
||||
*
|
||||
* @return string HTML code
|
||||
*/
|
||||
public function getBarcodeHtml($width = 10, $height = 10, $color = 'black')
|
||||
{
|
||||
return $this->barcode->getBarcodeHTML($width, $height, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the QR code as SVG document
|
||||
*
|
||||
* @param int $width Width of a single rectangle element in user units
|
||||
* @param int $height Height of a single rectangle element in user units
|
||||
* @param string $color Foreground color (in SVG format) for bar elements (background is transparent)
|
||||
*
|
||||
* @return string SVG document
|
||||
*/
|
||||
public function getBarcodeSvg($width = 10, $height = 10, $color = 'black')
|
||||
{
|
||||
return $this->barcode->getBarcodeSVGcode($width, $height, $color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the QR code as PNG image (requires GD or Imagick library)
|
||||
*
|
||||
* @param int $width Width of a single rectangle element in pixels
|
||||
* @param int $height Height of a single rectangle element in pixels
|
||||
* @param array $color RGB-Array (0-255) foreground color for bar elements (background is transparent)
|
||||
*
|
||||
* @throws MissingPhpExtensionException If gd and imagick extension missing
|
||||
* @throws BarcodeCreationFailedException
|
||||
*
|
||||
* @return string Image as string
|
||||
*/
|
||||
public function getBarcodePng($width, $height, $color = [0, 0, 0])
|
||||
{
|
||||
$imageData = $this->barcode->getBarcodePngData($width, $height, $color);
|
||||
if (!function_exists('imagecreate') && !extension_loaded('imagick')) {
|
||||
throw new MissingPhpExtensionException(
|
||||
'Barcode image creation failed. PHP extension "gd" or "imagick" is required; both missing.'
|
||||
);
|
||||
}
|
||||
if ($imageData === false) {
|
||||
throw new BarcodeCreationFailedException(
|
||||
'Barcode image creation failed. Unknown error.'
|
||||
);
|
||||
}
|
||||
|
||||
return $imageData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
# Barcodes
|
||||
|
||||
## QR-Codes
|
||||
|
||||
### Beispiel
|
||||
|
||||
```php
|
||||
$factory = $this->app->Container->get('BarcodeFactory');
|
||||
|
||||
// Qrcode-Objekt erzeugen
|
||||
$ecLevel = 'M'; // M = Medium error correction
|
||||
$qrcode = $factory->createQrCode($qrtext, $ecLevel);
|
||||
|
||||
// Varianten für die Ausgabe
|
||||
$html = $qrcode->toHtml($width, $height, $color);
|
||||
$svg = $qrcode->toSvg($width, $height, $color);
|
||||
$png = $qrcode->toPng($width, $height, $color);
|
||||
```
|
||||
|
||||
##### Fehlerkorrektur-Levels
|
||||
|
||||
* `L` = Low / Niedrige Fehlerkorrektur (Default)
|
||||
* `M` = Medium / Mittlere Fehlerkorrektur
|
||||
* `Q` = Quartile / Bessere Fehlerkorrektur
|
||||
* `H` = High / Höchste Fehlerkorrektur
|
||||
Reference in New Issue
Block a user