mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Added new config to set custom defaults for QR codes
This commit is contained in:
@@ -14,15 +14,17 @@ use Endroid\QrCode\Writer\SvgWriter;
|
||||
use Endroid\QrCode\Writer\WriterInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
|
||||
|
||||
use function Functional\contains;
|
||||
use function strtolower;
|
||||
use function trim;
|
||||
|
||||
final class QrCodeParams
|
||||
{
|
||||
private const DEFAULT_SIZE = 300;
|
||||
private const MIN_SIZE = 50;
|
||||
private const MAX_SIZE = 1000;
|
||||
private const SUPPORTED_FORMATS = ['png', 'svg'];
|
||||
|
||||
private function __construct(
|
||||
private int $size,
|
||||
@@ -32,22 +34,22 @@ final class QrCodeParams
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromRequest(ServerRequestInterface $request): self
|
||||
public static function fromRequest(ServerRequestInterface $request, QrCodeOptions $defaults): self
|
||||
{
|
||||
$query = $request->getQueryParams();
|
||||
|
||||
return new self(
|
||||
self::resolveSize($request, $query),
|
||||
self::resolveMargin($query),
|
||||
self::resolveWriter($query),
|
||||
self::resolveErrorCorrection($query),
|
||||
self::resolveSize($request, $query, $defaults),
|
||||
self::resolveMargin($query, $defaults),
|
||||
self::resolveWriter($query, $defaults),
|
||||
self::resolveErrorCorrection($query, $defaults),
|
||||
);
|
||||
}
|
||||
|
||||
private static function resolveSize(Request $request, array $query): int
|
||||
private static function resolveSize(Request $request, array $query, QrCodeOptions $defaults): int
|
||||
{
|
||||
// FIXME Size attribute is deprecated. After v3.0.0, always use the query param instead
|
||||
$size = (int) $request->getAttribute('size', $query['size'] ?? self::DEFAULT_SIZE);
|
||||
$size = (int) $request->getAttribute('size', $query['size'] ?? $defaults->size());
|
||||
if ($size < self::MIN_SIZE) {
|
||||
return self::MIN_SIZE;
|
||||
}
|
||||
@@ -55,13 +57,9 @@ final class QrCodeParams
|
||||
return $size > self::MAX_SIZE ? self::MAX_SIZE : $size;
|
||||
}
|
||||
|
||||
private static function resolveMargin(array $query): int
|
||||
private static function resolveMargin(array $query, QrCodeOptions $defaults): int
|
||||
{
|
||||
$margin = $query['margin'] ?? null;
|
||||
if ($margin === null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$margin = $query['margin'] ?? (string) $defaults->margin();
|
||||
$intMargin = (int) $margin;
|
||||
if ($margin !== (string) $intMargin) {
|
||||
return 0;
|
||||
@@ -70,18 +68,20 @@ final class QrCodeParams
|
||||
return $intMargin < 0 ? 0 : $intMargin;
|
||||
}
|
||||
|
||||
private static function resolveWriter(array $query): WriterInterface
|
||||
private static function resolveWriter(array $query, QrCodeOptions $defaults): WriterInterface
|
||||
{
|
||||
$format = strtolower(trim($query['format'] ?? 'png'));
|
||||
$qFormat = self::normalizeParam($query['format'] ?? '');
|
||||
$format = contains(self::SUPPORTED_FORMATS, $qFormat) ? $qFormat : self::normalizeParam($defaults->format());
|
||||
|
||||
return match ($format) {
|
||||
'svg' => new SvgWriter(),
|
||||
default => new PngWriter(),
|
||||
};
|
||||
}
|
||||
|
||||
private static function resolveErrorCorrection(array $query): ErrorCorrectionLevelInterface
|
||||
private static function resolveErrorCorrection(array $query, QrCodeOptions $defaults): ErrorCorrectionLevelInterface
|
||||
{
|
||||
$errorCorrectionLevel = strtolower(trim($query['errorCorrection'] ?? 'l'));
|
||||
$errorCorrectionLevel = self::normalizeParam($query['errorCorrection'] ?? $defaults->errorCorrection());
|
||||
return match ($errorCorrectionLevel) {
|
||||
'h' => new ErrorCorrectionLevelHigh(),
|
||||
'q' => new ErrorCorrectionLevelQuartile(),
|
||||
@@ -90,6 +90,11 @@ final class QrCodeParams
|
||||
};
|
||||
}
|
||||
|
||||
private static function normalizeParam(string $param): string
|
||||
{
|
||||
return strtolower(trim($param));
|
||||
}
|
||||
|
||||
public function size(): int
|
||||
{
|
||||
return $this->size;
|
||||
|
||||
@@ -14,6 +14,7 @@ use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
||||
use Shlinkio\Shlink\Core\Action\Model\QrCodeParams;
|
||||
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
||||
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
|
||||
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
||||
|
||||
@@ -23,6 +24,7 @@ class QrCodeAction implements MiddlewareInterface
|
||||
private ShortUrlResolverInterface $urlResolver,
|
||||
private ShortUrlStringifierInterface $stringifier,
|
||||
private LoggerInterface $logger,
|
||||
private QrCodeOptions $defaultOptions,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -37,7 +39,7 @@ class QrCodeAction implements MiddlewareInterface
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
$params = QrCodeParams::fromRequest($request);
|
||||
$params = QrCodeParams::fromRequest($request, $this->defaultOptions);
|
||||
$qrCodeBuilder = Builder::create()
|
||||
->data($this->stringifier->stringify($shortUrl))
|
||||
->size($params->size())
|
||||
|
||||
60
module/Core/src/Options/QrCodeOptions.php
Normal file
60
module/Core/src/Options/QrCodeOptions.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
use Laminas\Stdlib\AbstractOptions;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ERROR_CORRECTION;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_FORMAT;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_MARGIN;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_SIZE;
|
||||
|
||||
class QrCodeOptions extends AbstractOptions
|
||||
{
|
||||
private int $size = DEFAULT_QR_CODE_SIZE;
|
||||
private int $margin = DEFAULT_QR_CODE_MARGIN;
|
||||
private string $format = DEFAULT_QR_CODE_FORMAT;
|
||||
private string $errorCorrection = DEFAULT_QR_CODE_ERROR_CORRECTION;
|
||||
|
||||
public function size(): int
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
protected function setSize(int $size): void
|
||||
{
|
||||
$this->size = $size;
|
||||
}
|
||||
|
||||
public function margin(): int
|
||||
{
|
||||
return $this->margin;
|
||||
}
|
||||
|
||||
protected function setMargin(int $margin): void
|
||||
{
|
||||
$this->margin = $margin;
|
||||
}
|
||||
|
||||
public function format(): string
|
||||
{
|
||||
return $this->format;
|
||||
}
|
||||
|
||||
protected function setFormat(string $format): void
|
||||
{
|
||||
$this->format = $format;
|
||||
}
|
||||
|
||||
public function errorCorrection(): string
|
||||
{
|
||||
return $this->errorCorrection;
|
||||
}
|
||||
|
||||
protected function setErrorCorrection(string $errorCorrection): void
|
||||
{
|
||||
$this->errorCorrection = $errorCorrection;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user