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