Skip to content

Commit

Permalink
patch_fature_to_actions: add label and colors to actions as well so u…
Browse files Browse the repository at this point in the history
…ser can dinamically change its behavior
  • Loading branch information
2amjsouza committed Oct 17, 2023
1 parent 80859b9 commit f1766fb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
43 changes: 43 additions & 0 deletions src/Action/QrCodeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Da\QrCode\Action;

use Da\QrCode\Component\QrCodeComponent;
use Da\QrCode\Label;
use Yii;
use yii\base\Action;
use yii\web\Response;
Expand All @@ -31,6 +32,28 @@ class QrCodeAction extends Action
* @var string whether the URL parameter is passed via GET or POST. Defaults to 'get'.
*/
public $method = 'get';

/**
* @var string|Label|null
*/
public $label = null;

/**
* @var array|null
* 'r' => 0 //RED
* 'g' => 0 //GREEN
* 'B' => 0 //BLUE
*/
public $background = null;

/**
* @var array|null
* 'r' => 0 //RED
* 'g' => 0 //GREEN
* 'B' => 0 //BLUE
* 'a => 100 //ALPHA
*/
public $foreground = null;
/**
* @var string the qr component name configured on the Yii2 app. The component should have configured all the
* possible options like adding a logo, styling, labelling, etc.
Expand All @@ -49,6 +72,26 @@ public function run()
Yii::$app->response->format = Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', $qrCode->getContentType());

if ($this->label) {
$qrCode->setLabel($this->label);
}

if (is_array($this->background)) {
$qrCode->setBackgroundColor(
$this->background['r'],
$this->background['g'],
$this->background['b'],
);
}

if (is_array($this->foreground)) {
$qrCode->setForegroundColor(
$this->foreground['r'],
$this->foreground['g'],
$this->foreground['b'],
! empty($this->foreground['a']) ? $this->foreground['a'] : 100,
);
}
return $qrCode->setText((string)$text)->writeString();
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/Component/QrCodeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* @author Antonio Ramirez <[email protected]>
* @package Da\QrCode\Component
*
* @method QrCode useForegroundColor(integer $red, integer $green, integer $blue)
* @method QrCode useBackgroundColor(integer $red, integer $green, integer $blue)
* @method QrCode useEncoding(string $encoding)
* @method QrCode useWriter(WriterInterface $writer)
* @method QrCode useLogo(string $logo)
* @method QrCode setForegroundColor(integer $red, integer $green, integer $blue, integer $alpha)
* @method QrCode setBackgroundColor(integer $red, integer $green, integer $blue)
* @method QrCode setEncoding(string $encoding)
* @method QrCode setWriter(WriterInterface $writer)
* @method QrCode setLogo(string $logo)
* @method QrCode setText(string $text)
* @method QrCode setSize(integer $size)
* @method QrCode setLogoWidth(integer $width)
Expand Down Expand Up @@ -75,6 +75,7 @@ class QrCodeComponent extends Component
* 'r' => 0, // RED
* 'g' => 0, // GREEN
* 'b' => 0 // BLUE
* 'a => 100 // ALPHA
* ]
* ```
*/
Expand Down

0 comments on commit f1766fb

Please sign in to comment.