浏览代码

added QROptions

smiley 10 年之前
父节点
当前提交
02d140d582
共有 3 个文件被更改,包括 78 次插入82 次删除
  1. 7 8
      examples/custom.php
  2. 31 74
      src/QRCode.php
  3. 40 0
      src/QROptions.php

+ 7 - 8
examples/custom.php

@@ -4,20 +4,19 @@ require_once '../vendor/autoload.php';
 
 use chillerlan\QRCode\QRCode;
 use chillerlan\QRCode\QRConst;
+use chillerlan\QRCode\QROptions;
 
 $starttime = microtime(true);
 
-$qrcode = (new QRCode)
-	->setErrorCorrectLevel(QRConst::ERROR_CORRECT_LEVEL_M)
-	->setQRType(QRConst::TYPE_05)
-;
+$qrOptions = new QROptions;
+$qrOptions->typeNumber = QRConst::TYPE_05;
+$qrOptions->errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M;
 
 // google authenticator
 // https://chart.googleapis.com/chart?chs=200x200&chld=M%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2Ftest%3Fsecret%3DB3JX4VCVJDVNXNZ5%26issuer%3Dchillerlan.net
-$qrcode
-	->setData('otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net')
-	->getRawData()
-;
+$qrcode = new QRCode;
+$qrcode->setOptions($qrOptions, 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net');
+$qrcode->getRawData();
 
 for($row = 0; $row < $qrcode->pixelCount; $row++){
 	for($col = 0; $col < $qrcode->pixelCount; $col++){

+ 31 - 74
src/QRCode.php

@@ -12,8 +12,6 @@
 
 namespace chillerlan\QRCode;
 
-use chillerlan\QRCode\Data\QRDataInterface;
-
 /**
  * @link https://github.com/kazuhikoarase/qrcode-generator/tree/master/php
  */
@@ -22,17 +20,12 @@ class QRCode{
 	/**
 	 * @var int
 	 */
-	public $pixelCount;
+	public $pixelCount = 0;
 
 	/**
 	 * @var array
 	 */
-	public $matrix;
-
-	/**
-	 * @var int
-	 */
-	protected $mode;
+	public $matrix = [];
 
 	/**
 	 * @var int
@@ -57,80 +50,64 @@ class QRCode{
 	/**
 	 * QRCode constructor.
 	 *
-	 * @param string $data
-	 * @param int    $errorCorrectLevel
-	 * @param int    $typeNumber
+	 * @param string                       $data
+	 * @param \chillerlan\QRCode\QROptions $options
 	 *
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	public function __construct($data = '', $errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M, $typeNumber = null){
+	public function __construct($data = null, QROptions $options = null){
 		$this->bitBuffer = new BitBuffer;
 
-		if(!empty($data)){
-			$this->getQRCode($data, $errorCorrectLevel, $typeNumber);
+		if($data){
+
+			if(!$options instanceof QROptions){
+				$options = new QROptions;
+			}
+
+			$this->setOptions($options, $data);
 		}
 
 	}
 
 	/**
-	 * @param string $data
+	 * @param \chillerlan\QRCode\QROptions $options
+	 * @param string                       $data
 	 *
 	 * @return $this
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
-	public function setData($data){
-		$data = trim((string)$data);
+	public function setOptions(QROptions $options, $data){
+		$data = trim($data);
 
 		if(empty($data)){
 			throw new QRCodeException('No data given.');
 		}
 
-		$this->mode = Util::getMode($data);
+		if(!array_key_exists($options->errorCorrectLevel, QRConst::RSBLOCK)){
+			throw new QRCodeException('Invalid error correct level: '.$options->errorCorrectLevel);
+		}
+
+		$mode = Util::getMode($data);
 
 		$qrDataInterface = __NAMESPACE__.'\\Data\\'.[
 			QRConst::MODE_ALPHANUM => 'AlphaNum',
 			QRConst::MODE_BYTE     => 'Byte',
 			QRConst::MODE_KANJI    => 'Kanji',
 			QRConst::MODE_NUMBER   => 'Number',
-		][$this->mode];
+		][$mode];
 
+		$this->errorCorrectLevel = $options->errorCorrectLevel;
+		$this->typeNumber = intval($options->typeNumber);
 		$this->qrDataInterface = new $qrDataInterface($data);
 
-		return $this;
-	}
-
-	/**
-	 * @param int $errorCorrectLevel
-	 *
-	 * @return $this
-	 * @throws \chillerlan\QRCode\QRCodeException
-	 */
-	public function setErrorCorrectLevel($errorCorrectLevel){
-
-		if(!array_key_exists($errorCorrectLevel, QRConst::RSBLOCK)){
-			throw new QRCodeException('Invalid error correct level: '.$errorCorrectLevel);
-		}
-
-		$this->errorCorrectLevel = $errorCorrectLevel;
-
-		return $this;
-	}
-
-	/**
-	 * @param int $typeNumber
-	 *
-	 * @return $this
-	 * @throws \chillerlan\QRCode\QRCodeException
-	 */
-	public function setQRType($typeNumber){
-		$this->typeNumber = intval($typeNumber);
-
 		if($this->typeNumber < 1 || $this->typeNumber > 10){
-
-			$length = $this->qrDataInterface->mode === QRConst::MODE_KANJI ? floor($this->qrDataInterface->dataLength / 2) : $this->qrDataInterface->dataLength;
+			/** @noinspection PhpUndefinedFieldInspection */
+			$length = $this->qrDataInterface->mode === QRConst::MODE_KANJI
+				? floor($this->qrDataInterface->dataLength / 2)
+				: $this->qrDataInterface->dataLength;
 
 			for($type = 1; $type <= 10; $type++){
-				if($length <= Util::getMaxLength($type, $this->mode, $this->errorCorrectLevel)){
+				if($length <= Util::getMaxLength($type, $mode, $this->errorCorrectLevel)){
 					$this->typeNumber = $type;
 
 					return $this;
@@ -142,32 +119,10 @@ class QRCode{
 		return $this;
 	}
 
-	/**
-	 * @param string $data
-	 * @param int    $errorCorrectLevel
-	 * @param int    $typeNumber
-	 *
-	 * @return $this
-	 * @throws \chillerlan\QRCode\QRCodeException
-	 */
-	public function getQRCode($data, $errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M, $typeNumber = null){
-
-		$this
-		     ->setData($data)
-		     ->setErrorCorrectLevel($errorCorrectLevel)
-		     ->setQRType($typeNumber)
-		     ->getRawData()
-		;
-
-		return $this;
-	}
-
 	/**
 	 * @return $this
 	 */
 	public function getRawData(){
-
-		// getLostPoint
 		$minLostPoint = 0;
 		$pattern = 0;
 
@@ -504,7 +459,9 @@ class QRCode{
 		$MAX_BITS = QRConst::MAX_BITS; // php5 compat
 		$MAX_BITS = $MAX_BITS[$this->typeNumber][$this->errorCorrectLevel];
 
+		/** @noinspection PhpUndefinedFieldInspection */
 		$this->bitBuffer->put($this->qrDataInterface->mode, 4);
+		/** @noinspection PhpUndefinedFieldInspection */
 		$this->bitBuffer->put(
 			$this->qrDataInterface->mode === QRConst::MODE_KANJI ? floor($this->qrDataInterface->dataLength / 2) : $this->qrDataInterface->dataLength,
 			$this->qrDataInterface->getLengthInBits($this->typeNumber)

+ 40 - 0
src/QROptions.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Class QROptions
+ *
+ * @filesource   QROptions.php
+ * @created      08.12.2015
+ * @package      chillerlan\QRCode
+ * @author       Smiley <smiley@chillerlan.net>
+ * @copyright    2015 Smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCode;
+
+/**
+ *
+ */
+class QROptions{
+
+	/**
+	 * @var int
+	 */
+	public $errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M;
+
+	/**
+	 * @var int
+	 */
+	public $typeNumber = null;
+
+	/**
+	 * @var \chillerlan\QRCode\Output\QROutputInterface
+	 */
+	public $output = null ;
+
+	/**
+	 * @var \chillerlan\QRCode\Output\QROutputOptionsInterface
+	 */
+	public $outputOptions = null;
+
+}