:octocat: +QRDataModeInterface::getParity()

This commit is contained in:
smiley
2025-07-19 20:11:51 +02:00
parent daf01b96b2
commit 8c6ce5c4a7
5 changed files with 42 additions and 0 deletions
+5
View File
@@ -44,6 +44,11 @@ final class ECI extends QRDataModeAbstract{
$this->encoding = $encoding;
}
public function getParity():int{
throw new QRCodeDataException('not implemented');
}
public function getLengthInBits():int{
if($this->encoding < 128){
+15
View File
@@ -206,6 +206,21 @@ final class QRData{
throw new QRCodeDataException('failed to guess minimum version'); // @codeCoverageIgnore
}
public function getPariity():int{
$parity = 0;
foreach($this->dataSegments as $segment){
if($segment instanceof ECI){
continue;
}
$parity ^= $segment->getParity();
}
return $parity;
}
/**
* creates a BitBuffer and writes the string data to it
*
+11
View File
@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace chillerlan\QRCode\Data;
use chillerlan\QRCode\Common\Mode;
use function str_split;
/**
* abstract methods for the several data modes
@@ -45,6 +46,16 @@ abstract class QRDataModeAbstract implements QRDataModeInterface{
return strlen($this->data);
}
public function getParity():int{
$parity = 0;
foreach(str_split($this->data) as $chr){
$parity ^= ord($chr);
}
return $parity;
}
public static function convertEncoding(string $string):string{
return $string;
}
+2
View File
@@ -33,6 +33,8 @@ interface QRDataModeInterface{
*/
public function getLengthInBits():int;
public function getParity():int;
/**
* encoding conversion helper
*
+9
View File
@@ -15,6 +15,8 @@ use chillerlan\QRCode\Common\BitBuffer;
use chillerlan\QRCode\Common\EccLevel;
use chillerlan\QRCode\Common\MaskPattern;
use chillerlan\QRCode\Data\Byte;
use chillerlan\QRCode\Data\Kanji;
use chillerlan\QRCode\Data\Number;
use chillerlan\QRCode\Data\QRData;
use chillerlan\QRCode\Output\QRGdImagePNG;
use chillerlan\QRCode\QRCode;
@@ -88,4 +90,11 @@ final class QRDataTest extends TestCase{
$this::assertSame(11, $qrData->getMinimumVersion()->getVersionNumber()); // version adjusted to 11
}
public function testGetParity():void{
$qrData = new QRData(new QROptions, [new Number('0123'), new Number('4567'), new Number('89'), new Kanji('日本')]);
// ISO/IEC 18004:2000 - 9.3 Parity Data
$this::assertSame(133, $qrData->getPariity()); // 85 hex
}
}