:octocat: simplified output mode selection

This commit is contained in:
codemasher
2018-01-21 14:50:27 +01:00
parent de1890855d
commit 79af50a6fa
4 changed files with 32 additions and 23 deletions
+1 -1
View File
@@ -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
+3 -10
View File
@@ -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();
+22 -2
View File
@@ -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);
}
+6 -10
View File
@@ -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());
}
}