smiley 10 anni fa
parent
commit
e1ae12a533
4 ha cambiato i file con 146 aggiunte e 10 eliminazioni
  1. 13 10
      examples/custom.php
  2. 100 0
      src/Output/QRString.php
  3. 29 0
      src/Output/QRStringOptions.php
  4. 4 0
      src/QRConst.php

+ 13 - 10
examples/custom.php

@@ -5,24 +5,27 @@ require_once '../vendor/autoload.php';
 use chillerlan\QRCode\QRCode;
 use chillerlan\QRCode\QRCode;
 use chillerlan\QRCode\QRConst;
 use chillerlan\QRCode\QRConst;
 use chillerlan\QRCode\QROptions;
 use chillerlan\QRCode\QROptions;
+use chillerlan\QRCode\Output\QRString;
+use chillerlan\QRCode\Output\QRStringOptions;
 
 
 $starttime = microtime(true);
 $starttime = microtime(true);
 
 
+$qrStringOptions = new QRStringOptions;
+$qrStringOptions->type = QRConst::OUTPUT_STRING_TEXT;
+$qrStringOptions->textDark  = '+';
+$qrStringOptions->textLight = '-';
+
 $qrOptions = new QROptions;
 $qrOptions = new QROptions;
 $qrOptions->typeNumber = QRConst::TYPE_05;
 $qrOptions->typeNumber = QRConst::TYPE_05;
 $qrOptions->errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M;
 $qrOptions->errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M;
+$qrOptions->output = new QRString($qrStringOptions);
 
 
 // google authenticator
 // 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
 // https://chart.googleapis.com/chart?chs=200x200&chld=M%7C0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2Ftest%3Fsecret%3DB3JX4VCVJDVNXNZ5%26issuer%3Dchillerlan.net
-$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++){
-		echo $qrcode->matrix[$row][$col] ? '*' : ' ';
-	}
-	echo PHP_EOL;
-}
+$qrcode = new QRCode('otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=buildwars.net', $qrOptions);
+var_dump($qrcode->getRawData());
+
+$qrcode->setData('otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net', $qrOptions);
+var_dump($qrcode->output());
 
 
 echo 'QRCode: '.round((microtime(true)-$starttime), 5).PHP_EOL;
 echo 'QRCode: '.round((microtime(true)-$starttime), 5).PHP_EOL;

+ 100 - 0
src/Output/QRString.php

@@ -0,0 +1,100 @@
+<?php
+/**
+ * Class QRString
+ *
+ * @filesource   QRString.php
+ * @created      05.12.2015
+ * @package      chillerlan\QRCode\Output
+ * @author       Smiley <smiley@chillerlan.net>
+ * @copyright    2015 Smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCode\Output;
+use chillerlan\QRCode\QRConst;
+
+/**
+ *
+ */
+class QRString extends QROutputBase implements QROutputInterface{
+
+	/**
+	 * @var \chillerlan\QRCode\Output\QRStringOptions
+	 */
+	protected $options;
+
+	/**
+	 * @var \chillerlan\QRCode\Output\QRStringOptions $outputOptions
+	 */
+	public function __construct(QRStringOptions $outputOptions = null){
+		$this->options = $outputOptions;
+
+		if(!$this->options instanceof QRStringOptions){
+			$this->options = new QRStringOptions;
+		}
+
+	}
+
+	/**
+	 * @return string
+	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
+	 */
+	public function dump(){
+
+		switch($this->options->type){
+			case QRConst::OUTPUT_STRING_TEXT: return $this->toText();
+			case QRConst::OUTPUT_STRING_JSON: return $this->toJSON();
+			case QRConst::OUTPUT_STRING_HTML: return $this->toHTML();
+			default:
+				throw new QRCodeOutputException('Invalid string output type!');
+		}
+
+	}
+
+	/**
+	 * @return string
+	 */
+	public function toText(){
+		$text = '';
+
+		for($row = 0; $row < $this->pixelCount ; $row++){
+			for($col = 0; $col < $this->pixelCount; $col++){
+				$text .= $this->matrix[$row][$col] ? $this->options->textDark : $this->options->textLight;
+			}
+			$text .= $this->options->textNewline;
+		}
+
+		return $text;
+	}
+
+	/**
+	 * @return string
+	 */
+	public function toJSON(){
+		return json_encode($this->matrix);
+	}
+
+	/**
+	 * @return string
+	 */
+	public function toHTML(){
+		$html = '';
+
+		for($row = 0; $row < $this->pixelCount; $row++){
+			$html .= '<p>';
+
+			for($col = 0; $col < $this->pixelCount; $col++){
+				$tag = $this->matrix[$row][$col]
+					? 'b'  // dark
+					: 'i'; // light
+
+				$html .= '<'.$tag.'></'.$tag.'>';
+			}
+
+			$html .= '</p>'.PHP_EOL;
+		}
+
+		return $html;
+	}
+
+}

+ 29 - 0
src/Output/QRStringOptions.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * Class QRStringOptions
+ *
+ * @filesource   QRStringOptions.php
+ * @created      08.12.2015
+ * @package      chillerlan\QRCode\Output
+ * @author       Smiley <smiley@chillerlan.net>
+ * @copyright    2015 Smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCode\Output;
+use chillerlan\QRCode\QRConst;
+
+/**
+ *
+ */
+class QRStringOptions{
+
+	public $type = QRConst::OUTPUT_STRING_TEXT;
+
+	public $textDark = '#';
+
+	public $textLight = ' ';
+
+	public $textNewline = PHP_EOL;
+
+}

+ 4 - 0
src/QRConst.php

@@ -17,6 +17,10 @@ namespace chillerlan\QRCode;
  */
  */
 class QRConst{
 class QRConst{
 
 
+	const OUTPUT_STRING_TEXT = 0;
+	const OUTPUT_STRING_JSON = 1;
+	const OUTPUT_STRING_HTML = 2;
+
 	const ERROR_CORRECT_LEVEL_L = 1; // 7%.
 	const ERROR_CORRECT_LEVEL_L = 1; // 7%.
 	const ERROR_CORRECT_LEVEL_M = 0; // 15%.
 	const ERROR_CORRECT_LEVEL_M = 0; // 15%.
 	const ERROR_CORRECT_LEVEL_Q = 3; // 25%.
 	const ERROR_CORRECT_LEVEL_Q = 3; // 25%.