refactoring

This commit is contained in:
smiley
2016-03-21 00:00:52 +01:00
parent 3cb15c94fb
commit 2c2f4b8bec
8 changed files with 93 additions and 76 deletions
+54 -43
View File
@@ -17,7 +17,7 @@ use chillerlan\QRCode\QRCode;
/**
*
*/
class QRString extends QROutputBase implements QROutputInterface{
class QRString extends QROutputAbstract{
/**
* @var \chillerlan\QRCode\Output\QRStringOptions
@@ -46,50 +46,61 @@ class QRString extends QROutputBase implements QROutputInterface{
*/
public function dump(){
if($this->options->type === QRCode::OUTPUT_STRING_JSON){
return json_encode($this->matrix);
}
else if($this->options->type === QRCode::OUTPUT_STRING_TEXT){
$text = '';
foreach($this->matrix as $row){
foreach($row as $col){
$text .= $col
? $this->options->textDark
: $this->options->textLight;
}
$text .= $this->options->textNewline;
}
return $text;
}
else if($this->options->type === QRCode::OUTPUT_STRING_HTML){
$html = '';
foreach($this->matrix as $row){
// in order to not bloat the output too much, we use the shortest possible valid HTML tags
$html .= '<'.$this->options->htmlRowTag.'>';
foreach($row as $col){
$tag = $col
? 'b' // dark
: 'i'; // light
$html .= '<'.$tag.'></'.$tag.'>';
}
if(!(bool)$this->options->htmlOmitEndTag){
$html .= '</'.$this->options->htmlRowTag.'>';
}
$html .= PHP_EOL;
}
return $html;
switch($this->options->type){
case QRCode::OUTPUT_STRING_JSON: return json_encode($this->matrix);
case QRCode::OUTPUT_STRING_TEXT: return $this->toString();
case QRCode::OUTPUT_STRING_HTML:
default:
return $this->toHTML();
}
}
/**
* @return string
*/
protected function toString(){
$str = '';
foreach($this->matrix as $row){
foreach($row as $col){
$str .= $col
? $this->options->textDark
: $this->options->textLight;
}
$str .= $this->options->textNewline;
}
return $str;
}
/**
* @return string
*/
protected function toHTML(){
$html = '';
foreach($this->matrix as $row){
// in order to not bloat the output too much, we use the shortest possible valid HTML tags
$html .= '<'.$this->options->htmlRowTag.'>';
foreach($row as $col){
$tag = $col
? 'b' // dark
: 'i'; // light
$html .= '<'.$tag.'></'.$tag.'>';
}
if(!(bool)$this->options->htmlOmitEndTag){
$html .= '</'.$this->options->htmlRowTag.'>';
}
$html .= PHP_EOL;
}
return $html;
}
}