Ver código fonte

:octocat: inject QRDataModeInterface

codemasher 5 anos atrás
pai
commit
abf6c2fd94

+ 3 - 8
src/Data/QRData.php

@@ -60,8 +60,8 @@ final class QRData{
 	/**
 	 * QRData constructor.
 	 *
-	 * @param \chillerlan\Settings\SettingsContainerInterface $options
-	 * @param array|null                                      $dataSegments
+	 * @param \chillerlan\Settings\SettingsContainerInterface    $options
+	 * @param \chillerlan\QRCode\Data\QRDataModeInterface[]|null $dataSegments
 	 */
 	public function __construct(SettingsContainerInterface $options, array $dataSegments = null){
 		$this->options       = $options;
@@ -79,12 +79,7 @@ final class QRData{
 	 * Sets the data string (internally called by the constructor)
 	 */
 	public function setData(array $dataSegments):QRData{
-
-		foreach($dataSegments as $segment){
-			[$class, $data] = $segment;
-
-			$this->dataSegments[] = new $class($data);
-		}
+		$this->dataSegments = $dataSegments;
 
 		$version = $this->options->version === QRCode::VERSION_AUTO
 			? $this->getMinimumVersion()

+ 12 - 15
src/QRCode.php

@@ -12,7 +12,9 @@
 
 namespace chillerlan\QRCode;
 
-use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Kanji, MaskPatternTester, Number, QRData, QRCodeDataException, QRMatrix};
+use chillerlan\QRCode\Data\{
+	AlphaNum, Byte, ECI, Kanji, MaskPatternTester, Number, QRData, QRCodeDataException, QRDataModeInterface, QRMatrix
+};
 use chillerlan\QRCode\Common\{MaskPattern, Mode};
 use chillerlan\QRCode\Output\{QRCodeOutputException, QRFpdf, QRImage, QRImagick, QRMarkup, QROutputInterface, QRString};
 use chillerlan\Settings\SettingsContainerInterface;
@@ -88,7 +90,7 @@ class QRCode{
 	 *
 	 * @see \chillerlan\QRCode\Data\QRDataModeInterface
 	 *
-	 * @var string[][]|int[][]
+	 * @var \chillerlan\QRCode\Data\QRDataModeInterface[]
 	 */
 	protected array $dataSegments = [];
 
@@ -125,7 +127,7 @@ class QRCode{
 			foreach(Mode::DATA_INTERFACES as $dataInterface){
 
 				if($dataInterface::validateString($data)){
-					$this->addSegment($data, $dataInterface);
+					$this->addSegment(new $dataInterface($data));
 
 					break;
 				}
@@ -216,21 +218,16 @@ class QRCode{
 	/**
 	 * 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
-	 *
-	 * @return void
 	 */
-	protected function addSegment($data, string $classname):void{
-		$this->dataSegments[] = [$classname, $data];
+	protected function addSegment(QRDataModeInterface $segment):void{
+		$this->dataSegments[] = $segment;
 	}
 
 	/**
 	 * ISO/IEC 18004:2000 8.3.2 - Numeric Mode
 	 */
 	public function addNumberSegment(string $data):QRCode{
-		$this->addSegment($data, Number::class);
+		$this->addSegment(new Number($data));
 
 		return $this;
 	}
@@ -239,7 +236,7 @@ class QRCode{
 	 * ISO/IEC 18004:2000 8.3.3 - Alphanumeric Mode
 	 */
 	public function addAlphaNumSegment(string $data):QRCode{
-		$this->addSegment($data, AlphaNum::class);
+		$this->addSegment(new AlphaNum($data));
 
 		return $this;
 	}
@@ -248,7 +245,7 @@ class QRCode{
 	 * ISO/IEC 18004:2000 8.3.5 - Kanji Mode
 	 */
 	public function addKanjiSegment(string $data):QRCode{
-		$this->addSegment($data, Kanji::class);
+		$this->addSegment(new Kanji($data));
 
 		return $this;
 	}
@@ -257,7 +254,7 @@ class QRCode{
 	 * ISO/IEC 18004:2000 8.3.4 - 8-bit Byte Mode
 	 */
 	public function addByteSegment(string $data):QRCode{
-		$this->addSegment($data, Byte::class);
+		$this->addSegment(new Byte($data));
 
 		return $this;
 	}
@@ -266,7 +263,7 @@ class QRCode{
 	 * ISO/IEC 18004:2000 8.3.1 - Extended Channel Interpretation (ECI) Mode
 	 */
 	public function addEciDesignator(int $encoding):QRCode{
-		$this->addSegment($encoding, ECI::class);
+		$this->addSegment(new ECI($encoding));
 
 		return $this;
 	}

+ 4 - 2
tests/Data/AlphaNumTest.php

@@ -20,7 +20,7 @@ use chillerlan\QRCode\Data\{AlphaNum, QRCodeDataException};
 final class AlphaNumTest extends DatainterfaceTestAbstract{
 
 	/** @internal */
-	protected array $testdata  = [AlphaNum::class, '0 $%*+-./:'];
+	protected array $testdata = [AlphaNum::class, '0 $%*+-./:'];
 
 	/** @internal */
 	protected array  $expected  = [
@@ -46,7 +46,9 @@ final class AlphaNumTest extends DatainterfaceTestAbstract{
 		$this->expectException(QRCodeDataException::class);
 		$this->expectExceptionMessage('illegal char: "#" [35]');
 
-		$this->dataInterface->setData([[AlphaNum::class, '#']]);
+		$this->testdata = [AlphaNum::class, '#'];
+
+		$this->setTestData();
 	}
 
 }

+ 10 - 4
tests/Data/DatainterfaceTestAbstract.php

@@ -43,6 +43,11 @@ abstract class DatainterfaceTestAbstract extends TestCase{
 		$this->reflection    = new ReflectionClass($this->dataInterface);
 	}
 
+	protected function setTestData():void{
+		[$class, $data] = $this->testdata;
+		$this->dataInterface->setData([new $class($data)]);
+	}
+
 	/**
 	 * Verifies the data interface instance
 	 */
@@ -81,7 +86,7 @@ abstract class DatainterfaceTestAbstract extends TestCase{
 	 * @dataProvider MaskPatternProvider
 	 */
 	public function testInitMatrix(int $maskPattern):void{
-		$this->dataInterface->setData([$this->testdata]);
+		$this->setTestData();
 
 		$matrix = $this->dataInterface->writeMatrix(new MaskPattern($maskPattern));
 
@@ -93,7 +98,7 @@ abstract class DatainterfaceTestAbstract extends TestCase{
 	 * Tests getting the minimum QR version for the given data
 	 */
 	public function testGetMinimumVersion():void{
-		$this->dataInterface->setData([$this->testdata]);
+		$this->setTestData();
 
 		$getMinimumVersion = $this->reflection->getMethod('getMinimumVersion');
 		$getMinimumVersion->setAccessible(true);
@@ -111,7 +116,7 @@ abstract class DatainterfaceTestAbstract extends TestCase{
 
 		$this->dataInterface = new QRData(
 			new QROptions(['version' => QRCode::VERSION_AUTO]),
-			[[$class, str_repeat($data, 1337)]]
+			[new $class(str_repeat($data, 1337))]
 		);
 	}
 
@@ -122,8 +127,9 @@ abstract class DatainterfaceTestAbstract extends TestCase{
 		$this->expectException(QRCodeDataException::class);
 		$this->expectExceptionMessage('code length overflow');
 		[$class, $data] = $this->testdata;
+		$this->testdata = [$class, str_repeat($data, 1337)];
 
-		$this->dataInterface->setData([[$class, str_repeat($data, 1337)]]);
+		$this->setTestData();
 	}
 
 }

+ 6 - 2
tests/Data/KanjiTest.php

@@ -46,7 +46,9 @@ final class KanjiTest extends DatainterfaceTestAbstract{
 		$this->expectException(QRCodeDataException::class);
 		$this->expectExceptionMessage('illegal char at 1 [16191]');
 
-		$this->dataInterface->setData([[Kanji::class, 'ÃÃ']]);
+		$this->testdata = [Kanji::class, 'ÃÃ'];
+
+		$this->setTestData();
 	}
 
 	/**
@@ -56,7 +58,9 @@ final class KanjiTest extends DatainterfaceTestAbstract{
 		$this->expectException(QRCodeDataException::class);
 		$this->expectExceptionMessage('illegal char at 1');
 
-		$this->dataInterface->setData([[Kanji::class, 'Ã']]);
+		$this->testdata = [Kanji::class, 'Ã'];
+
+		$this->setTestData();
 	}
 
 }

+ 2 - 2
tests/Data/MaskPatternTesterTest.php

@@ -26,7 +26,7 @@ final class MaskPatternTesterTest extends TestCase{
 	 * Tests getting the best mask pattern
 	 */
 	public function testMaskpattern():void{
-		$dataInterface = new QRData(new QROptions(['version' => 10]), [[Byte::class, 'test']]);
+		$dataInterface = new QRData(new QROptions(['version' => 10]), [new Byte('test')]);
 
 		$this::assertSame(3, (new MaskPatternTester($dataInterface))->getBestMaskPattern()->getPattern());
 	}
@@ -35,7 +35,7 @@ final class MaskPatternTesterTest extends TestCase{
 	 * Tests getting the penalty value for a given mask pattern
 	 */
 	public function testMaskpatternID():void{
-		$dataInterface = new QRData(new QROptions(['version' => 10]), [[Byte::class, 'test']]);
+		$dataInterface = new QRData(new QROptions(['version' => 10]), [new Byte('test')]);
 
 		$this::assertSame(4243, (new MaskPatternTester($dataInterface))->testPattern(new MaskPattern(MaskPattern::PATTERN_011)));
 	}

+ 3 - 1
tests/Data/NumberTest.php

@@ -46,7 +46,9 @@ final class NumberTest extends DatainterfaceTestAbstract{
 		$this->expectException(QRCodeDataException::class);
 		$this->expectExceptionMessage('illegal char: "#" [35]');
 
-		$this->dataInterface->setData([[Number::class, '#']]);
+		$this->testdata = [Number::class, '#'];
+
+		$this->setTestData();
 	}
 
 }

+ 1 - 1
tests/Output/QROutputTestAbstract.php

@@ -49,7 +49,7 @@ abstract class QROutputTestAbstract extends TestCase{
 		}
 
 		$this->options         = new QROptions;
-		$this->matrix          = (new QRData($this->options, [[Byte::class, 'testdata']]))
+		$this->matrix          = (new QRData($this->options, [new Byte('testdata')]))
 			->writeMatrix(new MaskPattern(MaskPattern::PATTERN_010));
 		$this->outputInterface = $this->getOutputInterface($this->options);
 	}