@@ -29,10 +29,14 @@ final class Encoder
2929 /**
3030 * @return bool[][]
3131 */
32- public static function encode (string $ data , int $ errorCorrectionLevel = ErrorCorrectionLevel::M, ?int $ version = null ): array
32+ public static function encode (string $ data , int $ errorCorrectionLevel = ErrorCorrectionLevel::M, ?int $ version = null , ? int $ maskPattern = null ): array
3333 {
3434 ErrorCorrectionLevel::validate ($ errorCorrectionLevel );
3535
36+ if ($ maskPattern !== null && ($ maskPattern < 0 || $ maskPattern > 7 )) {
37+ throw new \InvalidArgumentException ("Invalid mask pattern: $ maskPattern (must be 0-7) " );
38+ }
39+
3640 $ mode = ModeDetector::detect ($ data );
3741 $ qrData = self ::createDataObject ($ data , $ mode );
3842
@@ -53,7 +57,7 @@ public static function encode(string $data, int $errorCorrectionLevel = ErrorCor
5357 self ::setupPositionAdjustPattern ($ modules , $ moduleCount , $ version );
5458 self ::setupTimingPattern ($ modules , $ moduleCount );
5559
56- $ bestPattern = self ::findBestMaskPattern ($ modules , $ moduleCount , $ version , $ errorCorrectionLevel , [$ qrData ]);
60+ $ bestPattern = $ maskPattern ?? self ::findBestMaskPattern ($ modules , $ moduleCount , $ version , $ errorCorrectionLevel , [$ qrData ]);
5761
5862 self ::setupTypeInfo ($ modules , $ moduleCount , false , $ bestPattern , $ errorCorrectionLevel );
5963
@@ -222,6 +226,7 @@ private static function findBestMaskPattern(array &$modules, int $moduleCount, i
222226 $ data = self ::createEncodedData ($ version , $ errorCorrectionLevel , $ dataList );
223227 self ::mapData ($ testModules , $ moduleCount , $ data , $ pattern );
224228
229+ /** @var array<int, array<int, bool>> $boolMatrix */
225230 $ boolMatrix = self ::toBoolMatrix ($ testModules , $ moduleCount );
226231 $ lostPoint = MaskPattern::getLostPoint ($ boolMatrix , $ moduleCount );
227232
@@ -293,8 +298,11 @@ private static function createBytes(BitBuffer $buffer, array $rsBlocks): array
293298 $ maxEcCount = 0 ;
294299
295300 $ rsBlockCount = count ($ rsBlocks );
296- $ dcdata = array_fill (0 , $ rsBlockCount , []);
297- $ ecdata = array_fill (0 , $ rsBlockCount , []);
301+
302+ /** @var array<int, int[]> $dcdata */
303+ $ dcdata = [];
304+ /** @var array<int, int[]> $ecdata */
305+ $ ecdata = [];
298306
299307 for ($ r = 0 ; $ r < $ rsBlockCount ; $ r ++) {
300308 $ dcCount = $ rsBlocks [$ r ]->getDataCount ();
@@ -303,32 +311,37 @@ private static function createBytes(BitBuffer $buffer, array $rsBlocks): array
303311 $ maxDcCount = max ($ maxDcCount , $ dcCount );
304312 $ maxEcCount = max ($ maxEcCount , $ ecCount );
305313
306- $ dcdata [$ r ] = array_fill (0 , $ dcCount , 0 );
314+ /** @var int[] $dcRow */
315+ $ dcRow = array_fill (0 , $ dcCount , 0 );
307316 $ bdata = $ buffer ->getBuffer ();
308317
309318 for ($ i = 0 ; $ i < $ dcCount ; $ i ++) {
310- $ dcdata [ $ r ] [$ i ] = 0xFF & $ bdata [$ i + $ offset ];
319+ $ dcRow [$ i ] = 0xFF & $ bdata [$ i + $ offset ];
311320 }
321+ $ dcdata [$ r ] = $ dcRow ;
312322 $ offset += $ dcCount ;
313323
314324 $ rsPoly = self ::getErrorCorrectPolynomial ($ ecCount );
315325 $ rawPoly = new Polynomial ($ dcdata [$ r ], $ rsPoly ->getLength () - 1 );
316326 $ modPoly = $ rawPoly ->mod ($ rsPoly );
317327
318- $ ecdata [$ r ] = array_fill (0 , $ rsPoly ->getLength () - 1 , 0 );
319- $ ecDataCount = count ($ ecdata [$ r ]);
328+ $ ecDataCount = $ rsPoly ->getLength () - 1 ;
329+ /** @var int[] $ecRow */
330+ $ ecRow = array_fill (0 , $ ecDataCount , 0 );
320331
321332 for ($ i = 0 ; $ i < $ ecDataCount ; $ i ++) {
322333 $ modIndex = $ i + $ modPoly ->getLength () - $ ecDataCount ;
323- $ ecdata [ $ r ] [$ i ] = ($ modIndex >= 0 ) ? $ modPoly ->get ($ modIndex ) : 0 ;
334+ $ ecRow [$ i ] = ($ modIndex >= 0 ) ? $ modPoly ->get ($ modIndex ) : 0 ;
324335 }
336+ $ ecdata [$ r ] = $ ecRow ;
325337 }
326338
327339 $ totalCodeCount = 0 ;
328340 for ($ i = 0 ; $ i < $ rsBlockCount ; $ i ++) {
329341 $ totalCodeCount += $ rsBlocks [$ i ]->getTotalCount ();
330342 }
331343
344+ /** @var int[] $data */
332345 $ data = array_fill (0 , $ totalCodeCount , 0 );
333346 $ index = 0 ;
334347
0 commit comments