🚿 refeactored away some superfluous constants

This commit is contained in:
codemasher
2021-11-27 14:01:14 +01:00
parent 9fde82f9c2
commit 3d563bfcc0
7 changed files with 44 additions and 56 deletions
+20 -30
View File
@@ -30,32 +30,6 @@ final class EccLevel{
/** @var int */
public const H = 0b10; // 30%.
/**
* References to the keys of the following tables:
*
* @see \chillerlan\QRCode\Common\Version::MAX_BITS
* @see \chillerlan\QRCode\Common\EccLevel::RSBLOCKS
* @see \chillerlan\QRCode\Common\EccLevel::formatPattern
*
* @var int[]
*/
public const MODES = [
self::L => 0,
self::M => 1,
self::Q => 2,
self::H => 3,
];
/**
* @var string[]
*/
public const MODES_STRING = [
self::L => 'L',
self::M => 'M',
self::Q => 'Q',
self::H => 'H',
];
/**
* ISO/IEC 18004:2000 Tables 7-11 - Number of symbol characters and input data capacity for versions 1 to 40
*
@@ -183,7 +157,12 @@ final class EccLevel{
* returns the string representation of the current ECC level
*/
public function __toString():string{
return self::MODES_STRING[$this->eccLevel];
return [
self::L => 'L',
self::M => 'M',
self::Q => 'Q',
self::H => 'H',
][$this->eccLevel];
}
/**
@@ -195,16 +174,27 @@ final class EccLevel{
/**
* returns the ordinal value of the current ECC level
*
* references to the keys of the following tables:
*
* @see \chillerlan\QRCode\Common\EccLevel::MAX_BITS
* @see \chillerlan\QRCode\Common\EccLevel::FORMAT_PATTERN
* @see \chillerlan\QRCode\Common\Version::RSBLOCKS
*/
public function getOrdinal():int{
return self::MODES[$this->eccLevel];
return [
self::L => 0,
self::M => 1,
self::Q => 2,
self::H => 3,
][$this->eccLevel];
}
/**
* returns the format pattern for the given $eccLevel and $maskPattern
*/
public function getformatPattern(MaskPattern $maskPattern):int{
return self::FORMAT_PATTERN[self::MODES[$this->eccLevel]][$maskPattern->getPattern()];
return self::FORMAT_PATTERN[$this->getOrdinal()][$maskPattern->getPattern()];
}
/**
@@ -215,7 +205,7 @@ final class EccLevel{
public function getMaxBits():array{
return array_combine(
array_keys(self::MAX_BITS),
array_column(self::MAX_BITS, self::MODES[$this->eccLevel])
array_column(self::MAX_BITS, $this->getOrdinal())
);
}
-14
View File
@@ -63,20 +63,6 @@ final class Mode{
self::BYTE => Byte::class,
];
/**
* References to the keys of the following table(s):
*
* @see \chillerlan\QRCode\Common\Version::MAX_LENGTH
*
* @var int[]
*/
public const MODES = [
self::NUMBER => 0,
self::ALPHANUM => 1,
self::BYTE => 2,
self::KANJI => 3,
];
/**
* returns the length bits for the version breakpoints 1-9, 10-26 and 27-40
*
+15 -1
View File
@@ -311,9 +311,23 @@ final class Version{
/**
* the maximum character count for the given $mode and $eccLevel
*
* @throws \chillerlan\QRCode\QRCodeException
*/
public function getMaxLengthForMode(int $mode, EccLevel $eccLevel):?int{
return self::MAX_LENGTH[$this->version][Mode::MODES[$mode]][$eccLevel->getOrdinal()] ?? null;
$dataModes = [
Mode::NUMBER => 0,
Mode::ALPHANUM => 1,
Mode::BYTE => 2,
Mode::KANJI => 3,
];
if(!isset($dataModes[$mode])){
throw new QRCodeException('invalid $mode');
}
return self::MAX_LENGTH[$this->version][$dataModes[$mode]][$eccLevel->getOrdinal()] ?? null;
}
/**
+6 -8
View File
@@ -29,24 +29,22 @@ use function class_exists, class_implements, in_array, mb_convert_encoding, mb_d
class QRCode{
/** @var int */
public const VERSION_AUTO = -1;
public const VERSION_AUTO = -1;
/** @var int */
public const MASK_PATTERN_AUTO = -1;
public const MASK_PATTERN_AUTO = -1;
/**
* @deprecated backward compatibility
* @see \chillerlan\QRCode\Common\EccLevel
*/
/** @var int */
public const ECC_L = EccLevel::L;
public const ECC_L = EccLevel::L;
/** @var int */
public const ECC_M = EccLevel::M;
public const ECC_M = EccLevel::M;
/** @var int */
public const ECC_Q = EccLevel::Q;
public const ECC_Q = EccLevel::Q;
/** @var int */
public const ECC_H = EccLevel::H;
/** @var int[] */
public const ECC_MODES = EccLevel::MODES;
public const ECC_H = EccLevel::H;
/** @var string */
public const OUTPUT_MARKUP_HTML = 'html';
+1 -1
View File
@@ -256,7 +256,7 @@ trait QROptionsTrait{
*/
protected function set_eccLevel(int $eccLevel):void{
if(!isset(EccLevel::MODES[$eccLevel])){
if(!in_array($eccLevel, [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H], true)){
throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel));
}
+1 -1
View File
@@ -68,7 +68,7 @@ final class QRMatrixTest extends TestCase{
* Tests if eccLevel() returns the current (given) ECC level
*/
public function testECC():void{
$this::assertSame(EccLevel::MODES[EccLevel::L], $this->matrix->eccLevel()->getOrdinal());
$this::assertSame(EccLevel::L, $this->matrix->eccLevel()->getLevel());
}
/**
+1 -1
View File
@@ -104,7 +104,7 @@ class QRCodeReaderTest extends TestCase{
foreach(range(1, 40) as $v){
$version = new Version($v);
foreach(EccLevel::MODES as $ecc => $_){
foreach([EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H] as $ecc){
$eccLevel = new EccLevel($ecc);
yield 'version: '.$version.$eccLevel => [