:octocat: moved options init

This commit is contained in:
codemasher
2018-09-19 02:11:03 +02:00
parent 1adba30c24
commit c13e006947
3 changed files with 28 additions and 32 deletions
+1 -30
View File
@@ -108,36 +108,7 @@ class QRCode{
public function __construct(SettingsContainerInterface $options = null){
mb_internal_encoding('UTF-8');
$this->setOptions($options ?? new QROptions);
}
/**
* Sets the options, called internally by the constructor
*
* @param \chillerlan\Settings\SettingsContainerInterface $options
*
* @return \chillerlan\QRCode\QRCode
* @throws \chillerlan\QRCode\QRCodeException
*/
public function setOptions(SettingsContainerInterface $options):QRCode{
if(!array_key_exists($options->eccLevel, $this::ECC_MODES)){
throw new QRCodeException('Invalid error correct level: '.$options->eccLevel);
}
if(!is_array($options->imageTransparencyBG) || count($options->imageTransparencyBG) < 3){
$options->imageTransparencyBG = [255, 255, 255];
}
$options->version = (int)$options->version;
// clamp min/max version number
$options->versionMin = (int)min($options->versionMin, $options->versionMax);
$options->versionMax = (int)max($options->versionMin, $options->versionMax);
$this->options = $options;
return $this;
$this->options = $options ?? new QROptions;
}
/**
+25
View File
@@ -218,4 +218,29 @@ trait QROptionsTrait{
QRMatrix::M_TEST << 8 => true, // 65280
];
/**
* Sets the options, called internally by the constructor
*
* @return void
* @throws \chillerlan\QRCode\QRCodeException
*/
public function QROptionsTrait():void{
if(!array_key_exists($this->eccLevel, QRCode::ECC_MODES)){
throw new QRCodeException('Invalid error correct level: '.$this->eccLevel);
}
if(!is_array($this->imageTransparencyBG) || count($this->imageTransparencyBG) < 3){
$this->imageTransparencyBG = [255, 255, 255];
}
$this->version = (int)$this->version;
// clamp min/max version number
$max = $this->versionMax;
$min = $this->versionMin;
$this->versionMin = (int)min($min, $max);
$this->versionMax = (int)max($min, $max);
}
}
+2 -2
View File
@@ -75,7 +75,7 @@ class QRCodeTest extends QRTestAbstract{
* @expectedExceptionMessage Invalid error correct level: 42
*/
public function testSetOptionsException(){
$this->qrcode->setOptions(new QROptions(['eccLevel' => 42]));
new QROptions(['eccLevel' => 42]);
}
/**
@@ -83,7 +83,7 @@ class QRCodeTest extends QRTestAbstract{
* @expectedExceptionMessage invalid output type
*/
public function testInitDataInterfaceException(){
$this->qrcode->setOptions(new QROptions(['outputType' => 'foo']))->render('test');
(new QRCode(new QROptions(['outputType' => 'foo'])))->render('test');
}
/**