fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Alignment;
final class LabelAlignmentCenter implements LabelAlignmentInterface
{
}

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Alignment;
interface LabelAlignmentInterface
{
}

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Alignment;
final class LabelAlignmentLeft implements LabelAlignmentInterface
{
}

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Alignment;
final class LabelAlignmentRight implements LabelAlignmentInterface
{
}

View File

@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Font;
final class Font implements FontInterface
{
private string $path;
private int $size;
public function __construct(string $path, int $size = 16)
{
$this->validatePath($path);
$this->path = $path;
$this->size = $size;
}
private function validatePath(string $path): void
{
if (!file_exists($path)) {
throw new \Exception(sprintf('Invalid font path "%s"', $path));
}
}
public function getPath(): string
{
return $this->path;
}
public function getSize(): int
{
return $this->size;
}
}

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Font;
interface FontInterface
{
public function getPath(): string;
public function getSize(): int;
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Font;
final class NotoSans implements FontInterface
{
private int $size;
public function __construct(int $size = 16)
{
$this->size = $size;
}
public function getPath(): string
{
return __DIR__.'/../../../assets/noto_sans.otf';
}
public function getSize(): int
{
return $this->size;
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Font;
final class OpenSans implements FontInterface
{
private int $size;
public function __construct(int $size = 16)
{
$this->size = $size;
}
public function getPath(): string
{
return __DIR__.'/../../../assets/open_sans.ttf';
}
public function getSize(): int
{
return $this->size;
}
}

View File

@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
use Endroid\QrCode\Label\Font\Font;
use Endroid\QrCode\Label\Font\FontInterface;
use Endroid\QrCode\Label\Margin\Margin;
use Endroid\QrCode\Label\Margin\MarginInterface;
final class Label implements LabelInterface
{
private string $text;
private FontInterface $font;
private LabelAlignmentInterface $alignment;
private MarginInterface $margin;
private ColorInterface $textColor;
public function __construct(
string $text,
FontInterface $font = null,
LabelAlignmentInterface $alignment = null,
MarginInterface $margin = null,
ColorInterface $textColor = null
) {
$this->text = $text;
$this->font = $font ?? new Font(__DIR__.'/../../assets/noto_sans.otf', 16);
$this->alignment = $alignment ?? new LabelAlignmentCenter();
$this->margin = $margin ?? new Margin(0, 10, 10, 10);
$this->textColor = $textColor ?? new Color(0, 0, 0);
}
public static function create(string $text): self
{
return new self($text);
}
public function getText(): string
{
return $this->text;
}
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
public function getFont(): FontInterface
{
return $this->font;
}
public function setFont(FontInterface $font): self
{
$this->font = $font;
return $this;
}
public function getAlignment(): LabelAlignmentInterface
{
return $this->alignment;
}
public function setAlignment(LabelAlignmentInterface $alignment): self
{
$this->alignment = $alignment;
return $this;
}
public function getMargin(): MarginInterface
{
return $this->margin;
}
public function setMargin(MarginInterface $margin): self
{
$this->margin = $margin;
return $this;
}
public function getTextColor(): ColorInterface
{
return $this->textColor;
}
public function setTextColor(ColorInterface $textColor): self
{
$this->textColor = $textColor;
return $this;
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label;
use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
use Endroid\QrCode\Label\Font\FontInterface;
use Endroid\QrCode\Label\Margin\MarginInterface;
interface LabelInterface
{
public function getText(): string;
public function getFont(): FontInterface;
public function getAlignment(): LabelAlignmentInterface;
public function getMargin(): MarginInterface;
public function getTextColor(): ColorInterface;
}

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Margin;
final class Margin implements MarginInterface
{
private int $top;
private int $right;
private int $bottom;
private int $left;
public function __construct(int $top, int $right, int $bottom, int $left)
{
$this->top = $top;
$this->right = $right;
$this->bottom = $bottom;
$this->left = $left;
}
public function getTop(): int
{
return $this->top;
}
public function getRight(): int
{
return $this->right;
}
public function getBottom(): int
{
return $this->bottom;
}
public function getLeft(): int
{
return $this->left;
}
/** @return array<string, int> */
public function toArray(): array
{
return [
'top' => $this->top,
'right' => $this->right,
'bottom' => $this->bottom,
'left' => $this->left,
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Label\Margin;
interface MarginInterface
{
public function getTop(): int;
public function getRight(): int;
public function getBottom(): int;
public function getLeft(): int;
/** @return array<string, int> */
public function toArray(): array;
}