-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added support for QR codes config option (#871)
- Loading branch information
Showing
6 changed files
with
224 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace App\Actions; | ||
|
||
use Endroid\QrCode\Builder\Builder; | ||
use Endroid\QrCode\Writer\Result\ResultInterface; | ||
|
||
class QrCode | ||
{ | ||
const MIN_SIZE = 50; | ||
|
||
const MAX_SIZE = 1000; | ||
|
||
const FORMAT = 'png'; | ||
|
||
const SUPPORTED_FORMAT = ['png', 'svg']; | ||
|
||
public function process(string $data): ResultInterface | ||
{ | ||
return Builder::create() | ||
->data($data) | ||
->labelText(__('Scan QR Code')) | ||
->size($this->resolveSize()) | ||
->margin($this->resolveMargin()) | ||
->writer($this->resolveWriter()) | ||
->errorCorrectionLevel($this->resolveErrorCorrection()) | ||
->roundBlockSizeMode($this->resolveRoundBlockSize()) | ||
->build(); | ||
} | ||
|
||
protected function resolveSize(): int | ||
{ | ||
$size = config('urlhub.qrcode_size'); | ||
|
||
if ($size < self::MIN_SIZE) { | ||
return self::MIN_SIZE; | ||
} | ||
|
||
return $size > self::MAX_SIZE ? self::MAX_SIZE : $size; | ||
} | ||
|
||
protected function resolveMargin(): int | ||
{ | ||
$margin = config('urlhub.qrcode_margin'); | ||
$intMargin = (int) $margin; | ||
|
||
if ($margin !== (string) $intMargin) { | ||
return 0; | ||
} | ||
|
||
return $intMargin < 0 ? 0 : $intMargin; | ||
} | ||
|
||
/** | ||
* @return \Endroid\QrCode\Writer\WriterInterface | ||
*/ | ||
protected function resolveWriter() | ||
{ | ||
$qFormat = self::normalizeValue(config('urlhub.qrcode_format')); | ||
$containSupportedFormat = collect(self::SUPPORTED_FORMAT) | ||
->containsStrict($qFormat); | ||
$format = $containSupportedFormat ? $qFormat : self::FORMAT; | ||
|
||
return match ($format) { | ||
'svg' => new \Endroid\QrCode\Writer\SvgWriter, | ||
default => new \Endroid\QrCode\Writer\PngWriter, | ||
}; | ||
} | ||
|
||
/** | ||
* @return \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface | ||
*/ | ||
protected function resolveErrorCorrection() | ||
{ | ||
$level = self::normalizeValue(config('urlhub.qrcode_error_correction')); | ||
|
||
return match ($level) { | ||
'h' => new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh, | ||
'q' => new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile, | ||
'm' => new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium, | ||
default => new \Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow, // 'l' | ||
}; | ||
} | ||
|
||
/** | ||
* @return \Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface | ||
*/ | ||
protected function resolveRoundBlockSize() | ||
{ | ||
$isRounded = config('urlhub.qrcode_round_block_size'); | ||
|
||
if (! $isRounded) { | ||
return new \Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeNone; | ||
} | ||
|
||
return new \Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; | ||
} | ||
|
||
protected function normalizeValue(string $param): string | ||
{ | ||
return strtolower(trim($param)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Actions; | ||
|
||
use App\Actions\QrCode; | ||
use Endroid\QrCode\Writer\Result\ResultInterface; | ||
use Tests\TestCase; | ||
|
||
class QrCodeTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @group u-actions | ||
*/ | ||
public function qrCode() | ||
{ | ||
$qrCode = (new QrCode)->process('foo'); | ||
|
||
$this->assertInstanceOf(ResultInterface::class, $qrCode); | ||
$this->assertIsString($qrCode->getDataUri()); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group u-actions | ||
*/ | ||
public function sizeMin() | ||
{ | ||
$size = QrCode::MIN_SIZE - 1; | ||
config(['urlhub.qrcode_size' => $size]); | ||
|
||
$image = imagecreatefromstring((new QrCode)->process('foo')->getString()); | ||
|
||
$this->assertNotSame($size, (int) imagesx($image)); | ||
$this->assertSame(QrCode::MIN_SIZE, imagesx($image)); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group u-actions | ||
*/ | ||
public function sizeMax() | ||
{ | ||
$size = QrCode::MAX_SIZE + 1; | ||
config(['urlhub.qrcode_size' => $size]); | ||
|
||
$image = imagecreatefromstring((new QrCode)->process('foo')->getString()); | ||
|
||
$this->assertNotSame($size, imagesx($image)); | ||
$this->assertSame(QrCode::MAX_SIZE, imagesx($image)); | ||
} | ||
} |