Skip to content

Commit

Permalink
Further refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Dec 2, 2017
1 parent d34057a commit 99f89f7
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 82 deletions.
28 changes: 18 additions & 10 deletions src/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public function __construct(string $text = '')

$this->errorCorrectionLevel = new ErrorCorrectionLevel(ErrorCorrectionLevel::LOW);
$this->labelAlignment = new LabelAlignment(LabelAlignment::CENTER);

$this->writerRegistry = new StaticWriterRegistry();
}

public function setText(string $text): void
Expand Down Expand Up @@ -147,7 +145,7 @@ public function setLogoPath(string $logoPath): void
$this->logoPath = $logoPath;
}

public function getLogoPath(): string
public function getLogoPath(): ?string
{
return $this->logoPath;
}
Expand All @@ -157,7 +155,7 @@ public function setLogoWidth(int $logoWidth): void
$this->logoWidth = $logoWidth;
}

public function getLogoWidth(): int
public function getLogoWidth(): ?int
{
return $this->logoWidth;
}
Expand All @@ -183,7 +181,7 @@ public function setLabel(string $label, int $labelFontSize = null, string $label
}
}

public function getLabel(): string
public function getLabel(): ?string
{
return $this->label;
}
Expand All @@ -193,7 +191,7 @@ public function setLabelFontSize(int $labelFontSize): void
$this->labelFontSize = $labelFontSize;
}

public function getLabelFontSize(): int
public function getLabelFontSize(): ?int
{
return $this->labelFontSize;
}
Expand All @@ -209,7 +207,7 @@ public function setLabelFontPath(string $labelFontPath): void
$this->labelFontPath = $labelFontPath;
}

public function getLabelFontPath(): string
public function getLabelFontPath(): ?string
{
return $this->labelFontPath;
}
Expand All @@ -219,7 +217,7 @@ public function setLabelAlignment(string $labelAlignment): void
$this->labelAlignment = new LabelAlignment($labelAlignment);
}

public function getLabelAlignment(): string
public function getLabelAlignment(): ?string
{
return $this->labelAlignment->getValue();
}
Expand All @@ -229,7 +227,7 @@ public function setLabelMargin(array $labelMargin): void
$this->labelMargin = array_merge($this->labelMargin, $labelMargin);
}

public function getLabelMargin(): array
public function getLabelMargin(): ?array
{
return $this->labelMargin;
}
Expand All @@ -246,6 +244,10 @@ public function setWriter(WriterInterface $writer): void

public function getWriter(string $name = null): WriterInterface
{
if (!$this->writerRegistry instanceof WriterRegistryInterface) {
$this->createWriterRegistry();
}

if (!is_null($name)) {
return $this->writerRegistry->getWriter($name);
}
Expand All @@ -257,9 +259,15 @@ public function getWriter(string $name = null): WriterInterface
return $this->writerRegistry->getDefaultWriter();
}

private function createWriterRegistry()
{
$this->writerRegistry = new WriterRegistry();
$this->writerRegistry->loadDefaultWriters();
}

public function setWriterByName(string $name)
{
$this->writer = $this->writerRegistry->getWriter($name);
$this->writer = $this->getWriter($name);
}

public function setWriterByPath(string $path): void
Expand Down
35 changes: 19 additions & 16 deletions src/QrCodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@

interface QrCodeInterface
{
public function getText(): string;
public function getSize(): int;
public function getMargin(): int;
public function getForegroundColor(): array;
public function getBackgroundColor(): array;
public function getEncoding(): string;
public function getErrorCorrectionLevel(): string;
public function getLogoPath(): string;
public function getLogoWidth(): int;
public function getLabel(): string;
public function getLabelFontPath(): string;
public function getLabelFontSize(): int;
public function getLabelAlignment(): string;
public function getLabelMargin(): array;
public function getValidateResult(): bool;
public function setWriterRegistry(WriterRegistryInterface $writerRegistry): void;
function getText(): string;
function getSize(): int;
function getMargin(): int;
function getForegroundColor(): array;
function getBackgroundColor(): array;
function getEncoding(): string;
function getErrorCorrectionLevel(): string;
function getLogoPath(): ?string;
function getLogoWidth(): ?int;
function getLabel(): ?string;
function getLabelFontPath(): ?string;
function getLabelFontSize(): ?int;
function getLabelAlignment(): ?string;
function getLabelMargin(): ?array;
function getValidateResult(): bool;
function setWriterRegistry(WriterRegistryInterface $writerRegistry): void;
function writeString(): string;
function writeDataUri(): string;
function writeFile(string $path): string;
}
41 changes: 0 additions & 41 deletions src/StaticWriterRegistry.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/Writer/PngWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function writeString(QrCodeInterface $qrCode): string
return $string;
}

