:feelsgood:

This commit is contained in:
smiley
2015-12-03 17:38:50 +01:00
parent 514f95c742
commit 02b289e366
9 changed files with 319 additions and 379 deletions
+10 -5
View File
@@ -1,5 +1,6 @@
<?php
/**
* Class AlphaNum
*
* @filesource AlphaNum.php
* @created 25.11.2015
@@ -12,14 +13,13 @@
namespace codemasher\QRCode\Data;
use codemasher\QRCode\BitBuffer;
use codemasher\QRCode\Data;
use codemasher\QRCode\Data\QRDataBase;
use codemasher\QRCode\Data\QRDataInterface;
use codemasher\QRCode\QRCodeException;
use codemasher\QRCode\QRConst;
/**
* Class AlphaNum
*
*/
class AlphaNum extends QRDataBase implements QRDataInterface{
@@ -28,21 +28,26 @@ class AlphaNum extends QRDataBase implements QRDataInterface{
*/
public $mode = QRConst::MODE_ALPHANUM;
/**
* @var array
*/
protected $lengthBits = [9, 11, 13];
/**
* @param $buffer
*/
public function write(BitBuffer &$buffer){
$i = 0;
$len = strlen($this->data);
while($i + 1 < $len){
while($i + 1 < $this->dataLength){
$buffer->put($this->getCode(ord($this->data[$i])) * 45 + $this->getCode(ord($this->data[$i + 1])), 11);
$i += 2;
}
if($i < $len){
if($i < $this->dataLength){
$buffer->put($this->getCode(ord($this->data[$i])), 6);
}
}
/**
+9 -4
View File
@@ -1,5 +1,6 @@
<?php
/**
* Class Byte
*
* @filesource Byte.php
* @created 25.11.2015
@@ -18,7 +19,7 @@ use codemasher\QRCode\Data\QRDataInterface;
/**
* Class Byte
*
*/
class Byte extends QRDataBase implements QRDataInterface{
@@ -27,14 +28,18 @@ class Byte extends QRDataBase implements QRDataInterface{
*/
public $mode = QRConst::MODE_BYTE;
/**
* @var array
*/
protected $lengthBits = [8, 16, 16];
/**
* @param $buffer
*/
public function write(BitBuffer &$buffer){
$len = strlen($this->data);
for($i = 0; $i < $len; $i++){
for($i = 0; $i < $this->dataLength; $i++){
$buffer->put(ord($this->data[$i]), 8);
}
}
}
}
+9 -10
View File
@@ -1,5 +1,6 @@
<?php
/**
* Class Kanji
*
* @filesource Kanji.php
* @created 25.11.2015
@@ -18,7 +19,7 @@ use codemasher\QRCode\QRCodeException;
use codemasher\QRCode\QRConst;
/**
* Class Kanji
*
*/
class Kanji extends QRDataBase implements QRDataInterface{
@@ -27,6 +28,11 @@ class Kanji extends QRDataBase implements QRDataInterface{
*/
public $mode = QRConst::MODE_KANJI;
/**
* @var array
*/
protected $lengthBits = [8, 10, 12];
/**
* @param $buffer
*
@@ -35,8 +41,7 @@ class Kanji extends QRDataBase implements QRDataInterface{
public function write(BitBuffer &$buffer){
$i = 0;
$len = strlen($this->data);
while($i + 1 < $len){
while($i + 1 < $this->dataLength){
$c = ((0xff&ord($this->data[$i])) << 8)|(0xff&ord($this->data[$i + 1]));
if(0x8140 <= $c && $c <= 0x9FFC){
@@ -54,16 +59,10 @@ class Kanji extends QRDataBase implements QRDataInterface{
$i += 2;
}
if($i < $len){
if($i < $this->dataLength){
throw new QRCodeException('illegal char at '.($i + 1));
}
}
/**
* @return float
*/
public function getLength(){
return floor(strlen($this->data) / 2);
}
}
+12 -7
View File
@@ -1,5 +1,6 @@
<?php
/**
* Class Number
*
* @filesource Number.php
* @created 26.11.2015
@@ -18,7 +19,7 @@ use codemasher\QRCode\QRCodeException;
use codemasher\QRCode\QRConst;
/**
* Class Number
*
*/
class Number extends QRDataBase implements QRDataInterface{
@@ -27,6 +28,11 @@ class Number extends QRDataBase implements QRDataInterface{
*/
public $mode = QRConst::MODE_NUMBER;
/**
* @var array
*/
protected $lengthBits = [10, 12, 14];
/**
* @param $buffer
*
@@ -35,20 +41,19 @@ class Number extends QRDataBase implements QRDataInterface{
public function write(BitBuffer &$buffer){
$i = 0;
$len = strlen($this->data);
while($i + 2 < $len){
while($i + 2 < $this->dataLength){
$num = $this->parseInt(substr($this->data, $i, 3));
$buffer->put($num, 10);
$i += 3;
}
if($i < $len){
if($i < $this->dataLength){
if($len - $i === 1){
if($this->dataLength - $i === 1){
$num = $this->parseInt(substr($this->data, $i, $i + 1));
$buffer->put($num, 4);
}
else if($len - $i === 2){
else if($this->dataLength - $i === 2){
$num = $this->parseInt(substr($this->data, $i, $i + 2));
$buffer->put($num, 7);
}
@@ -65,8 +70,8 @@ class Number extends QRDataBase implements QRDataInterface{
* @throws \codemasher\QRCode\QRCodeException
*/
protected function parseInt($string){
$num = 0;
$num = 0;
$len = strlen($string);
for($i = 0; $i < $len; $i++){
$c = ord($string[$i]);
+20 -48
View File
@@ -1,5 +1,6 @@
<?php
/**
* Class QRDataBase
*
* @filesource QRDataBase.php
* @created 25.11.2015
@@ -17,7 +18,7 @@ use codemasher\QRCode\QRCodeException;
use codemasher\QRCode\Util;
/**
* Class QRDataBase
*
*/
class QRDataBase{
@@ -27,10 +28,20 @@ class QRDataBase{
public $mode;
/**
* @var
* @var string
*/
public $data;
/**
* @var int
*/
public $dataLength;
/**
* @var array
*/
protected $lengthBits = [0, 0, 0];
/**
* @var \codemasher\QRCode\Util
*/
@@ -43,16 +54,10 @@ class QRDataBase{
*/
public function __construct($data){
$this->data = $data;
$this->dataLength = strlen($data);
$this->util = new Util;
}
/**
* @throws \codemasher\QRCode\QRCodeException
*/
public function getLength(){
return strlen($this->data);
}
/**
* @param $type
*
@@ -60,48 +65,15 @@ class QRDataBase{
* @throws \codemasher\QRCode\QRCodeException
*/
public function getLengthInBits($type){
if(1 <= $type && $type < 10){
// 1 - 9
switch($this->mode){
case QRConst::MODE_NUMBER : return 10;
case QRConst::MODE_ALPHANUM: return 9;
case QRConst::MODE_BYTE : return 8;
case QRConst::MODE_KANJI : return 8;
default :
throw new QRCodeException('mode: '.$this->mode);
}
switch(true){
case 1 <= $type && $type < 10: return $this->lengthBits[0]; break; // 1 - 9
case $type < 27: return $this->lengthBits[1]; break; // 10 - 26
case $type < 41: return $this->lengthBits[2]; break; // 27 - 40
default:
throw new QRCodeException('mode: '.$this->mode);
}
else if($type < 27){
// 10 - 26
switch($this->mode){
case QRConst::MODE_NUMBER : return 12;
case QRConst::MODE_ALPHANUM: return 11;
case QRConst::MODE_BYTE : return 16;
case QRConst::MODE_KANJI : return 10;
default :
throw new QRCodeException('mode: '.$this->mode);
}
}
else if($type < 41){
// 27 - 40
switch($this->mode){
case QRConst::MODE_NUMBER : return 14;
case QRConst::MODE_ALPHANUM: return 13;
case QRConst::MODE_BYTE : return 16;
case QRConst::MODE_KANJI : return 12;
default :
throw new QRCodeException('mode: '.$this->mode);
}
}
else{
throw new QRCodeException('mode: '.$this->mode);
}
}
}
+2 -6
View File
@@ -1,5 +1,6 @@
<?php
/**
* Interface QRDataInterface
*
* @filesource QRDataInterface.php
* @created 01.12.2015
@@ -13,7 +14,7 @@ namespace codemasher\QRCode\Data;
use codemasher\QRCode\BitBuffer;
/**
* Interface QRDataInterface
*
*/
interface QRDataInterface{
@@ -22,11 +23,6 @@ interface QRDataInterface{
*/
public function write(BitBuffer &$buffer);
/**
* @return int
*/
public function getLength();
/**
* @param $type
*