Skip to content

Commit

Permalink
Retrieve data via QR code
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Dec 16, 2018
1 parent fdd809a commit d951439
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 39 deletions.
32 changes: 32 additions & 0 deletions src/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Endroid\QrCode;

use BaconQrCode\Encoder\Encoder;
use Endroid\QrCode\Exception\InvalidPathException;
use Endroid\QrCode\Exception\UnsupportedExtensionException;
use Endroid\QrCode\Writer\WriterInterface;
Expand Down Expand Up @@ -369,4 +370,35 @@ public function getValidateResult(): bool
{
return $this->validateResult;
}

public function getData(): array
{
$baconErrorCorrectionLevel = $this->errorCorrectionLevel->toBaconErrorCorrectionLevel();

$baconQrCode = Encoder::encode($this->text, $baconErrorCorrectionLevel, $this->encoding);

$matrix = $baconQrCode->getMatrix()->getArray()->toArray();

foreach ($matrix as &$row) {
$row = $row->toArray();
}

$data = ['matrix' => $matrix];
$data['block_count'] = count($matrix[0]);
$data['block_size'] = $this->size / $data['block_count'];
if ($this->roundBlockSize) {
$data['block_size'] = intval(floor($data['block_size']));
}
$data['inner_width'] = $data['block_size'] * $data['block_count'];
$data['inner_height'] = $data['block_size'] * $data['block_count'];
$data['outer_width'] = $this->size + 2 * $this->margin;
$data['outer_height'] = $this->size + 2 * $this->margin;
$data['margin_left'] = ($data['outer_width'] - $data['inner_width']) / 2;
if ($this->roundBlockSize) {
$data['margin_left'] = intval(floor($data['margin_left']));
}
$data['margin_right'] = $data['outer_width'] - $data['inner_width'] - $data['margin_left'];

return $data;
}
}
2 changes: 2 additions & 0 deletions src/QrCodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ public function writeString(): string;
public function writeDataUri(): string;

public function writeFile(string $path): void;

public function getData(): array;
}
31 changes: 0 additions & 31 deletions src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,6 @@

abstract class AbstractWriter implements WriterInterface
{
protected function getData(QrCodeInterface $qrCode): array
{
$baconErrorCorrectionLevel = $qrCode->getErrorCorrectionLevel()->toBaconErrorCorrectionLevel();

$baconQrCode = Encoder::encode($qrCode->getText(), $baconErrorCorrectionLevel, $qrCode->getEncoding());

$matrix = $baconQrCode->getMatrix()->getArray()->toArray();

foreach ($matrix as &$row) {
$row = $row->toArray();
}

$data = ['matrix' => $matrix];
$data['block_count'] = count($matrix[0]);
$data['block_size'] = $qrCode->getSize() / $data['block_count'];
if ($qrCode->getRoundBlockSize()) {
$data['block_size'] = intval(floor($data['block_size']));
}
$data['inner_width'] = $data['block_size'] * $data['block_count'];
$data['inner_height'] = $data['block_size'] * $data['block_count'];
$data['outer_width'] = $qrCode->getSize() + 2 * $qrCode->getMargin();
$data['outer_height'] = $qrCode->getSize() + 2 * $qrCode->getMargin();
$data['margin_left'] = ($data['outer_width'] - $data['inner_width']) / 2;
if ($qrCode->getRoundBlockSize()) {
$data['margin_left'] = intval(floor($data['margin_left']));
}
$data['margin_right'] = $data['outer_width'] - $data['inner_width'] - $data['margin_left'];

return $data;
}

public function writeDataUri(QrCodeInterface $qrCode): string
{
$dataUri = 'data:'.$this->getContentType().';base64,'.base64_encode($this->writeString($qrCode));
Expand Down
3 changes: 1 addition & 2 deletions src/Writer/BinaryWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class BinaryWriter extends AbstractWriter
{
public function writeString(QrCodeInterface $qrCode): string
{
$data = $this->getData($qrCode);

$rows = [];
$data = $qrCode->getData();
foreach ($data['matrix'] as $row) {
$values = '';
foreach ($row as $value) {
Expand Down
3 changes: 2 additions & 1 deletion src/Writer/DebugWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ class DebugWriter extends AbstractWriter
public function writeString(QrCodeInterface $qrCode): string
{
$data = [];
$skip = ['getData'];

$reflectionClass = new ReflectionClass($qrCode);
foreach ($reflectionClass->getMethods() as $method) {
$methodName = $method->getShortName();
if (0 === strpos($methodName, 'get') && 0 == $method->getNumberOfParameters()) {
if (0 === strpos($methodName, 'get') && 0 == $method->getNumberOfParameters() && !in_array($methodName, $skip)) {
$value = $qrCode->{$methodName}();
if (is_array($value) && !is_object(current($value))) {
$value = '['.implode(', ', $value).']';
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/EpsWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EpsWriter extends AbstractWriter
{
public function writeString(QrCodeInterface $qrCode): string
{
$data = $this->getData($qrCode);
$data = $qrCode->getData();

$epsData = [];
$epsData[] = '%!PS-Adobe-3.0 EPSF-3.0';
Expand Down
4 changes: 1 addition & 3 deletions src/Writer/PngWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class PngWriter extends AbstractWriter
{
public function writeString(QrCodeInterface $qrCode): string
{
$data = $this->getData($qrCode);

$image = $this->createImage($data, $qrCode);
$image = $this->createImage($qrCode->getData(), $qrCode);

if ($qrCode->getLogoPath()) {
$image = $this->addLogo($image, $qrCode->getLogoPath(), $qrCode->getLogoWidth(), $qrCode->getLogoHeight());
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/SvgWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function writeString(QrCodeInterface $qrCode): string
throw new ValidationException('Built-in validation reader can not check SVG images: please disable via setValidateResult(false)');
}

$data = $this->getData($qrCode);
$data = $qrCode->getData();

$svg = new SimpleXMLElement('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"/>');
$svg->addAttribute('version', '1.1');
Expand Down
16 changes: 16 additions & 0 deletions tests/QrCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,20 @@ public function testWriteFile(): void

$this->assertTrue(is_resource($image));
}

public function testData(): void
{
$qrCode = new QrCode('QR Code');

$data = $qrCode->getData();

$this->assertArrayHasKey('block_count', $data);
$this->assertArrayHasKey('block_size', $data);
$this->assertArrayHasKey('inner_width', $data);
$this->assertArrayHasKey('inner_height', $data);
$this->assertArrayHasKey('outer_width', $data);
$this->assertArrayHasKey('outer_height', $data);
$this->assertArrayHasKey('margin_left', $data);
$this->assertArrayHasKey('margin_right', $data);
}
}

0 comments on commit d951439

Please sign in to comment.