smiley преди 9 години
родител
ревизия
2c2f4b8bec
променени са 8 файла, в които са добавени 80 реда и са изтрити 63 реда
  1. 1 1
      src/Data/AlphaNum.php
  2. 1 1
      src/Data/Byte.php
  3. 1 1
      src/Data/Kanji.php
  4. 1 1
      src/Data/Number.php
  5. 4 5
      src/Data/QRDataAbstract.php
  6. 28 21
      src/Output/QRImage.php
  7. 3 3
      src/Output/QROutputAbstract.php
  8. 41 30
      src/Output/QRString.php

+ 1 - 1
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 = [
 	const CHAR_MAP = [
 		36 => ' ',
 		36 => ' ',

+ 1 - 1
src/Data/Byte.php

@@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst;
 /**
 /**
  *
  *
  */
  */
-class Byte extends QRDataBase implements QRDataInterface{
+class Byte extends QRDataAbstract{
 
 
 	/**
 	/**
 	 * @var int
 	 * @var int

+ 1 - 1
src/Data/Kanji.php

@@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst;
 /**
 /**
  *
  *
  */
  */
-class Kanji extends QRDataBase implements QRDataInterface{
+class Kanji extends QRDataAbstract{
 
 
 	/**
 	/**
 	 * @var int
 	 * @var int

+ 1 - 1
src/Data/Number.php

@@ -18,7 +18,7 @@ use chillerlan\QRCode\QRConst;
 /**
 /**
  *
  *
  */
  */
-class Number extends QRDataBase implements QRDataInterface{
+class Number extends QRDataAbstract{
 
 
 	/**
 	/**
 	 * @var int
 	 * @var int

+ 4 - 5
src/Data/QRDataBase.php → src/Data/QRDataAbstract.php

@@ -1,8 +1,8 @@
 <?php
 <?php
 /**
 /**
- * Class QRDataBase
+ * Class QRDataAbstract
  *
  *
- * @filesource   QRDataBase.php
+ * @filesource   QRDataAbstract.php
  * @created      25.11.2015
  * @created      25.11.2015
  * @package      chillerlan\QRCode\Data
  * @package      chillerlan\QRCode\Data
  * @author       Smiley <smiley@chillerlan.net>
  * @author       Smiley <smiley@chillerlan.net>
@@ -11,12 +11,11 @@
  */
  */
 
 
 namespace chillerlan\QRCode\Data;
 namespace chillerlan\QRCode\Data;
-use chillerlan\QRCode\QRCode;
 
 
 /**
 /**
  *
  *
  */
  */
-class QRDataBase{
+abstract class QRDataAbstract implements QRDataInterface{
 
 
 	/**
 	/**
 	 * @var string
 	 * @var string
@@ -34,7 +33,7 @@ class QRDataBase{
 	protected $lengthBits = [0, 0, 0];
 	protected $lengthBits = [0, 0, 0];
 
 
 	/**
 	/**
-	 * QRDataBase constructor.
+	 * QRDataAbstract constructor.
 	 *
 	 *
 	 * @param string $data
 	 * @param string $data
 	 */
 	 */

+ 28 - 21
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
 	 * @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}));
 			$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
 	 * @return string
 	 */
 	 */
 	public function dump(){
 	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);
 		$foreground = imagecolorallocate($image, $this->options->fgRed, $this->options->fgGreen, $this->options->fgBlue);
 		$background = imagecolorallocate($image, $this->options->bgRed, $this->options->bgGreen, $this->options->bgBlue);
 		$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);
 			imagecolortransparent($image, $background);
 		}
 		}
 
 
@@ -89,9 +75,30 @@ class QRImage extends QROutputBase implements QROutputInterface{
 		ob_start();
 		ob_start();
 
 
 		switch($this->options->type){
 		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();
 		$imageData = ob_get_contents();

+ 3 - 3
src/Output/QROutputBase.php → src/Output/QROutputAbstract.php

@@ -1,8 +1,8 @@
 <?php
 <?php
 /**
 /**
- * Class QROutputBase
+ * Class QROutputAbstract
  *
  *
- * @filesource   QROutputBase.php
+ * @filesource   QROutputAbstract.php
  * @created      09.12.2015
  * @created      09.12.2015
  * @package      chillerlan\QRCode\Output
  * @package      chillerlan\QRCode\Output
  * @author       Smiley <smiley@chillerlan.net>
  * @author       Smiley <smiley@chillerlan.net>
@@ -15,7 +15,7 @@ namespace chillerlan\QRCode\Output;
 /**
 /**
  *
  *
  */
  */
-class QROutputBase{
+abstract class QROutputAbstract implements QROutputInterface{
 
 
 	/**
 	/**
 	 * @var array
 	 * @var array

+ 41 - 30
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
 	 * @var \chillerlan\QRCode\Output\QRStringOptions
@@ -46,50 +46,61 @@ class QRString extends QROutputBase implements QROutputInterface{
 	 */
 	 */
 	public function dump(){
 	public function dump(){
 
 
-		if($this->options->type === QRCode::OUTPUT_STRING_JSON){
-			return json_encode($this->matrix);
+		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();
 		}
 		}
 
 
-		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 string
+	 */
+	protected function toString(){
+		$str = '';
+
+		foreach($this->matrix as $row){
+			foreach($row as $col){
+				$str .= $col
+					? $this->options->textDark
+					: $this->options->textLight;
 			}
 			}
 
 
-			return $text;
+			$str .= $this->options->textNewline;
 		}
 		}
 
 
-		else if($this->options->type === QRCode::OUTPUT_STRING_HTML){
-			$html = '';
+		return $str;
+	}
 
 
-			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
+	/**
+	 * @return string
+	 */
+	protected function toHTML(){
+		$html = '';
 
 
-					$html .= '<'.$tag.'></'.$tag.'>';
-				}
+		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.'>';
 
 
-				if(!(bool)$this->options->htmlOmitEndTag){
-					$html .= '</'.$this->options->htmlRowTag.'>';
-				}
+			foreach($row as $col){
+				$tag = $col
+					? 'b'  // dark
+					: 'i'; // light
+
+				$html .= '<'.$tag.'></'.$tag.'>';
+			}
 
 
-				$html .= PHP_EOL;
+			if(!(bool)$this->options->htmlOmitEndTag){
+				$html .= '</'.$this->options->htmlRowTag.'>';
 			}
 			}
 
 
-			return $html;
+			$html .= PHP_EOL;
 		}
 		}
 
 
+		return $html;
 	}
 	}
 
 
 }
 }