EcBlocks.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCode\Common;
  4. /**
  5. * Encapsulates a set of error-correction blocks in one symbol version.
  6. *
  7. * Most versions will use blocks of differing sizes within one version, so, this encapsulates the parameters for each
  8. * set of blocks. It also holds the number of error-correction codewords per block since it will be the same across all
  9. * blocks within one version.
  10. */
  11. final class EcBlocks
  12. {
  13. /**
  14. * Number of EC codewords per block.
  15. *
  16. * @var int
  17. */
  18. private $ecCodewordsPerBlock;
  19. /**
  20. * List of EC blocks.
  21. *
  22. * @var EcBlock[]
  23. */
  24. private $ecBlocks;
  25. public function __construct(int $ecCodewordsPerBlock, EcBlock ...$ecBlocks)
  26. {
  27. $this->ecCodewordsPerBlock = $ecCodewordsPerBlock;
  28. $this->ecBlocks = $ecBlocks;
  29. }
  30. /**
  31. * Returns the number of EC codewords per block.
  32. */
  33. public function getEcCodewordsPerBlock() : int
  34. {
  35. return $this->ecCodewordsPerBlock;
  36. }
  37. /**
  38. * Returns the total number of EC block appearances.
  39. */
  40. public function getNumBlocks() : int
  41. {
  42. $total = 0;
  43. foreach ($this->ecBlocks as $ecBlock) {
  44. $total += $ecBlock->getCount();
  45. }
  46. return $total;
  47. }
  48. /**
  49. * Returns the total count of EC codewords.
  50. */
  51. public function getTotalEcCodewords() : int
  52. {
  53. return $this->ecCodewordsPerBlock * $this->getNumBlocks();
  54. }
  55. /**
  56. * Returns the EC blocks included in this collection.
  57. *
  58. * @return EcBlock[]
  59. */
  60. public function getEcBlocks() : array
  61. {
  62. return $this->ecBlocks;
  63. }
  64. }