refactoring

This commit is contained in:
smiley
2016-03-21 00:00:52 +01:00
parent 3cb15c94fb
commit 2c2f4b8bec
8 changed files with 93 additions and 76 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst;
/**
*
*/
class AlphaNum extends QRDataBase implements QRDataInterface{
class AlphaNum extends QRDataAbstract{
const CHAR_MAP = [
36 => ' ',
+1 -1
View File
@@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst;
/**
*
*/
class Byte extends QRDataBase implements QRDataInterface{
class Byte extends QRDataAbstract{
/**
* @var int
+1 -1
View File
@@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst;
/**
*
*/
class Kanji extends QRDataBase implements QRDataInterface{
class Kanji extends QRDataAbstract{
/**
* @var int
+1 -1
View File
@@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst;
/**
*
*/
class Number extends QRDataBase implements QRDataInterface{
class Number extends QRDataAbstract{
/**
* @var int
@@ -1,8 +1,8 @@
<?php
/**
* Class QRDataBase
* Class QRDataAbstract
*
* @filesource QRDataBase.php
* @filesource QRDataAbstract.php
* @created 25.11.2015
* @package chillerlan\QRCode\Data
* @author Smiley <smiley@chillerlan.net>
@@ -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
*/
+28 -21
View File
@@ -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();
@@ -1,8 +1,8 @@
<?php
/**
* Class QROutputBase
* Class QROutputAbstract
*
* @filesource QROutputBase.php
* @filesource QROutputAbstract.php
* @created 09.12.2015
* @package chillerlan\QRCode\Output
* @author Smiley <smiley@chillerlan.net>
@@ -15,7 +15,7 @@ namespace chillerlan\QRCode\Output;
/**
*
*/
class QROutputBase{
abstract class QROutputAbstract implements QROutputInterface{
/**
* @var array
+54 -43
View File
@@ -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.'></'.$tag.'>';
}
if(!(bool)$this->options->htmlOmitEndTag){
$html .= '</'.$this->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.'></'.$tag.'>';
}
if(!(bool)$this->options->htmlOmitEndTag){
$html .= '</'.$this->options->htmlRowTag.'>';
}
$html .= PHP_EOL;
}
return $html;
}
}