Skip to content

Commit 24c7a14

Browse files
committed
🚿 DecoderResult cleanup
1 parent bd67995 commit 24c7a14

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

src/Decoder/Decoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private function decodeBitStream(array $bytes, Version $version, EccLevel $ecLev
318318

319319
return new DecoderResult([
320320
'rawBytes' => $bytes,
321-
'text' => $result,
321+
'data' => $result,
322322
'version' => $version,
323323
'eccLevel' => $ecLevel,
324324
'structuredAppendParity' => $parityData,

src/Decoder/DecoderResult.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,69 @@
1111

1212
namespace chillerlan\QRCode\Decoder;
1313

14-
use chillerlan\Settings\SettingsContainerAbstract;
15-
use chillerlan\QRCode\Common\{EccLevel, Version};
14+
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
15+
use function property_exists;
1616

1717
/**
1818
* Encapsulates the result of decoding a matrix of bits. This typically
1919
* applies to 2D barcode formats. For now it contains the raw bytes obtained,
2020
* as well as a String interpretation of those bytes, if applicable.
2121
*
22-
* @property int[] $rawBytes
23-
* @property string $text
24-
* @property \chillerlan\QRCode\Common\Version $version
25-
* @property \chillerlan\QRCode\Common\EccLevel $eccLevel
26-
* @property int $structuredAppendParity
27-
* @property int $structuredAppendSequence
22+
* @property int[] $rawBytes
23+
* @property string $data
24+
* @property \chillerlan\QRCode\Common\Version $version
25+
* @property \chillerlan\QRCode\Common\EccLevel $eccLevel
26+
* @property \chillerlan\QRCode\Common\MaskPattern $maskPattern
27+
* @property int $structuredAppendParity
28+
* @property int $structuredAppendSequence
2829
*/
29-
final class DecoderResult extends SettingsContainerAbstract{
30+
final class DecoderResult{
3031

31-
protected array $rawBytes;
32-
protected string $text;
33-
protected Version $version;
34-
protected EccLevel $eccLevel;
35-
protected int $structuredAppendParity = -1;
36-
protected int $structuredAppendSequence = -1;
32+
protected array $rawBytes;
33+
protected string $data;
34+
protected Version $version;
35+
protected EccLevel $eccLevel;
36+
protected MaskPattern $maskPattern;
37+
protected int $structuredAppendParity = -1;
38+
protected int $structuredAppendSequence = -1;
3739

3840
/**
39-
* @inheritDoc
41+
* DecoderResult constructor.
4042
*/
41-
public function __set($property, $value):void{
42-
// noop, read-only
43-
}
43+
public function __construct(iterable $properties = null){
44+
45+
if(!empty($properties)){
46+
47+
foreach($properties as $property => $value){
48+
49+
if(!property_exists($this, $property)){
50+
continue;
51+
}
52+
53+
$this->{$property} = $value;
54+
}
55+
56+
}
4457

45-
/**
46-
* @inheritDoc
47-
*/
48-
public function __toString():string{
49-
return $this->text;
5058
}
5159

5260
/**
53-
* @inheritDoc
61+
* @return mixed|null
5462
*/
55-
public function fromIterable(iterable $properties):self{
63+
public function __get(string $property){
5664

57-
foreach($properties as $key => $value){
58-
parent::__set($key, $value);
65+
if(property_exists($this, $property)){
66+
return $this->{$property};
5967
}
6068

61-
return $this;
69+
return null;
70+
}
71+
72+
/**
73+
*
74+
*/
75+
public function __toString():string{
76+
return $this->data;
6277
}
6378

6479
/**

tests/QRCodeReaderTestAbstract.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ public function testReaderMultiSegment():void{
9797
->addByteSegment($byte)
9898
;
9999

100-
$this::assertSame($numeric.$alphanum.$kanji.$byte, (string)$qrcode->readFromBlob($qrcode->render()));
100+
$result = $qrcode->readFromBlob($qrcode->render());
101+
102+
$this::assertSame($numeric.$alphanum.$kanji.$byte, $result->data);
101103
}
102104

103105
public function dataTestProvider():Generator{
@@ -140,7 +142,7 @@ public function testReadData(Version $version, EccLevel $ecc, string $expected):
140142
$this::markTestSkipped(sprintf('skipped version %s%s: %s', $version, $ecc, $e->getMessage()));
141143
}
142144

143-
$this::assertSame($expected, $result->text);
145+
$this::assertSame($expected, $result->data);
144146
$this::assertSame($version->getVersionNumber(), $result->version->getVersionNumber());
145147
$this::assertSame($ecc->getLevel(), $result->eccLevel->getLevel());
146148
}

0 commit comments

Comments
 (0)