From a856d2aa25d0ac639fb192a887c4b067cb1e52a3 Mon Sep 17 00:00:00 2001 From: codemasher Date: Mon, 29 Nov 2021 20:47:08 +0100 Subject: [PATCH] :shower: use SettingsContainerInterface for DecoderResult --- src/Decoder/Decoder.php | 9 +++- src/Decoder/DecoderResult.php | 92 +++++++++++------------------------ tests/QRCodeReaderTest.php | 6 +-- 3 files changed, 39 insertions(+), 68 deletions(-) diff --git a/src/Decoder/Decoder.php b/src/Decoder/Decoder.php index d99f7b9c8..dbc652a4f 100644 --- a/src/Decoder/Decoder.php +++ b/src/Decoder/Decoder.php @@ -318,7 +318,14 @@ final class Decoder{ } } - return new DecoderResult($bytes, $result, $version, $ecLevel, $symbolSequence, $parityData); + return new DecoderResult([ + 'rawBytes' => $bytes, + 'text' => $result, + 'version' => $version, + 'eccLevel' => $ecLevel, + 'structuredAppendParity' => $parityData, + 'structuredAppendSequence' => $symbolSequence + ]); } } diff --git a/src/Decoder/DecoderResult.php b/src/Decoder/DecoderResult.php index e46655838..f12875080 100644 --- a/src/Decoder/DecoderResult.php +++ b/src/Decoder/DecoderResult.php @@ -11,97 +11,61 @@ namespace chillerlan\QRCode\Decoder; +use chillerlan\Settings\SettingsContainerAbstract; use chillerlan\QRCode\Common\{EccLevel, Version}; /** - *

Encapsulates the result of decoding a matrix of bits. This typically + * Encapsulates the result of decoding a matrix of bits. This typically * applies to 2D barcode formats. For now it contains the raw bytes obtained, - * as well as a String interpretation of those bytes, if applicable.

+ * as well as a String interpretation of those bytes, if applicable. * - * @author Sean Owen + * @property int[] $rawBytes + * @property string $text + * @property \chillerlan\QRCode\Common\Version $version + * @property \chillerlan\QRCode\Common\EccLevel $eccLevel + * @property int $structuredAppendParity + * @property int $structuredAppendSequence */ -final class DecoderResult{ +final class DecoderResult extends SettingsContainerAbstract{ - private array $rawBytes; - private string $text; - private Version $version; - private EccLevel $eccLevel; - private int $structuredAppendParity; - private int $structuredAppendSequenceNumber; + protected array $rawBytes; + protected string $text; + protected Version $version; + protected EccLevel $eccLevel; + protected int $structuredAppendParity = -1; + protected int $structuredAppendSequence = -1; /** - * + * @inheritDoc */ - public function __construct( - array $rawBytes, - string $text, - Version $version, - EccLevel $eccLevel, - int $saSequence = -1, - int $saParity = -1 - ){ - $this->rawBytes = $rawBytes; - $this->text = $text; - $this->version = $version; - $this->eccLevel = $eccLevel; - $this->structuredAppendParity = $saParity; - $this->structuredAppendSequenceNumber = $saSequence; + public function __set($property, $value):void{ + // noop, read-only } /** - * @return int[] raw bytes encoded by the barcode, if applicable, otherwise {@code null} - */ - public function getRawBytes():array{ - return $this->rawBytes; - } - - /** - * @return string raw text encoded by the barcode - */ - public function getText():string{ - return $this->text; - } - - /** - * + * @inheritDoc */ public function __toString():string{ return $this->text; } /** - * + * @inheritDoc */ - public function getVersion():Version{ - return $this->version; - } + public function fromIterable(iterable $properties):self{ - /** - * - */ - public function getEccLevel():EccLevel{ - return $this->eccLevel; + foreach($properties as $key => $value){ + parent::__set($key, $value); + } + + return $this; } /** * */ public function hasStructuredAppend():bool{ - return $this->structuredAppendParity >= 0 && $this->structuredAppendSequenceNumber >= 0; - } - - /** - * - */ - public function getStructuredAppendParity():int{ - return $this->structuredAppendParity; - } - - /** - * - */ - public function getStructuredAppendSequenceNumber():int{ - return $this->structuredAppendSequenceNumber; + return $this->structuredAppendParity >= 0 && $this->structuredAppendSequence >= 0; } } diff --git a/tests/QRCodeReaderTest.php b/tests/QRCodeReaderTest.php index 7bb22d220..efb1cd9ba 100644 --- a/tests/QRCodeReaderTest.php +++ b/tests/QRCodeReaderTest.php @@ -140,9 +140,9 @@ class QRCodeReaderTest extends TestCase{ $this::markTestSkipped(sprintf('skipped version %s%s: %s', $version, $ecc, $e->getMessage())); } - $this::assertSame($expected, $result->getText()); - $this::assertSame($version->getVersionNumber(), $result->getVersion()->getVersionNumber()); - $this::assertSame($ecc->getLevel(), $result->getEccLevel()->getLevel()); + $this::assertSame($expected, $result->text); + $this::assertSame($version->getVersionNumber(), $result->version->getVersionNumber()); + $this::assertSame($ecc->getLevel(), $result->eccLevel->getLevel()); } }