* @copyright 2016 Smiley
* @license MIT
*/
namespace chillerlan\QRCode\Output;
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\QRCode\QRCode;
use function implode, is_string, ksort, sprintf, strip_tags, trim;
/**
* Converts the matrix into markup types: HTML, SVG, ...
*/
class QRMarkup extends QROutputAbstract{
protected string $defaultMode = QRCode::OUTPUT_MARKUP_SVG;
/**
* @inheritDoc
*/
protected function setModuleValues():void{
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
$v = $this->options->moduleValues[$M_TYPE] ?? null;
if(!is_string($v)){
$this->moduleValues[$M_TYPE] = $defaultValue
? $this->options->markupDark
: $this->options->markupLight;
}
else{
$this->moduleValues[$M_TYPE] = trim(strip_tags($v), " '\"\r\n\t");
}
}
}
/**
* HTML output
*/
protected function html(string $file = null):string{
$html = empty($this->options->cssClass)
? '
'
: sprintf('
', $this->options->cssClass);
$html .= $this->options->eol;
foreach($this->matrix->matrix() as $row){
$html .= '
';
foreach($row as $M_TYPE){
$html .= sprintf('', $this->moduleValues[$M_TYPE]);
}
$html .= '
'.$this->options->eol;
}
$html .= '
'.$this->options->eol;
if($file !== null){
return sprintf(
'
QR Code%s',
$this->options->eol.$html
);
}
return $html;
}
/**
* SVG output
*
* @see https://github.com/codemasher/php-qrcode/pull/5
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg
* @see https://www.sarasoueidan.com/demos/interactive-svg-coordinate-system/
*/
protected function svg(string $file = null):string{
$svg = $this->svgHeader();
if(!empty($this->options->svgDefs)){
$svg .= sprintf('
%1$s%2$s%2$s', $this->options->svgDefs, $this->options->eol);
}
$svg .= $this->svgPaths();
// close svg
$svg .= sprintf('%1$s%1$s', $this->options->eol);
// transform to data URI only when not saving to file
if($file === null && $this->options->imageBase64){
$svg = $this->base64encode($svg, 'image/svg+xml');
}
return $svg;
}
/**
* returns the