Ver Fonte

added QROutput*

smiley há 10 anos atrás
pai
commit
4dde390d7e
4 ficheiros alterados com 114 adições e 33 exclusões
  1. 57 0
      src/Output/QROutputBase.php
  2. 16 2
      src/Output/QROutputInterface.php
  3. 34 21
      src/QRCode.php
  4. 7 10
      src/QROptions.php

+ 57 - 0
src/Output/QROutputBase.php

@@ -0,0 +1,57 @@
+<?php
+/**
+ * Class QROutputBase
+ *
+ * @filesource   QROutputBase.php
+ * @created      09.12.2015
+ * @package      chillerlan\QRCode\Output
+ * @author       Smiley <smiley@chillerlan.net>
+ * @copyright    2015 Smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCode\Output;
+
+/**
+ *
+ */
+class QROutputBase{
+
+	/**
+	 * @var array
+	 */
+	protected $matrix;
+
+	/**
+	 * @var int
+	 */
+	protected $pixelCount;
+
+	/**
+	 * @var object
+	 */
+	protected $options;
+
+	/**
+	 * @param array $matrix
+	 *
+	 * @return $this
+	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
+	 */
+	public function setMatrix(array $matrix){
+		$this->pixelCount = count($matrix);
+
+		// todo: specify valid range
+		if($this->pixelCount < 2
+			|| !isset($matrix[$this->pixelCount - 1])
+			|| $this->pixelCount !== count($matrix[$this->pixelCount - 1])
+		){
+			throw new QRCodeOutputException('Invalid matrix!');
+		}
+
+		$this->matrix = $matrix;
+
+		return $this;
+	}
+
+}

+ 16 - 2
src/Output/QROutputInterface.php

@@ -4,17 +4,31 @@
  *
  *
  * @filesource   QROutputInterface.php
  * @filesource   QROutputInterface.php
  * @created      02.12.2015
  * @created      02.12.2015
- * @package      codemasher\QRCode\Output
+ * @package      chillerlan\QRCode\Output
  * @author       Smiley <smiley@chillerlan.net>
  * @author       Smiley <smiley@chillerlan.net>
  * @copyright    2015 Smiley
  * @copyright    2015 Smiley
  * @license      MIT
  * @license      MIT
  */
  */
 
 
-namespace codemasher\QRCode\Output;
+namespace chillerlan\QRCode\Output;
 
 
 /**
 /**
  *
  *
  */
  */
 interface QROutputInterface{
 interface QROutputInterface{
 
 
+	/**
+	 * @return mixed
+	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
+	 */
+	public function dump();
+
+	/**
+	 * @param array $matrix
+	 *
+	 * @return $this
+	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
+	 */
+	public function setMatrix(array $matrix);
+
 }
 }

+ 34 - 21
src/QRCode.php

@@ -12,20 +12,22 @@
 
 
 namespace chillerlan\QRCode;
 namespace chillerlan\QRCode;
 
 
