From 5f673da7039624a533551c2d549bb86ebc5e3a87 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Mon, 13 Sep 2021 13:02:33 +0200 Subject: [PATCH] chg: [helpers:bootstrap] Added switch helper --- src/View/Helper/BootstrapHelper.php | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/View/Helper/BootstrapHelper.php b/src/View/Helper/BootstrapHelper.php index b5502d2..447d191 100644 --- a/src/View/Helper/BootstrapHelper.php +++ b/src/View/Helper/BootstrapHelper.php @@ -110,6 +110,12 @@ class BootstrapHelper extends Helper $bsProgressTimeline = new BoostrapProgressTimeline($options, $this); return $bsProgressTimeline->progressTimeline(); } + + public function switch($options) + { + $bsSwitch = new BoostrapSwitch($options, $this); + return $bsSwitch->switch(); + } } class BootstrapGeneric @@ -1015,6 +1021,58 @@ class BoostrapCard extends BootstrapGeneric } } +class BoostrapSwitch extends BootstrapGeneric { + private $defaultOptions = [ + 'label' => '', + 'variant' => 'primary', + 'disabled' => false, + 'checked' => false, + 'title' => '' + ]; + + function __construct($options) { + $this->allowedOptionValues = [ + 'variant' => BootstrapGeneric::$variants, + ]; + $this->processOptions($options); + } + + private function processOptions($options) + { + $this->options = array_merge($this->defaultOptions, $options); + $this->checkOptionValidity(); + } + + public function switch() + { + return $this->genSwitch(); + } + + private function genSwitch() + { + $tmpId = 'tmp-' . mt_rand(); + $html = $this->genNode('div', [ + 'class' => [ + 'custom-control custom-switch', + ], + 'title' => $this->options['title'] + ], implode('', [ + $this->genNode('input', [ + 'type' => "checkbox", + 'class' => 'custom-control-input', + 'id' => $tmpId, + ($this->options['disabled'] ? 'disabled' : '') => '', + ($this->options['checked'] ? 'checked' : '') => $this->options['checked'] ? 'checked' : '' + ]), + $this->genNode('label', [ + 'class' => 'custom-control-label', + 'for' => $tmpId, + ], h($this->options['label'])) + ])); + return $html; + } +} + class BoostrapProgress extends BootstrapGeneric { private $defaultOptions = [ 'value' => 0,