Skip to content

Fix issue #114 (Support for EndroidQR v5) #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-endroid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
php-version: ['8.1', '8.2']
endroid-version: ["^4"]
endroid-version: ["^3","^4","^5"]

steps:
- uses: actions/checkout@v3
Expand All @@ -25,7 +25,7 @@ jobs:

- uses: ramsey/composer-install@v2

- run: composer require endroid/qrcode:${{ matrix.endroid-version }}
- run: composer require endroid/qrcode:${{ matrix.endroid-version }} -W

- run: composer lint-ci
- run: composer test testsDependency/EndroidQRCodeTest.php
32 changes: 28 additions & 4 deletions lib/Providers/Qr/EndroidQrCodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ class EndroidQrCodeProvider implements IQRCodeProvider

protected $endroid4 = false;

protected $endroid5 = false;

public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
{
$this->endroid4 = method_exists(QrCode::class, 'create');
$this->endroid5 = enum_exists(ErrorCorrectionLevel::class);

$this->bgcolor = $this->handleColor($bgcolor);
$this->color = $this->handleColor($color);
Expand Down Expand Up @@ -76,11 +79,32 @@ private function handleColor(string $color): Color|array

private function handleErrorCorrectionLevel(string $level): ErrorCorrectionLevelInterface|ErrorCorrectionLevel
{
// First check for version 5 (using enums)
if ($this->endroid5) {
return match ($level) {
'L' => ErrorCorrectionLevel::Low,
'M' => ErrorCorrectionLevel::Medium,
'Q' => ErrorCorrectionLevel::Quartile,
default => ErrorCorrectionLevel::High,
};
}

// If not check for version 4 (using classes)
if ($this->endroid4) {
return match ($level) {
'L' => new ErrorCorrectionLevelLow(),
'M' => new ErrorCorrectionLevelMedium(),
'Q' => new ErrorCorrectionLevelQuartile(),
default => new ErrorCorrectionLevelHigh(),
};
}

// Any other version will be using strings
return match ($level) {
'L' => $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW(),
'M' => $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM(),
'Q' => $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE(),
default => $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH(),
'L' => ErrorCorrectionLevel::LOW(),
'M' => ErrorCorrectionLevel::MEDIUM(),
'Q' => ErrorCorrectionLevel::QUARTILE(),
default => ErrorCorrectionLevel::HIGH(),
};
}
}