Debian Codegraph @

src/Common/ErrorCorrectionLevel.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace BaconQrCode\Common;
5
6 use BaconQrCode\Exception\OutOfBoundsException;
7 use DASPRiD\Enum\AbstractEnum;
8
9 /**
10 * Enum representing the four error correction levels.
11 *
12 * @method static self L() ~7% correction
13 * @method static self M() ~15% correction
14 * @method static self Q() ~25% correction
15 * @method static self H() ~30% correction
16 */
17 final class ErrorCorrectionLevel extends AbstractEnum
18 {
19 protected const L = [0x01];
20 protected const M = [0x00];
21 protected const Q = [0x03];
22 protected const H = [0x02];
23
24 protected function __construct(private readonly int $bits)
25 {
26 }
27
28 /**
29 * @throws OutOfBoundsException if number of bits is invalid
30 */
31 public static function forBits(int $bits) : self
32 {
33 switch ($bits) {
34 case 0:
35 return self::M();
36
37 case 1:
38 return self::L();
39
40 case 2:
41 return self::H();
42
43 case 3:
44 return self::Q();
45 }
46
47 throw new OutOfBoundsException('Invalid number of bits');
48 }
49
50 /**
51 * Returns the two bits used to encode this error correction level.
52 */
53 public function getBits() : int
54 {
55 return $this->bits;
56 }
57 }