From 2c2f4b8bec0bb04ef7684754b8cb03cab86a98c9 Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 21 Mar 2016 00:00:52 +0100 Subject: [PATCH] refactoring --- src/Data/AlphaNum.php | 2 +- src/Data/Byte.php | 2 +- src/Data/Kanji.php | 2 +- src/Data/Number.php | 2 +- .../{QRDataBase.php => QRDataAbstract.php} | 9 +- src/Output/QRImage.php | 49 ++++++---- ...{QROutputBase.php => QROutputAbstract.php} | 6 +- src/Output/QRString.php | 97 +++++++++++-------- 8 files changed, 93 insertions(+), 76 deletions(-) rename src/Data/{QRDataBase.php => QRDataAbstract.php} (87%) rename src/Output/{QROutputBase.php => QROutputAbstract.php} (87%) diff --git a/src/Data/AlphaNum.php b/src/Data/AlphaNum.php index 71f8a5b48..941e319d7 100644 --- a/src/Data/AlphaNum.php +++ b/src/Data/AlphaNum.php @@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst; /** * */ -class AlphaNum extends QRDataBase implements QRDataInterface{ +class AlphaNum extends QRDataAbstract{ const CHAR_MAP = [ 36 => ' ', diff --git a/src/Data/Byte.php b/src/Data/Byte.php index f570b6334..8701fa2cd 100644 --- a/src/Data/Byte.php +++ b/src/Data/Byte.php @@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst; /** * */ -class Byte extends QRDataBase implements QRDataInterface{ +class Byte extends QRDataAbstract{ /** * @var int diff --git a/src/Data/Kanji.php b/src/Data/Kanji.php index 7b5ab649e..3d554cd9a 100644 --- a/src/Data/Kanji.php +++ b/src/Data/Kanji.php @@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst; /** * */ -class Kanji extends QRDataBase implements QRDataInterface{ +class Kanji extends QRDataAbstract{ /** * @var int diff --git a/src/Data/Number.php b/src/Data/Number.php index 029c7c1d6..004619d71 100644 --- a/src/Data/Number.php +++ b/src/Data/Number.php @@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst; /** * */ -class Number extends QRDataBase implements QRDataInterface{ +class Number extends QRDataAbstract{ /** * @var int diff --git a/src/Data/QRDataBase.php b/src/Data/QRDataAbstract.php similarity index 87% rename from src/Data/QRDataBase.php rename to src/Data/QRDataAbstract.php index 867056a0e..e7f4163b6 100644 --- a/src/Data/QRDataBase.php +++ b/src/Data/QRDataAbstract.php @@ -1,8 +1,8 @@ @@ -11,12 +11,11 @@ */ namespace chillerlan\QRCode\Data; -use chillerlan\QRCode\QRCode; /** * */ -class QRDataBase{ +abstract class QRDataAbstract implements QRDataInterface{ /** * @var string @@ -34,7 +33,7 @@ class QRDataBase{ protected $lengthBits = [0, 0, 0]; /** - * QRDataBase constructor. + * QRDataAbstract constructor. * * @param string $data */ diff --git a/src/Output/QRImage.php b/src/Output/QRImage.php index e1081e992..61ebec1ec 100644 --- a/src/Output/QRImage.php +++ b/src/Output/QRImage.php @@ -17,7 +17,7 @@ use chillerlan\QRCode\QRCode; /** * */ -class QRImage extends QROutputBase implements QROutputInterface{ +class QRImage extends QROutputAbstract{ /** * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions @@ -42,32 +42,18 @@ class QRImage extends QROutputBase implements QROutputInterface{ $this->options->{$val} = max(0, min(255, (int)$this->options->{$val})); } - if(!in_array($this->options->type, [QRCode::OUTPUT_IMAGE_PNG, QRCode::OUTPUT_IMAGE_JPG, QRCode::OUTPUT_IMAGE_GIF])){ - $this->options->type = QRCode::OUTPUT_IMAGE_PNG; - } - - $this->options->transparent = (bool)$this->options->transparent && $this->options->type !== QRCode::OUTPUT_IMAGE_JPG; - - if(!in_array($this->options->pngCompression, range(-1, 9), true)){ - $this->options->pngCompression = -1; - } - - if(!in_array($this->options->jpegQuality, range(0, 100), true)){ - $this->options->jpegQuality = 85; - } - } /** * @return string */ public function dump(){ - $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2; - $image = imagecreatetruecolor($length, $length); + $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2; + $image = imagecreatetruecolor($length, $length); $foreground = imagecolorallocate($image, $this->options->fgRed, $this->options->fgGreen, $this->options->fgBlue); $background = imagecolorallocate($image, $this->options->bgRed, $this->options->bgGreen, $this->options->bgBlue); - if($this->options->transparent){ + if((bool)$this->options->transparent && $this->options->type !== QRCode::OUTPUT_IMAGE_JPG){ imagecolortransparent($image, $background); } @@ -89,9 +75,30 @@ class QRImage extends QROutputBase implements QROutputInterface{ ob_start(); switch($this->options->type){ - case QRCode::OUTPUT_IMAGE_PNG: imagepng ($image, $this->options->cachefile, (int)$this->options->pngCompression); break; - case QRCode::OUTPUT_IMAGE_JPG: imagejpeg($image, $this->options->cachefile, (int)$this->options->jpegQuality); break; - case QRCode::OUTPUT_IMAGE_GIF: imagegif ($image, $this->options->cachefile); break; /** Actually, it's pronounced "DJIFF". *hides* */ + case QRCode::OUTPUT_IMAGE_JPG: + imagejpeg( + $image, + $this->options->cachefile, + in_array($this->options->jpegQuality, range(0, 100), true) + ? $this->options->jpegQuality + : 85 + ); + break; + case QRCode::OUTPUT_IMAGE_GIF: /** Actually, it's pronounced "DJIFF". *hides* */ + imagegif( + $image, + $this->options->cachefile + ); + break; + case QRCode::OUTPUT_IMAGE_PNG: + default: + imagepng( + $image, + $this->options->cachefile, + in_array($this->options->pngCompression, range(-1, 9), true) + ? $this->options->pngCompression + : -1 + ); } $imageData = ob_get_contents(); diff --git a/src/Output/QROutputBase.php b/src/Output/QROutputAbstract.php similarity index 87% rename from src/Output/QROutputBase.php rename to src/Output/QROutputAbstract.php index 1cc00cdcd..2ffa34cbb 100644 --- a/src/Output/QROutputBase.php +++ b/src/Output/QROutputAbstract.php @@ -1,8 +1,8 @@ @@ -15,7 +15,7 @@ namespace chillerlan\QRCode\Output; /** * */ -class QROutputBase{ +abstract class QROutputAbstract implements QROutputInterface{ /** * @var array diff --git a/src/Output/QRString.php b/src/Output/QRString.php index c246ddfc9..fd2754ba3 100644 --- a/src/Output/QRString.php +++ b/src/Output/QRString.php @@ -17,7 +17,7 @@ use chillerlan\QRCode\QRCode; /** * */ -class QRString extends QROutputBase implements QROutputInterface{ +class QRString extends QROutputAbstract{ /** * @var \chillerlan\QRCode\Output\QRStringOptions @@ -46,50 +46,61 @@ class QRString extends QROutputBase implements QROutputInterface{ */ public function dump(){ - if($this->options->type === QRCode::OUTPUT_STRING_JSON){ - return json_encode($this->matrix); - } - - else if($this->options->type === QRCode::OUTPUT_STRING_TEXT){ - $text = ''; - - foreach($this->matrix as $row){ - foreach($row as $col){ - $text .= $col - ? $this->options->textDark - : $this->options->textLight; - } - - $text .= $this->options->textNewline; - } - - return $text; - } - - else if($this->options->type === QRCode::OUTPUT_STRING_HTML){ - $html = ''; - - foreach($this->matrix as $row){ - // in order to not bloat the output too much, we use the shortest possible valid HTML tags - $html .= '<'.$this->options->htmlRowTag.'>'; - foreach($row as $col){ - $tag = $col - ? 'b' // dark - : 'i'; // light - - $html .= '<'.$tag.'>'; - } - - if(!(bool)$this->options->htmlOmitEndTag){ - $html .= 'options->htmlRowTag.'>'; - } - - $html .= PHP_EOL; - } - - return $html; + switch($this->options->type){ + case QRCode::OUTPUT_STRING_JSON: return json_encode($this->matrix); + case QRCode::OUTPUT_STRING_TEXT: return $this->toString(); + case QRCode::OUTPUT_STRING_HTML: + default: + return $this->toHTML(); } } + /** + * @return string + */ + protected function toString(){ + $str = ''; + + foreach($this->matrix as $row){ + foreach($row as $col){ + $str .= $col + ? $this->options->textDark + : $this->options->textLight; + } + + $str .= $this->options->textNewline; + } + + return $str; + } + + /** + * @return string + */ + protected function toHTML(){ + $html = ''; + + foreach($this->matrix as $row){ + // in order to not bloat the output too much, we use the shortest possible valid HTML tags + $html .= '<'.$this->options->htmlRowTag.'>'; + + foreach($row as $col){ + $tag = $col + ? 'b' // dark + : 'i'; // light + + $html .= '<'.$tag.'>'; + } + + if(!(bool)$this->options->htmlOmitEndTag){ + $html .= 'options->htmlRowTag.'>'; + } + + $html .= PHP_EOL; + } + + return $html; + } + }