* @copyright 2016 Smiley * @license MIT */ namespace chillerlan\QRCode\Output; use chillerlan\QRCode\QRCode; /** * */ class QRMarkup extends QROutputAbstract{ /** * @var string */ protected $optionsInterface = QRMarkupOptions::class; /** * @var array */ protected $types = [ QRCode::OUTPUT_MARKUP_HTML, QRCode::OUTPUT_MARKUP_SVG, ]; /** * @return string */ public function dump() { switch($this->options->type){ case QRCode::OUTPUT_MARKUP_SVG : return $this->toSVG(); case QRCode::OUTPUT_MARKUP_HTML: default: return $this->toHTML(); } } /** * @return string|bool */ 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 .= $this->options->eol; } if($this->options->cachefile){ $html = '
'.$this->options->eol.$html.''; return $this->saveToFile($html); } return $html; } /** * @link https://github.com/codemasher/php-qrcode/pull/5 * * @return string|bool */ protected function toSVG(){ $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2; $class = !empty($this->options->cssClass) ? $this->options->cssClass : hash('crc32', microtime(true)); // svg header $svg = ''.$this->options->eol; // if saving to file, append the correct headers if($this->options->cachefile){ $svg = ''.$this->options->eol.$svg; return $this->saveToFile($svg); } return $svg; } }