| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class QRData |
| 4 |
* |
| 5 |
* @created 25.11.2015 |
| 6 |
* @author Smiley <[email protected]> |
| 7 |
* @copyright 2015 Smiley |
| 8 |
* @license MIT |
| 9 |
*/ |
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace chillerlan\QRCode\Data; |
| 13 |
|
| 14 |
use chillerlan\QRCode\QROptions; |
| 15 |
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, Mode, Version}; |
| 16 |
use chillerlan\Settings\SettingsContainerInterface; |
| 17 |
use function sprintf; |
| 18 |
|
| 19 |
/** |
| 20 |
* Processes the binary data and maps it on a QRMatrix which is then being returned |
| 21 |
*/ |
| 22 |
final class QRData{ |
| 23 |
|
| 24 |
/** |
| 25 |
* the options instance |
| 26 |
*/ |
| 27 |
private SettingsContainerInterface|QROptions $options; |
| 28 |
|
| 29 |
/** |
| 30 |
* a BitBuffer instance |
| 31 |
*/ |
| 32 |
private BitBuffer $bitBuffer; |
| 33 |
|
| 34 |
/** |
| 35 |
* an EccLevel instance |
| 36 |
*/ |
| 37 |
private EccLevel $eccLevel; |
| 38 |
|
| 39 |
/** |
| 40 |
* current QR Code version |
| 41 |
*/ |
| 42 |
private Version $version; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var \chillerlan\QRCode\Data\QRDataModeInterface[] |
| 46 |
*/ |
| 47 |
private array $dataSegments = []; |
| 48 |
|
| 49 |
/** |
| 50 |
* Max bits for the current ECC mode |
| 51 |
* |
| 52 |
* @var int[] |
| 53 |
*/ |
| 54 |
private array $maxBitsForEcc; |
| 55 |
|
| 56 |
/** |
| 57 |
* QRData constructor. |
| 58 |
* |
| 59 |
* @param \chillerlan\QRCode\Data\QRDataModeInterface[] $dataSegments |
| 60 |
*/ |
| 61 |
public function __construct(SettingsContainerInterface|QROptions $options, array $dataSegments = []){ |
| 62 |
$this->options = $options; |
| 63 |
$this->bitBuffer = new BitBuffer; |
| 64 |
$this->eccLevel = new EccLevel($this->options->eccLevel); |
| 65 |
$this->maxBitsForEcc = $this->eccLevel->getMaxBits(); |
| 66 |
|
| 67 |
$this->setData($dataSegments); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Sets the data string (internally called by the constructor) |
| 72 |
* |
| 73 |
* Subsequent calls will overwrite the current state - use the QRCode::add*Segement() method instead |
| 74 |
* |
| 75 |
* @param \chillerlan\QRCode\Data\QRDataModeInterface[] $dataSegments |
| 76 |
*/ |
| 77 |
public function setData(array $dataSegments):self{ |
| 78 |
$this->dataSegments = $dataSegments; |
| 79 |
$this->version = $this->getMinimumVersion(); |
| 80 |
|
| 81 |
$this->bitBuffer->clear(); |
| 82 |
$this->writeBitBuffer(); |
| 83 |
|
| 84 |
return $this; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the current BitBuffer instance |
| 89 |
* |
| 90 |
* @deprecated 6.0.1 This method will be removed. In v7, use the property "QRData::$bitBuffer" instead. |
| 91 |
* @codeCoverageIgnore |
| 92 |
*/ |
| 93 |
public function getBitBuffer():BitBuffer{ |
| 94 |
return $this->bitBuffer; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Sets a BitBuffer object |
| 99 |
* |
| 100 |
* This can be used instead of setData(), however, the version auto-detection is not available in this case. |
| 101 |
* The version needs to match the length bits range for the data mode the data has been encoded with, |
| 102 |
* additionally the bit array needs to contain enough pad bits. |
| 103 |
* |
| 104 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
| 105 |
*/ |
| 106 |
public function setBitBuffer(BitBuffer $bitBuffer):self{ |
| 107 |
|
| 108 |
if($this->options->version === Version::AUTO){ |
| 109 |
throw new QRCodeDataException('version auto detection is not available'); |
| 110 |
} |
| 111 |
|
| 112 |
if($bitBuffer->getLength() === 0){ |
| 113 |
throw new QRCodeDataException('the given BitBuffer is empty'); |
| 114 |
} |
| 115 |
|
| 116 |
$this->dataSegments = []; |
| 117 |
$this->bitBuffer = $bitBuffer; |
| 118 |
$this->version = new Version($this->options->version); |
| 119 |
|
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* returns a fresh matrix object with the data written and masked with the given $maskPattern |
| 125 |
*/ |
| 126 |
public function writeMatrix():QRMatrix{ |
| 127 |
return (new QRMatrix($this->version, $this->eccLevel)) |
| 128 |
->initFunctionalPatterns() |
| 129 |
->writeCodewords($this->bitBuffer) |
| 130 |
; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* estimates the total length of the several mode segments in order to guess the minimum version |
| 135 |
* |
| 136 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
| 137 |
*/ |
| 138 |
public function estimateTotalBitLength():int{ |
| 139 |
$length = 0; |
| 140 |
|
| 141 |
foreach($this->dataSegments as $segment){ |
| 142 |
// data length of the current segment |
| 143 |
$length += $segment->getLengthInBits(); |
| 144 |
// +4 bits for the mode descriptor |
| 145 |
$length += 4; |
| 146 |
// Hanzi mode sets an additional 4 bit long subset identifier |
| 147 |
if($segment instanceof Hanzi){ |
| 148 |
$length += 4; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
$provisionalVersion = null; |
| 153 |
/** @var int $version */ |
| 154 |
foreach($this->maxBitsForEcc as $version => $maxBits){ |
| 155 |
|
| 156 |
if($length <= $maxBits){ |
| 157 |
$provisionalVersion = $version; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
if($provisionalVersion !== null){ |
| 163 |
|
| 164 |
// add character count indicator bits for the provisional version |
| 165 |
foreach($this->dataSegments as $segment){ |
| 166 |
$length += Mode::getLengthBitsForVersion($segment::DATAMODE, $provisionalVersion); |
| 167 |
} |
| 168 |
|
| 169 |
// it seems that in some cases the estimated total length is not 100% accurate, |
| 170 |
// so we substract 4 bits from the total when not in mixed mode |
| 171 |
if(count($this->dataSegments) <= 1){ |
| 172 |
$length -= 4; |
| 173 |
} |
| 174 |
|
| 175 |
// we've got a match! |
| 176 |
// or let's see if there's a higher version number available |
| 177 |
if($length <= $this->maxBitsForEcc[$provisionalVersion] || isset($this->maxBitsForEcc[($provisionalVersion + 1)])){ |
| 178 |
return $length; |
| 179 |
} |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
throw new QRCodeDataException(sprintf('estimated data exceeds %d bits', $length)); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* returns the minimum version number for the given string |
| 188 |
* |
| 189 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException |
| 190 |
*/ |
| 191 |
public function getMinimumVersion():Version{ |
| 192 |
|
| 193 |
if($this->options->version !== Version::AUTO){ |
| 194 |
return new Version($this->options->version); |
| 195 |
} |
| 196 |
|
| 197 |
$total = $this->estimateTotalBitLength(); |
| 198 |
|
| 199 |
// guess the version number within the given range |
| 200 |
for($version = $this->options->versionMin; $version <= $this->options->versionMax; $version++){ |
| 201 |
if($total <= ($this->maxBitsForEcc[$version] - 4)){ |
| 202 |
return new Version($version); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// it's almost impossible to run into this one as $this::estimateTotalBitLength() would throw first |
| 207 |
throw new QRCodeDataException('failed to guess minimum version'); // @codeCoverageIgnore |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* creates a BitBuffer and writes the string data to it |
| 212 |
* |
| 213 |
* @throws \chillerlan\QRCode\Data\QRCodeDataException on data overflow |
| 214 |
*/ |
| 215 |
private function writeBitBuffer():void{ |
| 216 |
$MAX_BITS = $this->eccLevel->getMaxBitsForVersion($this->version); |
| 217 |
|
| 218 |
foreach($this->dataSegments as $segment){ |
| 219 |
$segment->write($this->bitBuffer, $this->version->getVersionNumber()); |
| 220 |
} |
| 221 |
|
| 222 |
// overflow, likely caused due to invalid version setting |
| 223 |
if($this->bitBuffer->getLength() > $MAX_BITS){ |
| 224 |
throw new QRCodeDataException( |
| 225 |
sprintf('code length overflow. (%d > %d bit)', $this->bitBuffer->getLength(), $MAX_BITS), |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
// add terminator (ISO/IEC 18004:2000 Table 2) |
| 230 |
if(($this->bitBuffer->getLength() + 4) <= $MAX_BITS){ |
| 231 |
$this->bitBuffer->put(Mode::TERMINATOR, 4); |
| 232 |
} |
| 233 |
|
| 234 |
// Padding: ISO/IEC 18004:2000 8.4.9 Bit stream to codeword conversion |
| 235 |
|
| 236 |
// if the final codeword is not exactly 8 bits in length, it shall be made 8 bits long |
| 237 |
// by the addition of padding bits with binary value 0 |
| 238 |
while(($this->bitBuffer->getLength() % 8) !== 0){ |
| 239 |
|
| 240 |
if($this->bitBuffer->getLength() === $MAX_BITS){ |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
$this->bitBuffer->putBit(false); |
| 245 |
} |
| 246 |
|
| 247 |
// The message bit stream shall then be extended to fill the data capacity of the symbol |
| 248 |
// corresponding to the Version and Error Correction Level, by the addition of the Pad |
| 249 |
// Codewords 11101100 and 00010001 alternately. |
| 250 |
$alternate = false; |
| 251 |
|
| 252 |
while(($this->bitBuffer->getLength() + 8) <= $MAX_BITS){ |
| 253 |
$this->bitBuffer->put(($alternate) ? 0b00010001 : 0b11101100, 8); |
| 254 |
|
| 255 |
$alternate = !$alternate; |
| 256 |
} |
| 257 |
|
| 258 |
// In certain versions of symbol, it may be necessary to add 3, 4 or 7 Remainder Bits (all zeros) |
| 259 |
// to the end of the message in order exactly to fill the symbol capacity |
| 260 |
while($this->bitBuffer->getLength() <= $MAX_BITS){ |
| 261 |
$this->bitBuffer->putBit(false); |
| 262 |
} |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
} |