Skip to content

Commit ab93dd4

Browse files
authored
Merge pull request #115 from Mattie112/endroidqr-v5
Fix issue #114 (Support for EndroidQR v5)
2 parents 137df4d + ec35073 commit ab93dd4

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

.github/workflows/test-endroid.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
php-version: ['8.1', '8.2']
14-
endroid-version: ["^4"]
14+
endroid-version: ["^3","^4","^5"]
1515

1616
steps:
1717
- uses: actions/checkout@v3
@@ -25,7 +25,7 @@ jobs:
2525

2626
- uses: ramsey/composer-install@v2
2727

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

3030
- run: composer lint-ci
3131
- run: composer test testsDependency/EndroidQRCodeTest.php

lib/Providers/Qr/EndroidQrCodeProvider.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ class EndroidQrCodeProvider implements IQRCodeProvider
2626

2727
protected $endroid4 = false;
2828

29+
protected $endroid5 = false;
30+
2931
public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
3032
{
3133
$this->endroid4 = method_exists(QrCode::class, 'create');
34+
$this->endroid5 = enum_exists(ErrorCorrectionLevel::class);
3235

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

7780
private function handleErrorCorrectionLevel(string $level): ErrorCorrectionLevelInterface|ErrorCorrectionLevel
7881
{
82+
// First check for version 5 (using enums)
83+
if ($this->endroid5) {
84+
return match ($level) {
85+
'L' => ErrorCorrectionLevel::Low,
86+
'M' => ErrorCorrectionLevel::Medium,
87+
'Q' => ErrorCorrectionLevel::Quartile,
88+
default => ErrorCorrectionLevel::High,
89+
};
90+
}
91+
92+
// If not check for version 4 (using classes)
93+
if ($this->endroid4) {
94+
return match ($level) {
95+
'L' => new ErrorCorrectionLevelLow(),
96+
'M' => new ErrorCorrectionLevelMedium(),
97+
'Q' => new ErrorCorrectionLevelQuartile(),
98+
default => new ErrorCorrectionLevelHigh(),
99+
};
100+
}
101+
102+
// Any other version will be using strings
79103
return match ($level) {
80-
'L' => $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW(),
81-
'M' => $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM(),
82-
'Q' => $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE(),
83-
default => $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH(),
104+
'L' => ErrorCorrectionLevel::LOW(),
105+
'M' => ErrorCorrectionLevel::MEDIUM(),
106+
'Q' => ErrorCorrectionLevel::QUARTILE(),
107+
default => ErrorCorrectionLevel::HIGH(),
84108
};
85109
}
86110
}

0 commit comments

Comments
 (0)