diff --git a/src/Output/QRImage.php b/src/Output/QRImage.php index f642cf61c..7e3d2fd36 100644 --- a/src/Output/QRImage.php +++ b/src/Output/QRImage.php @@ -140,7 +140,7 @@ class QRImage extends QROutputAbstract{ ob_start(); try{ - call_user_func([$this, $this->options->outputType ?? QRCode::OUTPUT_IMAGE_PNG]); + call_user_func([$this, $this->outputMode ?? QRCode::OUTPUT_IMAGE_PNG]); } // not going to cover edge cases // @codeCoverageIgnoreStart diff --git a/src/Output/QRMarkup.php b/src/Output/QRMarkup.php index 425a1acdd..86a45a445 100644 --- a/src/Output/QRMarkup.php +++ b/src/Output/QRMarkup.php @@ -21,17 +21,10 @@ class QRMarkup extends QROutputAbstract{ /** * @return string - * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ public function dump(){ - if($this->options->cachefile !== null && !is_writable(dirname($this->options->cachefile))){ - throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile); - } - - $data = $this->options->outputType === QRCode::OUTPUT_MARKUP_HTML - ? $this->toHTML() - : $this->toSVG(); + $data = call_user_func([$this, $this->outputMode ?? QRCode::OUTPUT_MARKUP_SVG]); if($this->options->cachefile !== null){ $this->saveToFile($data); @@ -43,7 +36,7 @@ class QRMarkup extends QROutputAbstract{ /** * @return string|bool */ - protected function toHTML(){ + protected function html(){ $html = ''; foreach($this->matrix->matrix() as $row){ @@ -68,7 +61,7 @@ class QRMarkup extends QROutputAbstract{ * * @return string|bool */ - protected function toSVG(){ + protected function svg(){ $scale = $this->options->scale; $length = $this->moduleCount * $scale; $matrix = $this->matrix->matrix(); diff --git a/src/Output/QROutputAbstract.php b/src/Output/QROutputAbstract.php index 498b4bac6..c9f206f4e 100644 --- a/src/Output/QROutputAbstract.php +++ b/src/Output/QROutputAbstract.php @@ -12,7 +12,9 @@ namespace chillerlan\QRCode\Output; -use chillerlan\QRCode\{Data\QRMatrix, QROptions}; +use chillerlan\QRCode\{ + Data\QRMatrix, QRCode, QROptions +}; /** * @@ -34,6 +36,11 @@ abstract class QROutputAbstract implements QROutputInterface{ */ protected $options; + /** + * @var string + */ + protected $outputMode; + /** * QROutputAbstract constructor. * @@ -44,16 +51,29 @@ abstract class QROutputAbstract implements QROutputInterface{ $this->options = $options; $this->matrix = $matrix; $this->moduleCount = $this->matrix->size(); + + $class = get_called_class(); + + if(array_key_exists($class, QRCode::OUTPUT_MODES) && in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){ + $this->outputMode = $this->options->outputType; + } + } /** * @see file_put_contents() * * @param string $data - + * * @return bool|int + * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ protected function saveToFile(string $data) { + + if(!is_writable(dirname($this->options->cachefile))){ + throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile); + } + return file_put_contents($this->options->cachefile, $data); } diff --git a/src/Output/QRString.php b/src/Output/QRString.php index 8cb367a24..093722112 100644 --- a/src/Output/QRString.php +++ b/src/Output/QRString.php @@ -23,17 +23,9 @@ class QRString extends QROutputAbstract{ * @return string */ public function dump():string{ - - $data = $this->options->outputType === QRCode::OUTPUT_STRING_JSON - ? json_encode($this->matrix->matrix()) - : $this->toString(); + $data = call_user_func([$this, $this->outputMode ?? QRCode::OUTPUT_STRING_TEXT]); if($this->options->cachefile !== null){ - - if(!is_writable(dirname($this->options->cachefile))){ - throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile); - } - $this->saveToFile($data); } @@ -43,7 +35,7 @@ class QRString extends QROutputAbstract{ /** * @return string */ - protected function toString():string{ + protected function text():string{ $str = []; foreach($this->matrix->matrix() as $row){ @@ -68,4 +60,8 @@ class QRString extends QROutputAbstract{ return implode($this->options->eol, $str); } + protected function json(){ + return json_encode($this->matrix->matrix()); + } + }