ErrorCorrectionLevel.php 1.3 KB

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