Skip to content

Commit

Permalink
Merge pull request #18 from reyesoft/changed/changed-version-endroid
Browse files Browse the repository at this point in the history
changed/changed-version-endroid auto-commit
  • Loading branch information
ahumadamatias authored Dec 5, 2024
2 parents 311c0e3 + ae45680 commit 8a3e43d
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 29 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.0.0] - 2024-12-05

### Updated
- Upgraded the QR code generation library to the latest version of endroid/qr-code.

### Changed
- Refactored the implementation of QR code generation to align with the updated library.
- Breaking change: Applications using this library must update their QR generation code to ensure compatibility with the new implementation.

## [0.1.1] - 2021-06-02

### Changed
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
}
],
"require": {
"php": "^8.0",
"endroid/qr-code": "^3.2",
"php": "^8.1",
"endroid/qr-code": "5.0.9",
"ext-gd": "*"
},
"require-dev": {
"reyesoft/ci": "2.0.1",
"phpunit/phpunit": "^9.1.5",
"khanamiryan/qrcode-detector-decoder": "^2.0",
"codedungeon/phpunit-result-printer": "0.30.0"
},
"autoload": {
"classmap": [
"classmap": [
"src/"
],
"psr-4": {
],
"psr-4": {
"CryptoQr\\": "src/"
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/CryptoQr.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function updateText(): void
$this->getParam('label', $this->getLabel()) .
$this->getParam('message', $this->getMessage());

$this->getQrCode()->setText(
$this->getQrCode()->setData(
$uri . ($params !== '' ? '?' . substr($params, 1) : '')
);
}
Expand Down
42 changes: 39 additions & 3 deletions src/Qr.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,35 @@

namespace CryptoQr;

use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\Result\ResultInterface;

class Qr
{
/**
* @var string
*/
protected $address = '';

/**
* @var PngWriter
*/
protected $writer;
/**
* @var LogoInterface|null
*/
protected $logo;
/**
* @var QrCode
*/
protected $qr_code;

public function __construct(string $address = '')
{
$this->qr_code = new QrCode();
$this->qr_code = new QrCode($address);
$this->setAddress($address);
$this->writer = new PngWriter();
}

public function setAddress(string $address): void
Expand All @@ -40,16 +51,41 @@ protected function updateText(): void
{
$uri = $this->getAddress();

$this->getQrCode()->setText($uri);
$this->getQrCode()->setData($uri);
}

public function getAddress(): string
{
return $this->address;
}

public function setLogo(LogoInterface $logo): void
{
$this->logo = $logo;
}

public function getQrCode(): QrCode
{
return $this->qr_code;
}

private function writerResult(): ResultInterface
{
return $this->writer->write($this->getQrCode(), $this->logo);
}

public function getString(): string
{
return $this->writerResult()->getString();
}

public function getDataUri(): string
{
return $this->writerResult()->getDataUri();
}

public function writeFile(string $filename): void
{
$this->writerResult()->saveToFile($filename);
}
}
12 changes: 6 additions & 6 deletions tests/BitcoinQrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class BitcoinQrTest extends TestCase
public function testBitcoinQrAddress(): void
{
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY', $reader->text());
Expand All @@ -36,7 +36,7 @@ public function testBitcoinQrWithLabel(): void
$address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY';
$qr = new BitcoinQr($address);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('bitcoin:' . $address .
Expand All @@ -48,7 +48,7 @@ public function testBitcoinQrWithRequestBtc(): void
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setAmount(20.3);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -64,7 +64,7 @@ public function testBitcoinQrWithRequestAndMessage(): void
$qr = new BitcoinQr($address);
$qr->setAmount(0.000023456789);
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -79,7 +79,7 @@ public function testBitcoinQrWithMessage(): void
$message = 'Donation for project xyz';
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -97,7 +97,7 @@ public function testWriteBitcoinQrFile(): void
$qr->setAmount(80);
$qr->setLabel('Caritas');
$qr->setMessage('Donation for project xyz');
$qr->getQrCode()->writeFile($filename);
$qr->writeFile($filename);

$image = imagecreatefromstring((string) file_get_contents($filename));

Expand Down
14 changes: 7 additions & 7 deletions tests/CryptoQrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class CryptoQrTest extends TestCase
public function testBitcoinQrAddress(): void
{
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY', $reader->text());
Expand All @@ -35,7 +35,7 @@ public function testQrAddressWithoutProtocol(): void
{
$qr = new CryptoQr('TLPF4HgzmJgQc2oDmR8Msxq4LeUfkA4n4W');

$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('TLPF4HgzmJgQc2oDmR8Msxq4LeUfkA4n4W', $reader->text());
Expand All @@ -46,7 +46,7 @@ public function testBitcoinQrWithLabel(): void
$address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY';
$qr = new BitcoinQr($address);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('bitcoin:' . $address .
Expand All @@ -58,7 +58,7 @@ public function testBitcoinQrWithRequestBtc(): void
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setAmount(20.3);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -74,7 +74,7 @@ public function testBitcoinQrWithRequestAndMessage(): void
$qr = new BitcoinQr($address);
$qr->setAmount(0.000023456789);
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -89,7 +89,7 @@ public function testBitcoinQrWithMessage(): void
$message = 'Donation for project xyz';
$qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -107,7 +107,7 @@ public function testWriteBitcoinQrFile(): void
$qr->setAmount(80);
$qr->setLabel('Caritas');
$qr->setMessage('Donation for project xyz');
$qr->getQrCode()->writeFile($filename);
$qr->writeFile($filename);

$image = imagecreatefromstring((string) file_get_contents($filename));

Expand Down
12 changes: 6 additions & 6 deletions tests/EthereumQrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class EthereumQrTest extends TestCase
public function testEthereumQrAddress(): void
{
$qr = new EthereumQr('0xe8ecDFacE0b274042aAD072149eEc3e232586499');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('ethereum:0xe8ecDFacE0b274042aAD072149eEc3e232586499', $reader->text());
Expand All @@ -35,7 +35,7 @@ public function testEthereumQrWithLabel(): void
$address = '0xe8ecDFacE0b274042aAD072149eEc3e232586499';
$qr = new EthereumQr($address);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('ethereum:' . $address .
Expand All @@ -47,7 +47,7 @@ public function testEthereumQrWithRequestEth(): void
$qr = new EthereumQr('0xe8ecDFacE0b274042aAD072149eEc3e232586499');
$qr->setAmount(20.3);
$qr->setLabel('Caritas');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -63,7 +63,7 @@ public function testEthereumQrWithRequestAndMessage(): void
$qr = new EthereumQr($address);
$qr->setAmount(0.000023456789);
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -78,7 +78,7 @@ public function testEthereumQrWithMessage(): void
$message = 'Donation for project xyz';
$qr = new EthereumQr('0xe8ecDFacE0b274042aAD072149eEc3e232586499');
$qr->setMessage($message);
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame(
Expand All @@ -96,7 +96,7 @@ public function testWriteEthereumQrFile(): void
$qr->setAmount(80);
$qr->setLabel('Caritas');
$qr->setMessage('Donation for project xyz');
$qr->getQrCode()->writeFile($filename);
$qr->writeFile($filename);

$image = imagecreatefromstring((string) file_get_contents($filename));

Expand Down
32 changes: 31 additions & 1 deletion tests/QrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace CryptoQr\Tests;

use CryptoQr\Qr;
use Endroid\QrCode\Logo\Logo;
use PHPUnit\Framework\TestCase;
use Zxing\QrReader;

Expand All @@ -24,9 +25,38 @@ final class QrTest extends TestCase
public function testQrAddress(): void
{
$qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$pngData = $qr->getQrCode()->writeString();
$pngData = $qr->getString();

$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertSame('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY', $reader->text());
}

public function testQrUri(): void
{
$qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$pngUri = $qr->getDataUri();

$this->assertNotEmpty($pngUri);
}

public function testSetQrLogo(): void
{
$qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->setLogo(new Logo(__DIR__ . '/resources/img/BTC.png'));
$pngUri = $qr->getDataUri();

$this->assertNotEmpty($pngUri);
}

public function testWriteQrFile(): void
{
$filename = sys_get_temp_dir() . '/qr-code.png';

$qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY');
$qr->writeFile($filename);

$image = imagecreatefromstring((string) file_get_contents($filename));

$this->assertNotFalse($image);
}
}
Binary file added tests/resources/img/BTC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8a3e43d

Please sign in to comment.