Просмотр исходного кода

:octocat: de-static Util::class

smiley 8 лет назад
Родитель
Сommit
3279980006
3 измененных файлов с 50 добавлено и 34 удалено
  1. 14 7
      src/QRCode.php
  2. 13 13
      src/Util.php
  3. 23 14
      tests/UtilTest.php

+ 14 - 7
src/QRCode.php

@@ -175,6 +175,11 @@ class QRCode{
 	 */
 	 */
 	protected $qrOutputInterface;
 	protected $qrOutputInterface;
 
 
+	/**
+	 * @var \chillerlan\QRCode\Util
+	 */
+	protected $util;
+
 	/**
 	/**
 	 * QRCode constructor.
 	 * QRCode constructor.
 	 *
 	 *
@@ -185,6 +190,8 @@ class QRCode{
 	public function __construct($data, QROutputInterface $output, QROptions $options = null){
 	public function __construct($data, QROutputInterface $output, QROptions $options = null){
 		$this->qrOutputInterface = $output;
 		$this->qrOutputInterface = $output;
 		$this->bitBuffer = new BitBuffer;
 		$this->bitBuffer = new BitBuffer;
+		$this->util = new Util;
+
 		$this->setData($data, $options);
 		$this->setData($data, $options);
 	}
 	}
 
 
@@ -213,10 +220,10 @@ class QRCode{
 		$this->errorCorrectLevel = $options->errorCorrectLevel;
 		$this->errorCorrectLevel = $options->errorCorrectLevel;
 
 
 		switch(true){
 		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;
 				break;
-			case Util::isKanji($data):
+			case $this->util->isKanji($data):
 				$mode = QRDataInterface::MODE_KANJI;
 				$mode = QRDataInterface::MODE_KANJI;
 				break;
 				break;
 			default:
 			default:
@@ -255,7 +262,7 @@ class QRCode{
 		}
 		}
 
 
 		foreach(range(1, 10) as $type){
 		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;
 				return $type;
 			}
 			}
 		}
 		}
@@ -449,7 +456,7 @@ class QRCode{
 	 * @return void
 	 * @return void
 	 */
 	 */
 	protected function setTypeNumber(bool $test){
 	protected function setTypeNumber(bool $test){
-		$bits = Util::getBCHTypeNumber($this->typeNumber);
+		$bits = $this->util->getBCHTypeNumber($this->typeNumber);
 
 
 		for($i = 0; $i < 18; $i++){
 		for($i = 0; $i < 18; $i++){
 			$a = (int)floor($i / 3);
 			$a = (int)floor($i / 3);
@@ -468,7 +475,7 @@ class QRCode{
 	 */
 	 */
 	protected function setTypeInfo(bool $test, int $pattern){
 	protected function setTypeInfo(bool $test, int $pattern){
 		$this->setPattern();
 		$this->setPattern();
-		$bits = Util::getBCHTypeInfo(($this->errorCorrectLevel << 3) | $pattern);
+		$bits = $this->util->getBCHTypeInfo(($this->errorCorrectLevel << 3) | $pattern);
 
 
 		for($i = 0; $i < 15; $i++){
 		for($i = 0; $i < 15; $i++){
 			$mod = !$test && (($bits >> $i) & 1) === 1;
 			$mod = !$test && (($bits >> $i) & 1) === 1;
@@ -550,7 +557,7 @@ class QRCode{
 	 */
 	 */
 	protected function createBytes():array {
 	protected function createBytes():array {
 		$totalCodeCount = $maxDcCount = $maxEcCount = $offset = $index = 0;
 		$totalCodeCount = $maxDcCount = $maxEcCount = $offset = $index = 0;
-		$rsBlocks = Util::getRSBlocks($this->typeNumber, $this->errorCorrectLevel);
+		$rsBlocks = $this->util->getRSBlocks($this->typeNumber, $this->errorCorrectLevel);
 		$rsBlockCount = count($rsBlocks);
 		$rsBlockCount = count($rsBlocks);
 		$dcdata = $ecdata = array_fill(0, $rsBlockCount, null);
 		$dcdata = $ecdata = array_fill(0, $rsBlockCount, null);
 
 

+ 13 - 13
src/Util.php

@@ -90,7 +90,7 @@ class Util{
 	 *
 	 *
 	 * @return bool
 	 * @return bool
 	 */
 	 */
-	public static function isNumber(string $string):bool {
+	public function isNumber(string $string):bool {
 		$len = strlen($string);
 		$len = strlen($string);
 
 
 		for($i = 0; $i < $len; $i++){
 		for($i = 0; $i < $len; $i++){
@@ -109,7 +109,7 @@ class Util{
 	 *
 	 *
 	 * @return bool
 	 * @return bool
 	 */
 	 */
-	public static function isAlphaNum(string $string):bool {
+	public function isAlphaNum(string $string):bool {
 		$len = strlen($string);
 		$len = strlen($string);
 
 
 		for($i = 0; $i < $len; $i++){
 		for($i = 0; $i < $len; $i++){
@@ -132,7 +132,7 @@ class Util{
 	 *
 	 *
 	 * @return bool
 	 * @return bool
 	 */
 	 */
-	public static function isKanji(string $string):bool {
+	public function isKanji(string $string):bool {
 
 
 		if(empty($string)){
 		if(empty($string)){
 			return false;
 			return false;
@@ -159,11 +159,11 @@ class Util{
 	 *
 	 *
 	 * @return int
 	 * @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_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);
 		$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
 	 * @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);
 		$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
 	 * @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;
 		$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;
 		return $digit;
@@ -199,7 +199,7 @@ class Util{
 	 *
 	 *
 	 * @return int
 	 * @return int
 	 */
 	 */
-	public static function getBCHDigit(int $data):int {
+	public function getBCHDigit(int $data):int {
 		$digit = 0;
 		$digit = 0;
 
 
 		while($data !== 0){
 		while($data !== 0){
@@ -217,7 +217,7 @@ class Util{
 	 * @return array
 	 * @return array
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 * @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)){
 		if(!array_key_exists($errorCorrectLevel, QRCode::RSBLOCK)){
 			throw new QRCodeException('$typeNumber: '.$typeNumber.' / $errorCorrectLevel: '.$errorCorrectLevel);
 			throw new QRCodeException('$typeNumber: '.$typeNumber.' / $errorCorrectLevel: '.$errorCorrectLevel);
@@ -244,7 +244,7 @@ class Util{
 	 * @return int
 	 * @return int
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 * @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)){
 		if(!array_key_exists($errorCorrectLevel, QRCode::RSBLOCK)){
 			throw new QRCodeException('Invalid error correct level: '.$errorCorrectLevel);
 			throw new QRCodeException('Invalid error correct level: '.$errorCorrectLevel);

+ 23 - 14
tests/UtilTest.php

@@ -16,27 +16,36 @@ use PHPUnit\Framework\TestCase;
 
 
 class UtilTest extends TestCase{
 class UtilTest extends TestCase{
 
 
+	/**
+	 * @var \chillerlan\QRCode\Util
+	 */
+	protected $util;
+
+	public function setUp(){
+		$this->util = new Util;
+	}
+
 	public function testIsNumber(){
 	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(){
 	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
 	// http://stackoverflow.com/a/24755772
 	public function testIsKanji(){
 	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
 	// coverage
 	public function testGetBCHTypeNumber(){
 	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
 	 * @expectedExceptionMessage $typeNumber: 1 / $errorCorrectLevel: 42
 	 */
 	 */
 	public function testGetRSBlocksException(){
 	public function testGetRSBlocksException(){
-		Util::getRSBlocks(1, 42);
+		$this->util->getRSBlocks(1, 42);
 	}
 	}
 
 
 	/**
 	/**
 	 * @expectedException \chillerlan\QRCode\QRCodeException
 	 * @expectedException \chillerlan\QRCode\QRCodeException
 	 * @expectedExceptionMessage Invalid error correct level: 42
 	 * @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
 	 * @expectedException \chillerlan\QRCode\QRCodeException
 	 * @expectedExceptionMessage Invalid mode: 1337
 	 * @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);
 	}
 	}
 
 
 }
 }