codemasher 5 лет назад
Родитель
Сommit
0a1ceecdd8
3 измененных файлов с 42 добавлено и 32 удалено
  1. 2 2
      README.md
  2. 19 15
      src/Common/MaskPattern.php
  3. 21 15
      src/QRCode.php

+ 2 - 2
README.md

@@ -60,9 +60,9 @@ via terminal: `composer require chillerlan/php-qrcode`
 }
 ```
 
-Note: replace `dev-main` with a [version constraint](https://getcomposer.org/doc/articles/versions.md#writing-version-constraints), e.g. `^3.2` - see [releases](https://github.com/chillerlan/php-qrcode/releases) for valid versions.
+Note: replace `dev-main` with a [version constraint](https://getcomposer.org/doc/articles/versions.md#writing-version-constraints), e.g. `^4.3` - see [releases](https://github.com/chillerlan/php-qrcode/releases) for valid versions.
 For PHP version ... 
-  - 7.4+ use `^4.2`
+  - 7.4+ use `^4.3`
   - 7.2+ use `^3.3`
   - 7.0+ use `^2.0` (PHP 7.0 and 7.1 are EOL!)
   - 5.6+ use `^1.0` (please let PHP 5 die!)

+ 19 - 15
src/Common/MaskPattern.php

@@ -16,9 +16,9 @@ use chillerlan\QRCode\QRCodeException;
 use Closure;
 
 /**
- *
+ * ISO/IEC 18004:2000 Section 8.8.1
  */
-class MaskPattern{
+final class MaskPattern{
 
 	public const PATTERN_000 = 0b000;
 	public const PATTERN_001 = 0b001;
@@ -45,39 +45,43 @@ class MaskPattern{
 	/**
 	 * MaskPattern constructor.
 	 *
-	 * ISO/IEC 18004:2000 Section 8.8.1
-	 *
 	 * @throws \chillerlan\QRCode\QRCodeException
 	 */
 	public function __construct(int $maskPattern){
 
 		if((0b111 & $maskPattern) !== $maskPattern){
-			throw new QRCodeException('invalid mask pattern'); // @codeCoverageIgnore
+			throw new QRCodeException('invalid mask pattern');
 		}
 
 		$this->maskPattern = $maskPattern;
 	}
 
+	/**
+	 * Returns the current mask pattern
+	 */
 	public function getPattern():int{
 		return $this->maskPattern;
 	}
 
 	/**
-	 * ISO/IEC 18004:2000 Section 8.8.1
+	 * Returns a closure that applies the mask for the chosen mask pattern.
 	 *
 	 * Note that some versions of the QR code standard have had errors in the section about mask patterns.
-	 * The information below has been corrected. (https://www.thonky.com/qr-code-tutorial/mask-patterns)
+	 * The information below has been corrected.
+	 *
+	 * @see https://www.thonky.com/qr-code-tutorial/mask-patterns
 	 */
 	public function getMask():Closure{
+		// $x = column (width), $y = row (height)
 		return [
-			self::PATTERN_000 => fn($x, $y):int => ($x + $y) % 2,
-			self::PATTERN_001 => fn($x, $y):int => $y % 2,
-			self::PATTERN_010 => fn($x, $y):int => $x % 3,
-			self::PATTERN_011 => fn($x, $y):int => ($x + $y) % 3,
-			self::PATTERN_100 => fn($x, $y):int => ((int)($y / 2) + (int)($x / 3)) % 2,
-			self::PATTERN_101 => fn($x, $y):int => (($x * $y) % 2) + (($x * $y) % 3),
-			self::PATTERN_110 => fn($x, $y):int => ((($x * $y) % 2) + (($x * $y) % 3)) % 2,
-			self::PATTERN_111 => fn($x, $y):int => ((($x * $y) % 3) + (($x + $y) % 2)) % 2,
+			self::PATTERN_000 => fn(int $x, int $y):int => ($x + $y) % 2,
+			self::PATTERN_001 => fn(int $x, int $y):int => $y % 2,
+			self::PATTERN_010 => fn(int $x, int $y):int => $x % 3,
+			self::PATTERN_011 => fn(int $x, int $y):int => ($x + $y) % 3,
+			self::PATTERN_100 => fn(int $x, int $y):int => ((int)($y / 2) + (int)($x / 3)) % 2,
+			self::PATTERN_101 => fn(int $x, int $y):int => (($x * $y) % 2) + (($x * $y) % 3),
+			self::PATTERN_110 => fn(int $x, int $y):int => ((($x * $y) % 2) + (($x * $y) % 3)) % 2,
+			self::PATTERN_111 => fn(int $x, int $y):int => ((($x * $y) % 3) + (($x + $y) % 2)) % 2,
 		][$this->maskPattern];
 	}
 

+ 21 - 15
src/QRCode.php

@@ -13,11 +13,8 @@
 namespace chillerlan\QRCode;
 
 use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Kanji, MaskPatternTester, Number, QRData, QRCodeDataException, QRMatrix};
-use chillerlan\QRCode\Common\MaskPattern;
-use chillerlan\QRCode\Common\Mode;
-use chillerlan\QRCode\Output\{
-	QRCodeOutputException, QRFpdf, QRImage, QRImagick, QRMarkup, QROutputInterface, QRString
-};
+use chillerlan\QRCode\Common\{MaskPattern, Mode};
+use chillerlan\QRCode\Output\{QRCodeOutputException, QRFpdf, QRImage, QRImagick, QRMarkup, QROutputInterface, QRString};
 use chillerlan\Settings\SettingsContainerInterface;
 
 use function class_exists, in_array;
@@ -140,8 +137,6 @@ class QRCode{
 		return $this->initOutputInterface()->dump($file);
 	}
 
-
-
 	/**
 	 * Returns a QRMatrix object for the given $data and current QROptions
 	 *
@@ -192,8 +187,6 @@ class QRCode{
 
 	/**
 	 * checks if a string qualifies as numeric (convenience method)
-	 *
-	 * @see Number::validateString()
 	 */
 	public function isNumber(string $string):bool{
 		return Number::validateString($string);
@@ -201,8 +194,6 @@ class QRCode{
 
 	/**
 	 * checks if a string qualifies as alphanumeric (convenience method)
-	 *
-	 * @see AlphaNum::validateString()
 	 */
 	public function isAlphaNum(string $string):bool{
 		return AlphaNum::validateString($string);
@@ -210,8 +201,6 @@ class QRCode{
 
 	/**
 	 * checks if a string qualifies as Kanji (convenience method)
-	 *
-	 * @see Kanji::validateString()
 	 */
 	public function isKanji(string $string):bool{
 		return Kanji::validateString($string);
@@ -219,14 +208,15 @@ class QRCode{
 
 	/**
 	 * a dummy (convenience method)
-	 *
-	 * @see Byte::validateString()
 	 */
 	public function isByte(string $string):bool{
 		return Byte::validateString($string);
 	}
 
 	/**
+	 * ISO/IEC 18004:2000 8.3.6 - Mixing modes
+	 * ISO/IEC 18004:2000 Annex H - Optimisation of bit stream length
+	 *
 	 * @param string|int $data
 	 * @param string     $classname
 	 *
@@ -236,33 +226,49 @@ class QRCode{
 		$this->dataSegments[] = [$classname, $data];
 	}
 
+	/**
+	 * ISO/IEC 18004:2000 8.3.2 - Numeric Mode
+	 */
 	public function addNumberSegment(string $data):QRCode{
 		$this->addSegment($data, Number::class);
 
 		return $this;
 	}
 
+	/**
+	 * ISO/IEC 18004:2000 8.3.3 - Alphanumeric Mode
+	 */
 	public function addAlphaNumSegment(string $data):QRCode{
 		$this->addSegment($data, AlphaNum::class);
 
 		return $this;
 	}
 
+	/**
+	 * ISO/IEC 18004:2000 8.3.5 - Kanji Mode
+	 */
 	public function addKanjiSegment(string $data):QRCode{
 		$this->addSegment($data, Kanji::class);
 
 		return $this;
 	}
 
+	/**
+	 * ISO/IEC 18004:2000 8.3.4 - 8-bit Byte Mode
+	 */
 	public function addByteSegment(string $data):QRCode{
 		$this->addSegment($data, Byte::class);
 
 		return $this;
 	}
 
+	/**
+	 * ISO/IEC 18004:2000 8.3.1 - Extended Channel Interpretation (ECI) Mode
+	 */
 	public function addEciDesignator(int $encoding):QRCode{
 		$this->addSegment($encoding, ECI::class);
 
 		return $this;
 	}
+
 }