diff --git a/src/Output/QROutputBase.php b/src/Output/QROutputBase.php new file mode 100644 index 000000000..1edc083c6 --- /dev/null +++ b/src/Output/QROutputBase.php @@ -0,0 +1,57 @@ + + * @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; + } + +} diff --git a/src/Output/QROutputInterface.php b/src/Output/QROutputInterface.php index 6c7bd3b45..2d9d9a409 100644 --- a/src/Output/QROutputInterface.php +++ b/src/Output/QROutputInterface.php @@ -4,17 +4,31 @@ * * @filesource QROutputInterface.php * @created 02.12.2015 - * @package codemasher\QRCode\Output + * @package chillerlan\QRCode\Output * @author Smiley * @copyright 2015 Smiley * @license MIT */ -namespace codemasher\QRCode\Output; +namespace chillerlan\QRCode\Output; /** * */ 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); + } diff --git a/src/QRCode.php b/src/QRCode.php index 6cfbaab3e..6dd4de611 100644 --- a/src/QRCode.php +++ b/src/QRCode.php @@ -12,20 +12,22 @@ namespace chillerlan\QRCode; +use chillerlan\QRCode\Output\QROutputInterface; + /** * @link https://github.com/kazuhikoarase/qrcode-generator/tree/master/php */ class QRCode{ - /** - * @var int - */ - public $pixelCount = 0; - /** * @var array */ - public $matrix = []; + protected $matrix = []; + + /** + * @var int + */ + protected $pixelCount = 0; /** * @var int @@ -37,15 +39,20 @@ class QRCode{ */ protected $errorCorrectLevel; + /** + * @var \chillerlan\QRCode\BitBuffer + */ + protected $bitBuffer; + /** * @var \chillerlan\QRCode\Data\QRDataInterface */ protected $qrDataInterface; /** - * @var \chillerlan\QRCode\BitBuffer + * @var \chillerlan\QRCode\Output\QROutputInterface */ - protected $bitBuffer; + protected $qrOutputInterface; /** * QRCode constructor. @@ -55,28 +62,19 @@ class QRCode{ * * @throws \chillerlan\QRCode\QRCodeException */ - public function __construct($data = null, QROptions $options = null){ + public function __construct($data, QROptions $options){ $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 \chillerlan\QRCode\QROptions $options * * @return $this * @throws \chillerlan\QRCode\QRCodeException */ - public function setOptions(QROptions $options, $data){ + public function setData($data, QROptions $options){ $data = trim($data); if(empty($data)){ @@ -87,6 +85,12 @@ class QRCode{ 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); $qrDataInterface = __NAMESPACE__.'\\Data\\'.[ @@ -116,11 +120,20 @@ class QRCode{ } + return $this; } /** - * @return $this + * @return mixed + */ + public function output(){ + $this->qrOutputInterface->setMatrix($this->getRawData()); + return $this->qrOutputInterface->dump(); + } + + /** + * @return array */ public function getRawData(){ $minLostPoint = 0; @@ -239,7 +252,7 @@ class QRCode{ $this->getMatrix(false, $pattern); - return $this; + return $this->matrix; } /** diff --git a/src/QROptions.php b/src/QROptions.php index a1d8ed4d6..6d410059b 100644 --- a/src/QROptions.php +++ b/src/QROptions.php @@ -17,6 +17,13 @@ namespace chillerlan\QRCode; */ class QROptions{ + /** + * mandatory + * + * @var \chillerlan\QRCode\Output\QROutputInterface + */ + public $output = null ; + /** * @var int */ @@ -27,14 +34,4 @@ class QROptions{ */ public $typeNumber = null; - /** - * @var \chillerlan\QRCode\Output\QROutputInterface - */ - public $output = null ; - - /** - * @var \chillerlan\QRCode\Output\QROutputOptionsInterface - */ - public $outputOptions = null; - }