diff --git a/src/QRCode.php b/src/QRCode.php index 6c4ad8abb..51de03a27 100644 --- a/src/QRCode.php +++ b/src/QRCode.php @@ -175,6 +175,11 @@ class QRCode{ */ protected $qrOutputInterface; + /** + * @var \chillerlan\QRCode\Util + */ + protected $util; + /** * QRCode constructor. * @@ -185,6 +190,8 @@ class QRCode{ public function __construct($data, QROutputInterface $output, QROptions $options = null){ $this->qrOutputInterface = $output; $this->bitBuffer = new BitBuffer; + $this->util = new Util; + $this->setData($data, $options); } @@ -213,10 +220,10 @@ class QRCode{ $this->errorCorrectLevel = $options->errorCorrectLevel; switch(true){ - case Util::isAlphaNum($data): - $mode = Util::isNumber($data) ? QRDataInterface::MODE_NUMBER : QRDataInterface::MODE_ALPHANUM; + case $this->util->isAlphaNum($data): + $mode = $this->util->isNumber($data) ? QRDataInterface::MODE_NUMBER : QRDataInterface::MODE_ALPHANUM; break; - case Util::isKanji($data): + case $this->util->isKanji($data): $mode = QRDataInterface::MODE_KANJI; break; default: @@ -255,7 +262,7 @@ class QRCode{ } foreach(range(1, 10) as $type){ - if($length <= Util::getMaxLength($type, $mode, $this->errorCorrectLevel)){ + if($length <= $this->util->getMaxLength($type, $mode, $this->errorCorrectLevel)){ return $type; } } @@ -449,7 +456,7 @@ class QRCode{ * @return void */ protected function setTypeNumber(bool $test){ - $bits = Util::getBCHTypeNumber($this->typeNumber); + $bits = $this->util->getBCHTypeNumber($this->typeNumber); for($i = 0; $i < 18; $i++){ $a = (int)floor($i / 3); @@ -468,7 +475,7 @@ class QRCode{ */ protected function setTypeInfo(bool $test, int $pattern){ $this->setPattern(); - $bits = Util::getBCHTypeInfo(($this->errorCorrectLevel << 3) | $pattern); + $bits = $this->util->getBCHTypeInfo(($this->errorCorrectLevel << 3) | $pattern); for($i = 0; $i < 15; $i++){ $mod = !$test && (($bits >> $i) & 1) === 1; @@ -550,7 +557,7 @@ class QRCode{ */ protected function createBytes():array { $totalCodeCount = $maxDcCount = $maxEcCount = $offset = $index = 0; - $rsBlocks = Util::getRSBlocks($this->typeNumber, $this->errorCorrectLevel); + $rsBlocks = $this->util->getRSBlocks($this->typeNumber, $this->errorCorrectLevel); $rsBlockCount = count($rsBlocks); $dcdata = $ecdata = array_fill(0, $rsBlockCount, null); diff --git a/src/Util.php b/src/Util.php index 1c3b9ab7f..138c685ef 100644 --- a/src/Util.php +++ b/src/Util.php @@ -90,7 +90,7 @@ class Util{ * * @return bool */ - public static function isNumber(string $string):bool { + public function isNumber(string $string):bool { $len = strlen($string); for($i = 0; $i < $len; $i++){ @@ -109,7 +109,7 @@ class Util{ * * @return bool */ - public static function isAlphaNum(string $string):bool { + public function isAlphaNum(string $string):bool { $len = strlen($string); for($i = 0; $i < $len; $i++){ @@ -132,7 +132,7 @@ class Util{ * * @return bool */ - public static function isKanji(string $string):bool { + public function isKanji(string $string):bool { if(empty($string)){ return false; @@ -159,11 +159,11 @@ class Util{ * * @return int */ - public static function getBCHTypeInfo(int $data):int { + public function getBCHTypeInfo(int $data):int { $G15_MASK = (1 << 14)|(1 << 12)|(1 << 10)|(1 << 4)|(1 << 1); $G15 = (1 << 10)|(1 << 8)|(1 << 5)|(1 << 4)|(1 << 2)|(1 << 1)|(1 << 0); - return (($data << 10)|self::getBCHT($data, 10, $G15))^$G15_MASK; + return (($data << 10)|$this->getBCHT($data, 10, $G15))^$G15_MASK; } /** @@ -171,10 +171,10 @@ class Util{ * * @return int */ - public static function getBCHTypeNumber(int $data):int{ + public function getBCHTypeNumber(int $data):int{ $G18 = (1 << 12)|(1 << 11)|(1 << 10)|(1 << 9)|(1 << 8)|(1 << 5)|(1 << 2)|(1 << 0); - return ($data << 12)|self::getBCHT($data, 12, $G18); + return ($data << 12)|$this->getBCHT($data, 12, $G18); } /** @@ -184,11 +184,11 @@ class Util{ * * @return int */ - protected static function getBCHT(int $data, int $bits, int $mask):int { + protected function getBCHT(int $data, int $bits, int $mask):int { $digit = $data << $bits; - while(self::getBCHDigit($digit) - self::getBCHDigit($mask) >= 0){ - $digit ^= ($mask << (self::getBCHDigit($digit) - self::getBCHDigit($mask))); + while($this->getBCHDigit($digit) - $this->getBCHDigit($mask) >= 0){ + $digit ^= ($mask << ($this->getBCHDigit($digit) - $this->getBCHDigit($mask))); } return $digit; @@ -199,7 +199,7 @@ class Util{ * * @return int */ - public static function getBCHDigit(int $data):int { + public function getBCHDigit(int $data):int { $digit = 0; while($data !== 0){ @@ -217,7 +217,7 @@ class Util{ * @return array * @throws \chillerlan\QRCode\QRCodeException */ - public static function getRSBlocks(int $typeNumber, int $errorCorrectLevel):array { + public function getRSBlocks(int $typeNumber, int $errorCorrectLevel):array { if(!array_key_exists($errorCorrectLevel, QRCode::RSBLOCK)){ throw new QRCodeException('$typeNumber: '.$typeNumber.' / $errorCorrectLevel: '.$errorCorrectLevel); @@ -244,7 +244,7 @@ class Util{ * @return int * @throws \chillerlan\QRCode\QRCodeException */ - public static function getMaxLength(int $typeNumber, int $mode, int $errorCorrectLevel):int { + public function getMaxLength(int $typeNumber, int $mode, int $errorCorrectLevel):int { if(!array_key_exists($errorCorrectLevel, QRCode::RSBLOCK)){ throw new QRCodeException('Invalid error correct level: '.$errorCorrectLevel); diff --git a/tests/UtilTest.php b/tests/UtilTest.php index db243cd71..c150b7109 100644 --- a/tests/UtilTest.php +++ b/tests/UtilTest.php @@ -16,27 +16,36 @@ use PHPUnit\Framework\TestCase; class UtilTest extends TestCase{ + /** + * @var \chillerlan\QRCode\Util + */ + protected $util; + + public function setUp(){ + $this->util = new Util; + } + public function testIsNumber(){ - $this->assertEquals(true, Util::isNumber('1234567890')); - $this->assertEquals(false, Util::isNumber('abc')); + $this->assertEquals(true, $this->util->isNumber('1234567890')); + $this->assertEquals(false, $this->util->isNumber('abc')); } public function testIsAlphaNum(){ - $this->assertEquals(true, Util::isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:')); - $this->assertEquals(false, Util::isAlphaNum('#')); + $this->assertEquals(true, $this->util->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:')); + $this->assertEquals(false, $this->util->isAlphaNum('#')); } // http://stackoverflow.com/a/24755772 public function testIsKanji(){ - $this->assertEquals(true, Util::isKanji('茗荷')); - $this->assertEquals(false, Util::isKanji('')); - $this->assertEquals(false, Util::isKanji('ÃÃÃ')); // non-kanji - $this->assertEquals(false, Util::isKanji('荷')); // kanji forced into byte mode due to length + $this->assertEquals(true, $this->util->isKanji('茗荷')); + $this->assertEquals(false, $this->util->isKanji('')); + $this->assertEquals(false, $this->util->isKanji('ÃÃÃ')); // non-kanji + $this->assertEquals(false, $this->util->isKanji('荷')); // kanji forced into byte mode due to length } // coverage public function testGetBCHTypeNumber(){ - $this->assertEquals(7973, Util::getBCHTypeNumber(1)); + $this->assertEquals(7973, $this->util->getBCHTypeNumber(1)); } /** @@ -44,23 +53,23 @@ class UtilTest extends TestCase{ * @expectedExceptionMessage $typeNumber: 1 / $errorCorrectLevel: 42 */ public function testGetRSBlocksException(){ - Util::getRSBlocks(1, 42); + $this->util->getRSBlocks(1, 42); } /** * @expectedException \chillerlan\QRCode\QRCodeException * @expectedExceptionMessage Invalid error correct level: 42 */ - public static function testGetMaxLengthECLevelException(){ - Util::getMaxLength(QRCode::TYPE_01, QRDataInterface::MODE_BYTE, 42); + public function testGetMaxLengthECLevelException(){ + $this->util->getMaxLength(QRCode::TYPE_01, QRDataInterface::MODE_BYTE, 42); } /** * @expectedException \chillerlan\QRCode\QRCodeException * @expectedExceptionMessage Invalid mode: 1337 */ - public static function testGetMaxLengthModeException(){ - Util::getMaxLength(QRCode::TYPE_01, 1337, QRCode::ERROR_CORRECT_LEVEL_H); + public function testGetMaxLengthModeException(){ + $this->util->getMaxLength(QRCode::TYPE_01, 1337, QRCode::ERROR_CORRECT_LEVEL_H); } }