Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.4', '8.5']
php-versions: ['8.5', '8.6']
fail-fast: false
steps:
- uses: actions/checkout@v5
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": "^8.4",
"php": "^8.5",
"bacon/bacon-qr-code": "^3.0"
},
"require-dev": {
Expand Down Expand Up @@ -43,7 +43,8 @@
"endroid/*": "source"
},
"allow-plugins": {
"endroid/installer": true
"endroid/installer": true,
"carthage-software/mago": true
}
},
"extra": {
Expand Down
11 changes: 9 additions & 2 deletions src/Bacon/MatrixFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@

final readonly class MatrixFactory implements MatrixFactoryInterface
{
#[\Override]
public function create(QrCodeInterface $qrCode): MatrixInterface
{
$baconErrorCorrectionLevel = ErrorCorrectionLevelConverter::convertToBaconErrorCorrectionLevel($qrCode->getErrorCorrectionLevel());
$baconMatrix = Encoder::encode($qrCode->getData(), $baconErrorCorrectionLevel, strval($qrCode->getEncoding()))->getMatrix();
$baconErrorCorrectionLevel = ErrorCorrectionLevelConverter::convertToBaconErrorCorrectionLevel(
$qrCode->getErrorCorrectionLevel(),
);
$baconMatrix = Encoder::encode(
$qrCode->getData(),
$baconErrorCorrectionLevel,
strval($qrCode->getEncoding()),
)->getMatrix();

$blockValues = [];
$columnCount = $baconMatrix->getWidth();
Expand Down
48 changes: 26 additions & 22 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
public function __construct(
private WriterInterface $writer = new PngWriter(),
/** @var array<mixed> */
/** @var array<string, mixed> */
private array $writerOptions = [],
private bool $validateResult = false,
// QrCode options
Expand All @@ -42,7 +42,7 @@ public function __construct(
private ColorInterface $backgroundColor = new Color(255, 255, 255),
// Label options
private string $labelText = '',
private FontInterface $labelFont = new Font(__DIR__.'/../../assets/open_sans.ttf', 16),
private FontInterface $labelFont = new Font(__DIR__ . '/../../assets/open_sans.ttf', 16),
private LabelAlignment $labelAlignment = LabelAlignment::Center,
private MarginInterface $labelMargin = new Margin(0, 10, 10, 10),
private ColorInterface $labelTextColor = new Color(0, 0, 0),
Expand All @@ -51,10 +51,10 @@ public function __construct(
private ?int $logoResizeToWidth = null,
private ?int $logoResizeToHeight = null,
private bool $logoPunchoutBackground = false,
) {
}
) {}

/** @param array<mixed>|null $writerOptions */
/** @param array<string, mixed>|null $writerOptions */
#[\Override]
public function build(
?WriterInterface $writer = null,
?array $writerOptions = null,
Expand Down Expand Up @@ -84,9 +84,9 @@ public function build(
throw ValidationException::createForUnsupportedWriter(get_class($this->writer));
}

$writer = $writer ?? $this->writer;
$writerOptions = $writerOptions ?? $this->writerOptions;
$validateResult = $validateResult ?? $this->validateResult;
$writer ??= $this->writer;
$writerOptions ??= $this->writerOptions;
$validateResult ??= $this->validateResult;

$createLabel = $this->labelText || $labelText;
$createLogo = $this->logoPath || $logoPath;
Expand All @@ -99,23 +99,27 @@ public function build(
margin: $margin ?? $this->margin,
roundBlockSizeMode: $roundBlockSizeMode ?? $this->roundBlockSizeMode,
foregroundColor: $foregroundColor ?? $this->foregroundColor,
backgroundColor: $backgroundColor ?? $this->backgroundColor
backgroundColor: $backgroundColor ?? $this->backgroundColor,
);

$logo = $createLogo ? new Logo(
path: $logoPath ?? $this->logoPath,
resizeToWidth: $logoResizeToWidth ?? $this->logoResizeToWidth,
resizeToHeight: $logoResizeToHeight ?? $this->logoResizeToHeight,
punchoutBackground: $logoPunchoutBackground ?? $this->logoPunchoutBackground
) : null;
$logo = $createLogo
? new Logo(
path: $logoPath ?? $this->logoPath,
resizeToWidth: $logoResizeToWidth ?? $this->logoResizeToWidth,
resizeToHeight: $logoResizeToHeight ?? $this->logoResizeToHeight,
punchoutBackground: $logoPunchoutBackground ?? $this->logoPunchoutBackground,
)
: null;

$label = $createLabel ? new Label(
text: $labelText ?? $this->labelText,
font: $labelFont ?? $this->labelFont,
alignment: $labelAlignment ?? $this->labelAlignment,
margin: $labelMargin ?? $this->labelMargin,
textColor: $labelTextColor ?? $this->labelTextColor
) : null;
$label = $createLabel
? new Label(
text: $labelText ?? $this->labelText,
font: $labelFont ?? $this->labelFont,
alignment: $labelAlignment ?? $this->labelAlignment,
margin: $labelMargin ?? $this->labelMargin,
textColor: $labelTextColor ?? $this->labelTextColor,
)
: null;

$result = $writer->write($qrCode, $logo, $label, $writerOptions);

Expand Down
2 changes: 1 addition & 1 deletion src/Builder/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

interface BuilderInterface
{
/** @param array<mixed>|null $writerOptions */
/** @param array<string, mixed>|null $writerOptions */
public function build(
?WriterInterface $writer = null,
?array $writerOptions = null,
Expand Down
4 changes: 3 additions & 1 deletion src/Builder/BuilderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ final class BuilderRegistry implements BuilderRegistryInterface
/** @var array<BuilderInterface> */
private array $builders = [];

#[\Override]
public function set(string $name, BuilderInterface $builder): void
{
$this->builders[$name] = $builder;
}

#[\Override]
public function get(string $name): BuilderInterface
{
if (!isset($this->builders[$name])) {
if (null === ($this->builders[$name] ?? null)) {
throw new \Exception(sprintf('Builder with name "%s" not available from registry', $name));
}

Expand Down
12 changes: 9 additions & 3 deletions src/Color/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,45 @@ public function __construct(
private int $blue,
/** @var int<0, 127> */
private int $alpha = 0,
) {
}
) {}

#[\Override]
public function getRed(): int
{
return $this->red;
}

#[\Override]
public function getGreen(): int
{
return $this->green;
}

#[\Override]
public function getBlue(): int
{
return $this->blue;
}

#[\Override]
public function getAlpha(): int
{
return $this->alpha;
}

#[\Override]
public function getOpacity(): float
{
return 1 - $this->alpha / 127;
return 1 - ($this->alpha / 127);
}

#[\Override]
public function getHex(): string
{
return sprintf('#%02x%02x%02x', $this->red, $this->green, $this->blue);
}

#[\Override]
public function toArray(): array
{
return [
Expand Down
12 changes: 9 additions & 3 deletions src/Encoding/Encoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ public function __construct(
) {
if ('UTF-8' !== $value) {
if (!function_exists('mb_list_encodings')) {
throw new \Exception('Unable to validate encoding: make sure the mbstring extension is installed and enabled');
throw new \Exception(
'Unable to validate encoding: make sure the mbstring extension is installed and enabled',
);
}

if (!in_array($value, mb_list_encodings())) {
throw new \Exception(sprintf('Invalid encoding "%s": choose one of '.implode(', ', mb_list_encodings()), $value));
if (!in_array($value, mb_list_encodings(), strict: true)) {
throw new \Exception(sprintf(
'Invalid encoding "%s": choose one of ' . implode(', ', mb_list_encodings()),
$value,
));
}
}
}

#[\Override]
public function __toString(): string
{
return $this->value;
Expand Down
1 change: 1 addition & 0 deletions src/Encoding/EncodingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

interface EncodingInterface extends \Stringable
{
#[\Override]
public function __toString(): string;
}
4 changes: 1 addition & 3 deletions src/Exception/BlockSizeTooSmallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Endroid\QrCode\Exception;

final class BlockSizeTooSmallException extends \Exception
{
}
final class BlockSizeTooSmallException extends \Exception {}
8 changes: 7 additions & 1 deletion src/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public static function createForMissingPackage(string $packageName): self

public static function createForInvalidData(string $expectedData, string $actualData): self
{
return new self('The validation reader read "'.$actualData.'" instead of "'.$expectedData.'". Adjust your parameters to increase readability or disable validation.');
return new self(
'The validation reader read "'
. $actualData
. '" instead of "'
. $expectedData
. '". Adjust your parameters to increase readability or disable validation.',
);
}
}
16 changes: 9 additions & 7 deletions src/ImageData/LabelImageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
private function __construct(
private int $width,
private int $height,
) {
}
) {}

public static function createForLabel(LabelInterface $label): self
{
Expand All @@ -24,16 +23,19 @@ public static function createForLabel(LabelInterface $label): self
throw new \Exception('Function "imagettfbbox" does not exist: check your FreeType installation');
}

$labelBox = imagettfbbox($label->getFont()->getSize(), 0, $label->getFont()->getPath(), $label->getText());
/** @var array{int, int, int, int, int, int, int, int}|false $labelBox FreeType returns four coordinate pairs. */
$labelBox = imagettfbbox(
$label->getFont()->getSize(),
angle: 0,
font_filename: $label->getFont()->getPath(),
string: $label->getText(),
);

if (!is_array($labelBox)) {
throw new \Exception('Unable to generate label image box: check your FreeType installation');
}

return new self(
intval($labelBox[2] - $labelBox[0]),
intval($labelBox[0] - $labelBox[7])
);
return new self(intval($labelBox[2] - $labelBox[0]), intval($labelBox[0] - $labelBox[7]));
}

public function getWidth(): int
Expand Down
Loading
Loading