protected function addMargin(resource $sourceImage, int $margin, int $size, array $foregroundColor, array $backgroundColor): resource
protected function addMargin($sourceImage, int $margin, int $size, array $foregroundColor, array $backgroundColor)
{
$additionalWhitespace = $this->calculateAdditionalWhiteSpace($sourceImage, $foregroundColor);

Expand All @@ -72,7 +72,7 @@ protected function addMargin(resource $sourceImage, int $margin, int $size, arra
return $targetImage;
}

protected function calculateAdditionalWhiteSpace(resource $image, array $foregroundColor): int
protected function calculateAdditionalWhiteSpace($image, array $foregroundColor): int
{
$width = imagesx($image);
$height = imagesy($image);
Expand All @@ -93,7 +93,7 @@ protected function calculateAdditionalWhiteSpace(resource $image, array $foregro
return $whitespace;
}

protected function addLogo(resource $sourceImage, string $logoPath, int $logoWidth = null): resource
protected function addLogo($sourceImage, string $logoPath, int $logoWidth = null)
{
$logoImage = imagecreatefromstring(file_get_contents($logoPath));
$logoSourceWidth = imagesx($logoImage);
Expand All @@ -115,7 +115,7 @@ protected function addLogo(resource $sourceImage, string $logoPath, int $logoWid
return $sourceImage;
}

protected function addLabel(resource $sourceImage, string $label, string $labelFontPath, int $labelFontSize, string $labelAlignment, array $labelMargin, array $foregroundColor, array $backgroundColor): resource
protected function addLabel($sourceImage, string $label, string $labelFontPath, int $labelFontSize, string $labelAlignment, array $labelMargin, array $foregroundColor, array $backgroundColor)
{
if (!function_exists('imagettfbbox')) {
throw new MissingFunctionException('Missing function "imagettfbbox", please make sure you installed the FreeType library');
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function addLabel(resource $sourceImage, string $label, string $labelF
return $targetImage;
}

protected function imageToString(resource $image): string
protected function imageToString($image): string
{
ob_start();
imagepng($image);
Expand Down
14 changes: 7 additions & 7 deletions src/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

interface WriterInterface
{
public function writeString(QrCodeInterface $qrCode): string;
public function writeDataUri(QrCodeInterface $qrCode): string;
public function writeFile(QrCodeInterface $qrCode, string $path): string;
public static function getContentType(): string;
public static function supportsExtension(string $extension): bool;
public static function getSupportedExtensions(): array;
public function getName(): string;
function writeString(QrCodeInterface $qrCode): string;
function writeDataUri(QrCodeInterface $qrCode): string;
function writeFile(QrCodeInterface $qrCode, string $path): string;
static function getContentType(): string;
static function supportsExtension(string $extension): bool;
static function getSupportedExtensions(): array;
function getName(): string;
}
20 changes: 20 additions & 0 deletions src/WriterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,33 @@
namespace Endroid\QrCode;

use Endroid\QrCode\Exception\InvalidWriterException;
use Endroid\QrCode\Writer\BinaryWriter;
use Endroid\QrCode\Writer\DebugWriter;
use Endroid\QrCode\Writer\EpsWriter;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use Endroid\QrCode\Writer\WriterInterface;

class WriterRegistry implements WriterRegistryInterface
{
private $writers = [];
private $defaultWriter;

public function loadDefaultWriters(): void
{
if (count($this->writers) > 0) {
return;
}

$this->addWriter(new BinaryWriter());
$this->addWriter(new DebugWriter());
$this->addWriter(new EpsWriter());
$this->addWriter(new PngWriter());
$this->addWriter(new SvgWriter());

$this->setDefaultWriter('png');
}

public function addWriter(WriterInterface $writer): void
{
$this->writers[$writer->getName()] = $writer;
Expand Down
6 changes: 3 additions & 3 deletions src/WriterRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

interface WriterRegistryInterface
{
public function addWriter(WriterInterface $writer): void;
public function getWriter(string $name): WriterInterface;
public function getWriters(): array;
function addWriter(WriterInterface $writer): void;
function getWriter(string $name): WriterInterface;
function getWriters(): array;
}

0 comments on commit 99f89f7

Please sign in to comment.