:octocat: move determining min/max version

This commit is contained in:
smiley
2026-03-17 18:37:01 +01:00
parent d746b66211
commit 3bfa0be1d5
3 changed files with 16 additions and 53 deletions
+6 -2
View File
@@ -14,7 +14,7 @@ namespace chillerlan\QRCode\Data;
use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, Mode, Version};
use chillerlan\Settings\SettingsContainerInterface;
use function sprintf;
use function max, min, sprintf;
/**
* Processes the binary data and maps it on a QRMatrix which is then being returned
@@ -193,10 +193,14 @@ final class QRData{
return new Version($this->options->version);
}
// we don't know whether the min/max versions were set correctly, so we determine here which value is higher
$min = min($this->options->versionMin, $this->options->versionMax);
$max = max($this->options->versionMin, $this->options->versionMax);
$total = $this->estimateTotalBitLength();
// guess the version number within the given range
for($version = $this->options->versionMin; $version <= $this->options->versionMax; $version++){
for($version = $min; $version <= $max; $version++){
if($total <= ($this->maxBitsForEcc[$version] - 4)){
return new Version($version);
}
+10 -36
View File
@@ -51,14 +51,22 @@ trait QROptionsTrait{
*
* if `QROptions::$version` is set to `Version::AUTO` (default: 1)
*/
protected int $versionMin = 1;
protected int $versionMin = 1 {
set{
$this->versionMin = max(1, min(40, $value));
}
}
/**
* Maximum QR version
*
* if `QROptions::$version` is set to `Version::AUTO` (default: 40)
*/
protected int $versionMax = 40;
protected int $versionMax = 40 {
set{
$this->versionMax = max(1, min(40, $value));
}
}
/**
* Error correct level
@@ -492,11 +500,6 @@ trait QROptionsTrait{
* @see FPDF::__construct()
*/
public string $fpdfMeasureUnit = 'pt' {
/**
* sets the FPDF measurement unit
*
* @codeCoverageIgnore
*/
set{
$value = strtolower($value);
@@ -505,7 +508,6 @@ trait QROptionsTrait{
}
// @todo throw or ignore silently?
}
}
@@ -521,34 +523,6 @@ trait QROptionsTrait{
*/
public string|null $xmlStylesheet = null;
/**
* clamp min/max version number
*/
protected function setMinMaxVersion(int $versionMin, int $versionMax):void{
$min = max(1, min(40, $versionMin));
$max = max(1, min(40, $versionMax));
$this->versionMin = min($min, $max);
$this->versionMax = max($min, $max);
}
/**
* sets the minimum version number
*
* @todo: for some reason this crashes php when trying to access the other property ($this->versionMax) within the hook
*/
protected function set_versionMin(int $version):void{
$this->setMinMaxVersion($version, $this->versionMax);
}
/**
* sets the maximum version number
*/
protected function set_versionMax(int $version):void{
$this->setMinMaxVersion($this->versionMin, $version);
}
/**
* clamp the logo space values between 0 and maximum length (177 modules at version 40)
*/
-15
View File
@@ -37,21 +37,6 @@ final class QROptionsTest extends TestCase{
$this::assertSame($expected, $o->version);
}
/**
* Tests the $versionMin/$versionMax clamping
*/
#[Test]
#[TestWith([5, 10, 5, 10], 'normal clamp')]
#[TestWith([-42, 42, 1, 40], 'exceeding values')]
#[TestWith([10, 5, 5, 10], 'min > max' )]
#[TestWith([42, -42, 1, 40], 'min > max, exceeding')]
public function versionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
$o = new QROptions(['versionMin' => $versionMin, 'versionMax' => $versionMax]);
$this::assertSame($expectedMin, $o->versionMin);
$this::assertSame($expectedMax, $o->versionMax);
}
/**
* Tests setting the ECC level from string or int
*