Просмотр исходного кода

:shower: DecoderResult cleanup

smiley 3 лет назад
Родитель
Сommit
24c7a148c3
3 измененных файлов с 49 добавлено и 32 удалено
  1. 1 1
      src/Decoder/Decoder.php
  2. 44 29
      src/Decoder/DecoderResult.php
  3. 4 2
      tests/QRCodeReaderTestAbstract.php

+ 1 - 1
src/Decoder/Decoder.php

@@ -318,7 +318,7 @@ final class Decoder{
 
 
 		return new DecoderResult([
 		return new DecoderResult([
 			'rawBytes'                 => $bytes,
 			'rawBytes'                 => $bytes,
-			'text'                     => $result,
+			'data'                     => $result,
 			'version'                  => $version,
 			'version'                  => $version,
 			'eccLevel'                 => $ecLevel,
 			'eccLevel'                 => $ecLevel,
 			'structuredAppendParity'   => $parityData,
 			'structuredAppendParity'   => $parityData,

+ 44 - 29
src/Decoder/DecoderResult.php

@@ -11,54 +11,69 @@
 
 
 namespace chillerlan\QRCode\Decoder;
 namespace chillerlan\QRCode\Decoder;
 
 
-use chillerlan\Settings\SettingsContainerAbstract;
-use chillerlan\QRCode\Common\{EccLevel, Version};
+use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
+use function property_exists;
 
 
 /**
 /**
  * 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,
  * 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.
  *
  *
- * @property int[]                              $rawBytes
- * @property string                             $text
- * @property \chillerlan\QRCode\Common\Version  $version
- * @property \chillerlan\QRCode\Common\EccLevel $eccLevel
- * @property int                                $structuredAppendParity
- * @property int                                $structuredAppendSequence
+ * @property int[]                                 $rawBytes
+ * @property string                                $data
+ * @property \chillerlan\QRCode\Common\Version     $version
+ * @property \chillerlan\QRCode\Common\EccLevel    $eccLevel
+ * @property \chillerlan\QRCode\Common\MaskPattern $maskPattern
+ * @property int                                   $structuredAppendParity
+ * @property int                                   $structuredAppendSequence
  */
  */
-final class DecoderResult extends SettingsContainerAbstract{
+final class DecoderResult{
 
 
-	protected array    $rawBytes;
-	protected string   $text;
-	protected Version  $version;
-	protected EccLevel $eccLevel;
-	protected int      $structuredAppendParity = -1;
-	protected int      $structuredAppendSequence = -1;
+	protected array       $rawBytes;
+	protected string      $data;
+	protected Version     $version;
+	protected EccLevel    $eccLevel;
+	protected MaskPattern $maskPattern;
+	protected int         $structuredAppendParity = -1;
+	protected int         $structuredAppendSequence = -1;
 
 
 	/**
 	/**
-	 * @inheritDoc
+	 * DecoderResult constructor.
 	 */
 	 */
-	public function __set($property, $value):void{
-		// noop, read-only
-	}
+	public function __construct(iterable $properties = null){
+
+		if(!empty($properties)){
+
+			foreach($properties as $property => $value){
+
+				if(!property_exists($this, $property)){
+					continue;
+				}
+
+				$this->{$property} = $value;
+			}
+
+		}
 
 
-	/**
-	 * @inheritDoc
-	 */
-	public function __toString():string{
-		return $this->text;
 	}
 	}
 
 
 	/**
 	/**
-	 * @inheritDoc
+	 * @return mixed|null
 	 */
 	 */
-	public function fromIterable(iterable $properties):self{
+	public function __get(string $property){
 
 
-		foreach($properties as $key => $value){
-			parent::__set($key, $value);
+		if(property_exists($this, $property)){
+			return $this->{$property};
 		}
 		}
 
 
-		return $this;
+		return null;
+	}
+
+	/**
+	 *
+	 */
+	public function __toString():string{
+		return $this->data;
 	}
 	}
 
 
 	/**
 	/**

+ 4 - 2
tests/QRCodeReaderTestAbstract.php

@@ -97,7 +97,9 @@ abstract class QRCodeReaderTestAbstract extends TestCase{
 			->addByteSegment($byte)
 			->addByteSegment($byte)
 		;
 		;
 
 
-		$this::assertSame($numeric.$alphanum.$kanji.$byte, (string)$qrcode->readFromBlob($qrcode->render()));
+		$result = $qrcode->readFromBlob($qrcode->render());
+
+		$this::assertSame($numeric.$alphanum.$kanji.$byte, $result->data);
 	}
 	}
 
 
 	public function dataTestProvider():Generator{
 	public function dataTestProvider():Generator{
@@ -140,7 +142,7 @@ abstract class QRCodeReaderTestAbstract extends TestCase{
 			$this::markTestSkipped(sprintf('skipped version %s%s: %s', $version, $ecc, $e->getMessage()));
 			$this::markTestSkipped(sprintf('skipped version %s%s: %s', $version, $ecc, $e->getMessage()));
 		}
 		}
 
 
-		$this::assertSame($expected, $result->text);
+		$this::assertSame($expected, $result->data);
 		$this::assertSame($version->getVersionNumber(), $result->version->getVersionNumber());
 		$this::assertSame($version->getVersionNumber(), $result->version->getVersionNumber());
 		$this::assertSame($ecc->getLevel(), $result->eccLevel->getLevel());
 		$this::assertSame($ecc->getLevel(), $result->eccLevel->getLevel());
 	}
 	}