<?php
/*
* (c) Jeroen van den Enden <info@endroid.nl>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Endroid\QrCode;
use Endroid\QrCode\Exception\InvalidPathException;
use Endroid\QrCode\Exception\InvalidWriterException;
use Endroid\QrCode\Exception\UnsupportedExtensionException;
use Endroid\QrCode\Writer\WriterInterface;
class QrCode implements QrCodeInterface
{
const LABEL_FONT_PATH_DEFAULT = __DIR__.'/../assets/noto_sans.otf';
protected $text;
protected $size = 300;
protected $margin = 10;
protected $foregroundColor = [
'r' => 0,
'g' => 0,
'b' => 0,
];
protected $backgroundColor = [
'r' => 255,
'g' => 255,
'b' => 255,
];
protected $encoding = 'UTF-8';
protected $errorCorrectionLevel;
protected $logoPath;
protected $logoWidth;
protected $label;
protected $labelFontSize = 16;
protected $labelFontPath = self::LABEL_FONT_PATH_DEFAULT;
protected $labelAlignment;
protected $labelMargin = [
't' => 0,
'r' => 10,
'b' => 10,
'l' => 10,
];
protected $writerRegistry;
protected $writer;
protected $validateResult = false;
public function __construct($text = '')
{
$this->text = $text;
$this->errorCorrectionLevel = new ErrorCorrectionLevel(ErrorCorrectionLevel::LOW);
$this->labelAlignment = new LabelAlignment(LabelAlignment::CENTER);
$this->writerRegistry = new StaticWriterRegistry();
}
public function setText($text)
{
$this->text = $text;
return $this;
}
public function getText()
{
return $this->text;
}
public function setSize($size)
{
$this->size = $size;
return $this;
}
public function getSize()
{
return $this->size;
}
public function setMargin($margin)
{
$this->margin = $margin;
return $this;
}
public function getMargin()
{
return $this->margin;
}
public function setForegroundColor($foregroundColor)
{
$this->foregroundColor = $foregroundColor;
return $this;
}
public function getForegroundColor()
{
return $this->foregroundColor;
}
public function setBackgroundColor($backgroundColor)
{
$this->backgroundColor = $backgroundColor;
return $this;
}
public function getBackgroundColor()
{
return $this->backgroundColor;
}
public function setEncoding($encoding)
{
$this->encoding = $encoding;
return $this;
}
public function getEncoding()
{
return $this->encoding;
}
public function setErrorCorrectionLevel($errorCorrectionLevel)
{
$this->errorCorrectionLevel = new ErrorCorrectionLevel($errorCorrectionLevel);
return $this;
}
public function getErrorCorrectionLevel()
{
return $this->errorCorrectionLevel->getValue();
}
public function setLogoPath($logoPath)
{
$logoPath = realpath($logoPath);
if (!is_file($logoPath)) {
throw new InvalidPathException('Invalid logo path: '.$logoPath);
}
$this->logoPath = $logoPath;
return $this;
}
public function getLogoPath()
{
return $this->logoPath;
}
public function setLogoWidth($logoWidth)
{
$this->logoWidth = $logoWidth;
return $this;
}
public function getLogoWidth()
{
return $this->logoWidth;
}
public function setLabel($label, $labelFontSize = null, $labelFontPath = null, $labelAlignment = null, $labelMargin = null)
{
$this->label = $label;
if (null !== $labelFontSize) {
$this->setLabelFontSize($labelFontSize);
}
if (null !== $labelFontPath) {
$this->setLabelFontPath($labelFontPath);
}
if (null !== $labelAlignment) {
$this->setLabelAlignment($labelAlignment);
}
if (null !== $labelMargin) {
$this->setLabelMargin($labelMargin);
}
return $this;
}
public function getLabel()
{
return $this->label;
}
public function setLabelFontSize($labelFontSize)
{
$this->labelFontSize = $labelFontSize;
return $this;
}
public function getLabelFontSize()
{
return $this->labelFontSize;
}
public function setLabelFontPath($labelFontPath)
{
$labelFontPath = realpath($labelFontPath);
if (!is_file($labelFontPath)) {
throw new InvalidPathException('Invalid label font path: '.$labelFontPath);
}
$this->labelFontPath = $labelFontPath;
return $this;
}
public function getLabelFontPath()
{
return $this->labelFontPath;
}
public function setLabelAlignment($labelAlignment)
{
$this->labelAlignment = new LabelAlignment($labelAlignment);
return $this;
}
public function getLabelAlignment()
{
return $this->labelAlignment->getValue();
}
public function setLabelMargin(array $labelMargin)
{
$this->labelMargin = array_merge($this->labelMargin, $labelMargin);
return $this;
}
public function getLabelMargin()
{
return $this->labelMargin;
}
public function setWriterRegistry(WriterRegistryInterface $writerRegistry)
{
$this->writerRegistry = $writerRegistry;
return $this;
}
public function setWriter(WriterInterface $writer)
{
$this->writer = $writer;
return $this;
}
public function getWriter($name = null)
{
if (!is_null($name)) {
return $this->writerRegistry->getWriter($name);
}
if ($this->writer instanceof WriterInterface) {
return $this->writer;
}
return $this->writerRegistry->getDefaultWriter();
}
public function setWriterByName($name)
{
$this->writer = $this->writerRegistry->getWriter($name);
return $this;
}
public function setWriterByPath($path)
{
$extension = pathinfo($path, PATHINFO_EXTENSION);
$this->setWriterByExtension($extension);
return $this;
}
public function setWriterByExtension($extension)
{
foreach ($this->writerRegistry->getWriters() as $writer) {
if ($writer->supportsExtension($extension)) {
$this->writer = $writer;
return $this;
}
}
throw new UnsupportedExtensionException('Missing writer for extension "'.$extension.'"');
}
public function setValidateResult($validateResult)
{
$this->validateResult = $validateResult;
return $this;
}
public function getValidateResult()
{
return $this->validateResult;
}
public function writeString()
{
return $this->getWriter()->writeString($this);
}
public function writeDataUri()
{
return $this->getWriter()->writeDataUri($this);
}
public function writeFile($path)
{
return $this->getWriter()->writeFile($this, $path);
}
public function getContentType()
{
return $this->getWriter()->getContentType();
}
}