:octocat: +QRData::setBitBuffer() (see #143)

This commit is contained in:
smiley
2022-08-08 21:59:27 +02:00
parent 739579552d
commit 9b36cfb4a5
3 changed files with 105 additions and 15 deletions
+32 -8
View File
@@ -79,12 +79,7 @@ final class QRData{
*/
public function setData(array $dataSegments):self{
$this->dataSegments = $dataSegments;
$version = $this->options->version === Version::AUTO
? $this->getMinimumVersion()
: $this->options->version;
$this->version = new Version($version);
$this->version = $this->getMinimumVersion();
$this->bitBuffer->clear();
$this->writeBitBuffer();
@@ -92,6 +87,30 @@ final class QRData{
return $this;
}
/**
* Sets a BitBuffer object
*
* this can be used instead of setData(), however, the version auto detection is not available in this case.
*
* @throws \chillerlan\QRCode\Data\QRCodeDataException
*/
public function setBitBuffer(BitBuffer $bitBuffer):self{
if($this->options->version === Version::AUTO){
throw new QRCodeDataException('version auto detection is not available');
}
if($bitBuffer->getLength() === 0){
throw new QRCodeDataException('the given BitBuffer is empty');
}
$this->dataSegments = [];
$this->bitBuffer = $bitBuffer;
$this->version = new Version($this->options->version);
return $this;
}
/**
* returns a fresh matrix object with the data written and masked with the given $maskPattern
*/
@@ -142,13 +161,18 @@ final class QRData{
*
* @throws \chillerlan\QRCode\Data\QRCodeDataException
*/
private function getMinimumVersion():int{
private function getMinimumVersion():Version{
if($this->options->version !== Version::AUTO){
return new Version($this->options->version);
}
$total = $this->estimateTotalBitLength();
// guess the version number within the given range
for($version = $this->options->versionMin; $version <= $this->options->versionMax; $version++){
if($total <= $this->maxBitsForEcc[$version]){
return $version;
return new Version($version);
}
}
+10 -7
View File
@@ -29,7 +29,7 @@ abstract class DatainterfaceTestAbstract extends TestCase{
protected string $testdata;
protected function setUp():void{
$this->QRData = new QRData(new QROptions(['version' => 4]));
$this->QRData = new QRData(new QROptions);
$this->reflection = new ReflectionClass($this->QRData);
}
@@ -79,8 +79,11 @@ abstract class DatainterfaceTestAbstract extends TestCase{
$getMinimumVersion = $this->reflection->getMethod('getMinimumVersion');
$getMinimumVersion->setAccessible(true);
/** @var \chillerlan\QRCode\Common\Version $version */
$version = $getMinimumVersion->invoke($this->QRData);
$this::assertSame(1, $getMinimumVersion->invoke($this->QRData));
$this::assertInstanceOf(Version::class, $version);
$this::assertSame(1, $version->getVersionNumber());
}
abstract public function stringValidateProvider():array;
@@ -102,10 +105,7 @@ abstract class DatainterfaceTestAbstract extends TestCase{
$this->expectException(QRCodeDataException::class);
$this->expectExceptionMessage('data exceeds');
$this->QRData = new QRData(
new QROptions(['version' => Version::AUTO]),
[new $this->FQN(str_repeat($this->testdata, 1337))]
);
$this->QRData->setData([new $this->FQN(str_repeat($this->testdata, 1337))]);
}
/**
@@ -115,7 +115,10 @@ abstract class DatainterfaceTestAbstract extends TestCase{
$this->expectException(QRCodeDataException::class);
$this->expectExceptionMessage('code length overflow');
$this->QRData->setData([new $this->FQN(str_repeat($this->testdata, 1337))]);
$this->QRData = new QRData(
new QROptions(['version' => 4]),
[new $this->FQN(str_repeat($this->testdata, 1337))]
);
}
/**
+63
View File
@@ -0,0 +1,63 @@
<?php
/**
* Class QRDataTest
*
* @created 08.08.2022
* @author smiley <smiley@chillerlan.net>
* @copyright 2022 smiley
* @license MIT
*/
namespace chillerlan\QRCodeTest\Data;
use chillerlan\QRCode\Common\BitBuffer;
use chillerlan\QRCode\Common\MaskPattern;
use chillerlan\QRCode\Data\QRData;
use chillerlan\QRCode\Output\QRGdImage;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use PHPUnit\Framework\TestCase;
/**
*
*/
class QRDataTest extends TestCase{
/**
* tests setting the BitBuffer object directly
*/
public function testSetBitBuffer():void{
$rawBytes = [
67, 22, 135, 71, 71, 7, 51, 162, 242, 247, 119, 119, 114, 231, 150, 247,
87, 71, 86, 38, 82, 230, 54, 246, 210, 247, 118, 23, 70, 54, 131, 247,
99, 212, 68, 199, 167, 135, 39, 164, 100, 55, 148, 247, 50, 103, 67, 211,
67, 55, 48, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236,
17, 236, 17, 236, 17, 236, 17, 236, 17, 236, 17, 236
];
$options = new QROptions(['version' => 3]);
$bitBuffer = new BitBuffer($rawBytes);
$QRData = (new QRData($options))->setBitBuffer($bitBuffer);
$maskPattern = MaskPattern::getBestPattern($QRData);
$matrix = $QRData->writeMatrix($maskPattern);
$this::assertSame(3, $matrix->version()->getVersionNumber());
// attempt to read
$options->imageBase64 = false;
$options->readerUseImagickIfAvailable = false;
$output = new QRGdImage($options, $matrix);
$decodeResult = (new QRCode($options))->readFromBlob($output->dump());
$this::assertSame($decodeResult->data, 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s');
QRMatrixTest::debugMatrix($matrix);
}
}