From ab8d58b7b2391b142420e281c10fcac46dbe30c2 Mon Sep 17 00:00:00 2001 From: smiley Date: Tue, 9 Feb 2016 15:56:46 +0100 Subject: [PATCH] moved type number auto detection --- src/QRCode.php | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/QRCode.php b/src/QRCode.php index ff96ef8ed..66e7c3ee8 100644 --- a/src/QRCode.php +++ b/src/QRCode.php @@ -143,24 +143,33 @@ class QRCode{ $this->qrDataInterface = new $qrDataInterface($data); if($this->typeNumber < 1 || $this->typeNumber > 10){ - /** @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, $mode, $this->errorCorrectLevel)){ - $this->typeNumber = $type; - - return $this; - } - } - + $this->typeNumber = $this->getTypeNumber($mode); } return $this; } + /** + * @param $mode + * + * @return int + * @throws \chillerlan\QRCode\QRCodeException + */ + protected function getTypeNumber($mode){ + /** @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, $mode, $this->errorCorrectLevel)){ + return $type; + } + } + + throw new QRCodeException('Unable to determine type number.'); + } + /** * @return mixed */