Skip to content

Commit

Permalink
Modernize code base
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Oct 2, 2023
1 parent 0db25b5 commit 1f65da3
Show file tree
Hide file tree
Showing 45 changed files with 176 additions and 299 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.x-dev"
"dev-master": "5.x-dev"
}
}
}
27 changes: 9 additions & 18 deletions src/Bacon/ErrorCorrectionLevelConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@

namespace Endroid\QrCode\Bacon;

use BaconQrCode\Common\ErrorCorrectionLevel;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile;
use BaconQrCode\Common\ErrorCorrectionLevel as BaconErrorCorrectionLevel;
use Endroid\QrCode\ErrorCorrectionLevel;

final class ErrorCorrectionLevelConverter
{
public static function convertToBaconErrorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): ErrorCorrectionLevel
public static function convertToBaconErrorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BaconErrorCorrectionLevel
{
if ($errorCorrectionLevel instanceof ErrorCorrectionLevelLow) {
return ErrorCorrectionLevel::valueOf('L');
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelMedium) {
return ErrorCorrectionLevel::valueOf('M');
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelQuartile) {
return ErrorCorrectionLevel::valueOf('Q');
} elseif ($errorCorrectionLevel instanceof ErrorCorrectionLevelHigh) {
return ErrorCorrectionLevel::valueOf('H');
}

throw new \Exception('Error correction level could not be converted');
return match ($errorCorrectionLevel) {
ErrorCorrectionLevel::Low => BaconErrorCorrectionLevel::valueOf('L'),
ErrorCorrectionLevel::Medium => BaconErrorCorrectionLevel::valueOf('M'),
ErrorCorrectionLevel::Quartile => BaconErrorCorrectionLevel::valueOf('Q'),
ErrorCorrectionLevel::High => BaconErrorCorrectionLevel::valueOf('H')
};
}
}
9 changes: 6 additions & 3 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Encoding\EncodingInterface;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
use Endroid\QrCode\Exception\ValidationException;
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
use Endroid\QrCode\Label\Font\FontInterface;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Label\LabelAlignment;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Label\Margin\MarginInterface;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\Result\ResultInterface;
Expand Down Expand Up @@ -100,7 +103,7 @@ public function encoding(EncodingInterface $encoding): BuilderInterface
return $this;
}

public function errorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): BuilderInterface
public function errorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BuilderInterface
{
$this->options['errorCorrectionLevel'] = $errorCorrectionLevel;

Expand All @@ -121,7 +124,7 @@ public function margin(int $margin): BuilderInterface
return $this;
}

public function roundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): BuilderInterface
public function roundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): BuilderInterface
{
$this->options['roundBlockSizeMode'] = $roundBlockSizeMode;

Expand Down Expand Up @@ -184,7 +187,7 @@ public function labelFont(FontInterface $labelFont): BuilderInterface
return $this;
}

public function labelAlignment(LabelAlignmentInterface $labelAlignment): BuilderInterface
public function labelAlignment(LabelAlignment $labelAlignment): BuilderInterface
{
$this->options['labelAlignment'] = $labelAlignment;

Expand Down
12 changes: 6 additions & 6 deletions src/Builder/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Encoding\EncodingInterface;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Label\Font\FontInterface;
use Endroid\QrCode\Label\LabelAlignment;
use Endroid\QrCode\Label\Margin\MarginInterface;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface;
use Endroid\QrCode\RoundBlockSizeMode;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Endroid\QrCode\Writer\WriterInterface;

Expand All @@ -27,13 +27,13 @@ public function data(string $data): BuilderInterface;

public function encoding(EncodingInterface $encoding): BuilderInterface;

public function errorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): BuilderInterface;
public function errorCorrectionLevel(ErrorCorrectionLevel $errorCorrectionLevel): BuilderInterface;

public function size(int $size): BuilderInterface;

public function margin(int $margin): BuilderInterface;

public function roundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): BuilderInterface;
public function roundBlockSizeMode(RoundBlockSizeMode $roundBlockSizeMode): BuilderInterface;

public function foregroundColor(ColorInterface $foregroundColor): BuilderInterface;

Expand All @@ -51,7 +51,7 @@ public function labelText(string $labelText): BuilderInterface;

public function labelFont(FontInterface $labelFont): BuilderInterface;

public function labelAlignment(LabelAlignmentInterface $labelAlignment): BuilderInterface;
public function labelAlignment(LabelAlignment $labelAlignment): BuilderInterface;