+use chillerlan\QRCode\Output\QROutputInterface;
+
 /**
 /**
  * @link https://github.com/kazuhikoarase/qrcode-generator/tree/master/php
  * @link https://github.com/kazuhikoarase/qrcode-generator/tree/master/php
  */
  */
 class QRCode{
 class QRCode{
 
 
 	/**
 	/**
-	 * @var int
+	 * @var array
 	 */
 	 */
-	public $pixelCount = 0;
+	protected $matrix = [];
 
 
 	/**
 	/**
-	 * @var array
+	 * @var int
 	 */
 	 */
-	public $matrix = [];
+	protected $pixelCount = 0;
 
 
 	/**
 	/**
 	 * @var int
 	 * @var int
@@ -37,15 +39,20 @@ class QRCode{
 	 */
 	 */
 	protected $errorCorrectLevel;
 	protected $errorCorrectLevel;
 
 
+	/**
+	 * @var \chillerlan\QRCode\BitBuffer
+	 */
+	protected $bitBuffer;
+
 	/**
 	/**
 	 * @var \chillerlan\QRCode\Data\QRDataInterface
 	 * @var \chillerlan\QRCode\Data\QRDataInterface
 	 */
 	 */
 	protected $qrDataInterface;
 	protected $qrDataInterface;
 
 
 	/**
 	/**
-	 * @var \chillerlan\QRCode\BitBuffer
+	 * @var \chillerlan\QRCode\Output\QROutputInterface
 	 */
 	 */
-	protected $bitBuffer;
+	protected $qrOutputInterface;
 
 
 	/**
 	/**
 	 * QRCode constructor.
 	 * QRCode constructor.
@@ -55,28 +62,19 @@ class QRCode{
 	 *
 	 *
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
 	 */
-	public function __construct($data = null, QROptions $options = null){
+	public function __construct($data, QROptions $options){
 		$this->bitBuffer = new BitBuffer;
 		$this->bitBuffer = new BitBuffer;
-
-		if($data){
-
-			if(!$options instanceof QROptions){
-				$options = new QROptions;
-			}
-
-			$this->setOptions($options, $data);
-		}
-
+		$this->setData($data, $options);
 	}
 	}
 
 
 	/**
 	/**
-	 * @param \chillerlan\QRCode\QROptions $options
 	 * @param string                       $data
 	 * @param string                       $data
+	 * @param \chillerlan\QRCode\QROptions $options
 	 *
 	 *
 	 * @return $this
 	 * @return $this
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
 	 */
-	public function setOptions(QROptions $options, $data){
+	public function setData($data, QROptions $options){
 		$data = trim($data);
 		$data = trim($data);
 
 
 		if(empty($data)){
 		if(empty($data)){
@@ -87,6 +85,12 @@ class QRCode{
 			throw new QRCodeException('Invalid error correct level: '.$options->errorCorrectLevel);
 			throw new QRCodeException('Invalid error correct level: '.$options->errorCorrectLevel);
 		}
 		}
 
 
+		if(!$options->output instanceof QROutputInterface){
+			throw new QRCodeException('$options->output is no instance of QROutputInterface');
+		}
+
+		$this->qrOutputInterface = $options->output;
+
 		$mode = Util::getMode($data);
 		$mode = Util::getMode($data);
 
 
 		$qrDataInterface = __NAMESPACE__.'\\Data\\'.[
 		$qrDataInterface = __NAMESPACE__.'\\Data\\'.[
@@ -116,11 +120,20 @@ class QRCode{
 
 
 		}
 		}
 
 
+
 		return $this;
 		return $this;
 	}
 	}
 
 
 	/**
 	/**
-	 * @return $this
+	 * @return mixed
+	 */
+	public function output(){
+		$this->qrOutputInterface->setMatrix($this->getRawData());
+		return $this->qrOutputInterface->dump();
+	}
+
+	/**
+	 * @return array
 	 */
 	 */
 	public function getRawData(){
 	public function getRawData(){
 		$minLostPoint = 0;
 		$minLostPoint = 0;
@@ -239,7 +252,7 @@ class QRCode{
 
 
 		$this->getMatrix(false, $pattern);
 		$this->getMatrix(false, $pattern);
 
 
-		return $this;
+		return $this->matrix;
 	}
 	}
 
 
 	/**
 	/**

+ 7 - 10
src/QROptions.php

@@ -18,23 +18,20 @@ namespace chillerlan\QRCode;
 class QROptions{
 class QROptions{
 
 
 	/**
 	/**
-	 * @var int
+	 * mandatory
+	 *
+	 * @var \chillerlan\QRCode\Output\QROutputInterface
 	 */
 	 */
-	public $errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M;
+	public $output = null ;
 
 
 	/**
 	/**
 	 * @var int
 	 * @var int
 	 */
 	 */
-	public $typeNumber = null;
-
-	/**
-	 * @var \chillerlan\QRCode\Output\QROutputInterface
-	 */
-	public $output = null ;
+	public $errorCorrectLevel = QRConst::ERROR_CORRECT_LEVEL_M;
 
 
 	/**
 	/**
-	 * @var \chillerlan\QRCode\Output\QROutputOptionsInterface
+	 * @var int
 	 */
 	 */
-	public $outputOptions = null;
+	public $typeNumber = null;
 
 
 }
 }