Browse Source

:shower: use SettingsContainerInterface for DecoderResult

codemasher 4 years ago
parent
commit
a856d2aa25
3 changed files with 39 additions and 68 deletions
  1. 8 1
      src/Decoder/Decoder.php
  2. 28 64
      src/Decoder/DecoderResult.php
  3. 3 3
      tests/QRCodeReaderTest.php

+ 8 - 1
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
+		]);
 	}
 
 }

+ 28 - 64
src/Decoder/DecoderResult.php

@@ -11,97 +11,61 @@
 
 namespace chillerlan\QRCode\Decoder;
 
+use chillerlan\Settings\SettingsContainerAbstract;
 use chillerlan\QRCode\Common\{EccLevel, Version};
 
 /**
- * <p>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.</p>
+ * 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;
 
 	/**
-	 *
-	 */
-	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;
-	}
-
-	/**
-	 * @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
+	 * @inheritDoc
 	 */
-	public function getText():string{
-		return $this->text;
+	public function __set($property, $value):void{
+		// noop, read-only
 	}
 
 	/**
-	 *
+	 * @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);
+		}
 
-	/**
-	 *
-	 */
-	public function hasStructuredAppend():bool{
-		return $this->structuredAppendParity >= 0 && $this->structuredAppendSequenceNumber >= 0;
+		return $this;
 	}
 
 	/**
 	 *
 	 */
-	public function getStructuredAppendParity():int{
-		return $this->structuredAppendParity;
-	}
-
-	/**
-	 *
-	 */
-	public function getStructuredAppendSequenceNumber():int{
-		return $this->structuredAppendSequenceNumber;
+	public function hasStructuredAppend():bool{
+		return $this->structuredAppendParity >= 0 && $this->structuredAppendSequence >= 0;
 	}
 
 }

+ 3 - 3
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());
 	}
 
 }