public function labelMargin(MarginInterface $labelMargin): BuilderInterface;

Expand Down
8 changes: 4 additions & 4 deletions src/Color/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
final class Color implements ColorInterface
{
public function __construct(
private int $red,
private int $green,
private int $blue,
private int $alpha = 0
private readonly int $red,
private readonly int $green,
private readonly int $blue,
private readonly int $alpha = 0
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Encoding/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final class Encoding implements EncodingInterface
{
public function __construct(
private string $value
private readonly string $value
) {
if (!in_array($value, mb_list_encodings())) {
throw new \Exception(sprintf('Invalid encoding "%s"', $value));
Expand Down
2 changes: 1 addition & 1 deletion src/Encoding/EncodingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Endroid\QrCode\Encoding;

interface EncodingInterface
interface EncodingInterface extends \Stringable
{
public function __toString(): string;
}
13 changes: 13 additions & 0 deletions src/ErrorCorrectionLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Endroid\QrCode;

enum ErrorCorrectionLevel: string
{
case High = 'high';
case Low = 'low';
case Medium = 'medium';
case Quartile = 'quartile';
}
9 changes: 0 additions & 9 deletions src/ErrorCorrectionLevel/ErrorCorrectionLevelHigh.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/ErrorCorrectionLevel/ErrorCorrectionLevelInterface.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/ErrorCorrectionLevel/ErrorCorrectionLevelLow.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/ErrorCorrectionLevel/ErrorCorrectionLevelMedium.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/ErrorCorrectionLevel/ErrorCorrectionLevelQuartile.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/ImageData/LabelImageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use Endroid\QrCode\Label\LabelInterface;

class LabelImageData
final class LabelImageData
{
private function __construct(
private int $width,
private int $height
private readonly int $width,
private readonly int $height
) {
}

Expand Down
12 changes: 6 additions & 6 deletions src/ImageData/LogoImageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

use Endroid\QrCode\Logo\LogoInterface;

class LogoImageData
final class LogoImageData
{
private function __construct(
private string $data,
private readonly string $data,
private \GdImage|null $image,
private string $mimeType,
private int $width,
private int $height,
private bool $punchoutBackground
private readonly string $mimeType,
private readonly int $width,
private readonly int $height,
private readonly bool $punchoutBackground
) {
}

Expand Down
9 changes: 0 additions & 9 deletions src/Label/Alignment/LabelAlignmentCenter.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Label/Alignment/LabelAlignmentInterface.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Label/Alignment/LabelAlignmentLeft.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Label/Alignment/LabelAlignmentRight.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Label/Font/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
final class Font implements FontInterface
{
public function __construct(
private string $path,
private int $size = 16
private readonly string $path,
private readonly int $size = 16
) {
$this->assertValidPath($path);
}
Expand Down
23 changes: 6 additions & 17 deletions src/Label/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,20 @@

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 FontInterface $font;
private LabelAlignmentInterface $alignment;
private MarginInterface $margin;
private ColorInterface $textColor;

public function __construct(
private string $text,
FontInterface $font = null,
LabelAlignmentInterface $alignment = null,
MarginInterface $margin = null,
ColorInterface $textColor = null
private FontInterface $font = new Font(__DIR__.'/../../assets/noto_sans.otf', 16),
private LabelAlignment $alignment = LabelAlignment::Center,
private MarginInterface $margin = new Margin(0, 10, 10, 10),
private ColorInterface $textColor = new Color(0, 0, 0)
) {
$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
Expand Down Expand Up @@ -62,12 +51,12 @@ public function setFont(FontInterface $font): self
return $this;
}

public function getAlignment(): LabelAlignmentInterface
public function getAlignment(): LabelAlignment
{
return $this->alignment;
}

public function setAlignment(LabelAlignmentInterface $alignment): self
public function setAlignment(LabelAlignment $alignment): self
{
$this->alignment = $alignment;

Expand Down
12 changes: 12 additions & 0 deletions src/Label/LabelAlignment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Endroid\QrCode\Label;

enum LabelAlignment: string
{
case Center = 'center';
case Left = 'left';
case Right = 'right';
}
3 changes: 1 addition & 2 deletions src/Label/LabelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;

Expand All @@ -15,7 +14,7 @@ public function getText(): string;

public function getFont(): FontInterface;

public function getAlignment(): LabelAlignmentInterface;
public function getAlignment(): LabelAlignment;

public function getMargin(): MarginInterface;

Expand Down
Loading

0 comments on commit 1f65da3

Please sign in to comment.