mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-08-17 12:33:09 +00:00
:octocat: +QRDataModeInterface::getParity()
This commit is contained in:
@@ -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){
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ interface QRDataModeInterface{
|
||||
*/
|
||||
public function getLengthInBits():int;
|
||||
|
||||
public function getParity():int;
|
||||
|
||||
/**
|
||||
* encoding conversion helper
|
||||
*
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user