mirror of
https://github.com/chillerlan/php-qrcode.git
synced 2026-09-11 18:26:33 +00:00
:octocat: moved options init
This commit is contained in:
+1
-30
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user