DecoderResult.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Class DecoderResult
  4. *
  5. * @created 17.01.2021
  6. * @author ZXing Authors
  7. * @author Smiley <smiley@chillerlan.net>
  8. * @copyright 2021 Smiley
  9. * @license Apache-2.0
  10. */
  11. namespace chillerlan\QRCode\Decoder;
  12. use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, Version};
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use function property_exists;
  15. /**
  16. * Encapsulates the result of decoding a matrix of bits. This typically
  17. * applies to 2D barcode formats. For now, it contains the raw bytes obtained
  18. * as well as a String interpretation of those bytes, if applicable.
  19. *
  20. * @property \chillerlan\QRCode\Common\BitBuffer $rawBytes
  21. * @property string $data
  22. * @property \chillerlan\QRCode\Common\Version $version
  23. * @property \chillerlan\QRCode\Common\EccLevel $eccLevel
  24. * @property \chillerlan\QRCode\Common\MaskPattern $maskPattern
  25. * @property int $structuredAppendParity
  26. * @property int $structuredAppendSequence
  27. */
  28. final class DecoderResult{
  29. private BitBuffer $rawBytes;
  30. private Version $version;
  31. private EccLevel $eccLevel;
  32. private MaskPattern $maskPattern;
  33. private string $data = '';
  34. private int $structuredAppendParity = -1;
  35. private int $structuredAppendSequence = -1;
  36. /**
  37. * DecoderResult constructor.
  38. */
  39. public function __construct(iterable $properties = null){
  40. if(!empty($properties)){
  41. foreach($properties as $property => $value){
  42. if(!property_exists($this, $property)){
  43. continue;
  44. }
  45. $this->{$property} = $value;
  46. }
  47. }
  48. }
  49. /**
  50. * @return mixed|null
  51. */
  52. public function __get(string $property){
  53. if(property_exists($this, $property)){
  54. return $this->{$property};
  55. }
  56. return null;
  57. }
  58. /**
  59. *
  60. */
  61. public function __toString():string{
  62. return $this->data;
  63. }
  64. /**
  65. *
  66. */
  67. public function hasStructuredAppend():bool{
  68. return $this->structuredAppendParity >= 0 && $this->structuredAppendSequence >= 0;
  69. }
  70. /**
  71. * Returns a QRMatrix instance with the settings and data of the reader result
  72. */
  73. public function getQRMatrix():QRMatrix{
  74. return (new QRMatrix($this->version, $this->eccLevel))
  75. ->initFunctionalPatterns()
  76. ->writeCodewords($this->rawBytes)
  77. ->setFormatInfo($this->maskPattern)
  78. ->mask($this->maskPattern)
  79. ;
  80. }
  81. }