From f1766fb9c8b5cb952b0a35eff3393f92b75e3e08 Mon Sep 17 00:00:00 2001 From: Jonatas Souza Date: Mon, 16 Oct 2023 23:17:40 -0300 Subject: [PATCH] patch_fature_to_actions: add label and colors to actions as well so user can dinamically change its behavior --- src/Action/QrCodeAction.php | 43 +++++++++++++++++++++++++++++++ src/Component/QrCodeComponent.php | 11 ++++---- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Action/QrCodeAction.php b/src/Action/QrCodeAction.php index 5c5c315..581a5f5 100644 --- a/src/Action/QrCodeAction.php +++ b/src/Action/QrCodeAction.php @@ -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; @@ -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. @@ -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(); } } diff --git a/src/Component/QrCodeComponent.php b/src/Component/QrCodeComponent.php index 47b9e72..0f61ed9 100644 --- a/src/Component/QrCodeComponent.php +++ b/src/Component/QrCodeComponent.php @@ -24,11 +24,11 @@ * @author Antonio Ramirez * @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) @@ -75,6 +75,7 @@ class QrCodeComponent extends Component * 'r' => 0, // RED * 'g' => 0, // GREEN * 'b' => 0 // BLUE + * 'a => 100 // ALPHA * ] * ``` */