diff --git a/src/Data/ECI.php b/src/Data/ECI.php index fd51ce6c2..a12ffe397 100644 --- a/src/Data/ECI.php +++ b/src/Data/ECI.php @@ -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){ diff --git a/src/Data/QRData.php b/src/Data/QRData.php index 65e46c5b7..4a3cc953c 100644 --- a/src/Data/QRData.php +++ b/src/Data/QRData.php @@ -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 * diff --git a/src/Data/QRDataModeAbstract.php b/src/Data/QRDataModeAbstract.php index 560d631a9..f6e2ea45e 100644 --- a/src/Data/QRDataModeAbstract.php +++ b/src/Data/QRDataModeAbstract.php @@ -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; } diff --git a/src/Data/QRDataModeInterface.php b/src/Data/QRDataModeInterface.php index f553a90ac..51cd5b554 100644 --- a/src/Data/QRDataModeInterface.php +++ b/src/Data/QRDataModeInterface.php @@ -33,6 +33,8 @@ interface QRDataModeInterface{ */ public function getLengthInBits():int; + public function getParity():int; + /** * encoding conversion helper * diff --git a/tests/Data/QRDataTest.php b/tests/Data/QRDataTest.php index 0e7fb7654..594bb5b39 100644 --- a/tests/Data/QRDataTest.php +++ b/tests/Data/QRDataTest.php @@ -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 + } + }