From 02d140d58262ea2987e74ee5cc2b4bde92ae19db Mon Sep 17 00:00:00 2001 From: smiley Date: Tue, 8 Dec 2015 22:54:30 +0100 Subject: [PATCH] added QROptions --- examples/custom.php | 15 +++---- src/QRCode.php | 105 +++++++++++++------------------------------- src/QROptions.php | 40 +++++++++++++++++ 3 files changed, 78 insertions(+), 82 deletions(-) create mode 100644 src/QROptions.php diff --git a/examples/custom.php b/examples/custom.php index 2123d68f9..b7fd973ef 100644 --- a/examples/custom.php +++ b/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++){ diff --git a/src/QRCode.php b/src/QRCode.php index 03f98b770..6cfbaab3e 100644 --- a/src/QRCode.php +++ b/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) diff --git a/src/QROptions.php b/src/QROptions.php new file mode 100644 index 000000000..a1d8ed4d6 --- /dev/null +++ b/src/QROptions.php @@ -0,0 +1,40 @@ + + * @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; + +}