From abd9e04a0ff9a0b90b668703d983edeaeff23484 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Mon, 28 Nov 2022 10:01:18 +0100 Subject: [PATCH] chg: [helper:bootstrap] Added more documentation and typing --- .../BootstrapElements/BootstrapAccordion.php | 13 ++++--- .../BootstrapElements/BootstrapAlert.php | 10 ++--- .../BootstrapElements/BootstrapBadge.php | 8 ++-- .../BootstrapElements/BootstrapButton.php | 12 +++--- .../BootstrapElements/BootstrapCard.php | 14 +++---- .../BootstrapElements/BootstrapCollapse.php | 13 ++++--- .../BootstrapDropdownMenu.php | 19 +++++----- .../BootstrapElements/BootstrapIcon.php | 8 ++-- .../BootstrapElements/BootstrapListGroup.php | 8 ++-- .../BootstrapElements/BootstrapListTable.php | 23 ++++++------ .../BootstrapElements/BootstrapModal.php | 22 +++++------ .../BootstrapNotificationBubble.php | 8 ++-- .../BootstrapElements/BootstrapProgress.php | 6 +-- .../BootstrapProgressTimeline.php | 12 +++--- .../BootstrapElements/BootstrapSwitch.php | 8 ++-- .../BootstrapElements/BootstrapTable.php | 37 +++++++++++-------- .../BootstrapElements/BootstrapTabs.php | 20 +++++----- .../BootstrapElements/BootstrapToast.php | 8 ++-- src/View/Helper/BootstrapHelper.php | 4 +- 19 files changed, 131 insertions(+), 122 deletions(-) diff --git a/src/View/Helper/BootstrapElements/BootstrapAccordion.php b/src/View/Helper/BootstrapElements/BootstrapAccordion.php index 9bce200..6f0688c 100644 --- a/src/View/Helper/BootstrapElements/BootstrapAccordion.php +++ b/src/View/Helper/BootstrapElements/BootstrapAccordion.php @@ -3,6 +3,7 @@ namespace App\View\Helper\BootstrapElements; use App\View\Helper\BootstrapGeneric; +use App\View\Helper\BootstrapHelper; /** * Creates an collapsible accordion component @@ -53,7 +54,7 @@ class BootstrapAccordion extends BootstrapGeneric 'class' => [], ]; - function __construct($options, $content, $btHelper) + function __construct(array $options, array $content, BootstrapHelper $btHelper) { $this->allowedOptionValues = []; $this->content = $content; @@ -61,7 +62,7 @@ class BootstrapAccordion extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); @@ -78,12 +79,12 @@ class BootstrapAccordion extends BootstrapGeneric } } - public function accordion() + public function accordion(): string { return $this->genAccordion(); } - private function genHeader($accordionItem, $i) + private function genHeader(array $accordionItem, int $i): string { $html = $this->nodeOpen('h2', [ 'class' => ['accordion-header'], @@ -110,7 +111,7 @@ class BootstrapAccordion extends BootstrapGeneric return $html; } - private function genBody($accordionItem, $i) + private function genBody(array $accordionItem, int $i): string { $content = $this->node('div', [ 'class' => ['accordion-body'] @@ -134,7 +135,7 @@ class BootstrapAccordion extends BootstrapGeneric return $html; } - private function genAccordion() + private function genAccordion(): string { $html = $this->nodeOpen('div', [ 'class' => array_merge(['accordion'], $this->options['class']), diff --git a/src/View/Helper/BootstrapElements/BootstrapAlert.php b/src/View/Helper/BootstrapElements/BootstrapAlert.php index 49c0240..8bff3c1 100644 --- a/src/View/Helper/BootstrapElements/BootstrapAlert.php +++ b/src/View/Helper/BootstrapElements/BootstrapAlert.php @@ -34,7 +34,7 @@ class BootstrapAlert extends BootstrapGeneric 'class' => [], ]; - function __construct($options) + function __construct(array $options) { $this->allowedOptionValues = [ 'variant' => BootstrapGeneric::$variants, @@ -42,19 +42,19 @@ class BootstrapAlert extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $this->checkOptionValidity(); } - public function alert() + public function alert(): string { return $this->genAlert(); } - private function genAlert() + private function genAlert(): string { $html = $this->nodeOpen('div', [ 'class' => array_merge([ @@ -72,7 +72,7 @@ class BootstrapAlert extends BootstrapGeneric return $html; } - private function genCloseButton() + private function genCloseButton(): string { $html = ''; if ($this->options['dismissible']) { diff --git a/src/View/Helper/BootstrapElements/BootstrapBadge.php b/src/View/Helper/BootstrapElements/BootstrapBadge.php index d4cf856..3538334 100644 --- a/src/View/Helper/BootstrapElements/BootstrapBadge.php +++ b/src/View/Helper/BootstrapElements/BootstrapBadge.php @@ -34,7 +34,7 @@ class BootstrapBadge extends BootstrapGeneric 'class' => [], ]; - function __construct($options) + function __construct(array $options) { $this->allowedOptionValues = [ 'variant' => BootstrapGeneric::$variants, @@ -42,19 +42,19 @@ class BootstrapBadge extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $this->checkOptionValidity(); } - public function badge() + public function badge(): string { return $this->genBadge(); } - private function genBadge() + private function genBadge(): string { $html = $this->node('span', [ 'class' => array_merge($this->options['class'], [ diff --git a/src/View/Helper/BootstrapElements/BootstrapButton.php b/src/View/Helper/BootstrapElements/BootstrapButton.php index 7cf2b05..458680c 100644 --- a/src/View/Helper/BootstrapElements/BootstrapButton.php +++ b/src/View/Helper/BootstrapElements/BootstrapButton.php @@ -53,7 +53,7 @@ class BootstrapButton extends BootstrapGeneric private $bsClasses = []; - function __construct($options) + function __construct(array $options) { $this->allowedOptionValues = [ 'variant' => array_merge(BootstrapGeneric::$variants, ['link', 'text']), @@ -63,7 +63,7 @@ class BootstrapButton extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); @@ -91,12 +91,12 @@ class BootstrapButton extends BootstrapGeneric } } - public function button() + public function button(): string { return $this->genButton(); } - private function genButton() + private function genButton(): string { $html = $this->nodeOpen($this->options['nodeType'], array_merge($this->options['attrs'], [ 'class' => array_merge($this->options['class'], $this->bsClasses), @@ -116,7 +116,7 @@ class BootstrapButton extends BootstrapGeneric return $html; } - private function genIcon() + private function genIcon(): string { if (!empty($this->options['icon'])) { $bsIcon = new BootstrapIcon($this->options['icon'], [ @@ -127,7 +127,7 @@ class BootstrapButton extends BootstrapGeneric return ''; } - private function genImage() + private function genImage(): string { if (!empty($this->options['image'])) { return $this->node('img', [ diff --git a/src/View/Helper/BootstrapElements/BootstrapCard.php b/src/View/Helper/BootstrapElements/BootstrapCard.php index 3053660..47ecd68 100644 --- a/src/View/Helper/BootstrapElements/BootstrapCard.php +++ b/src/View/Helper/BootstrapElements/BootstrapCard.php @@ -41,7 +41,7 @@ class BootstrapCard extends BootstrapGeneric 'footerClass' => '', ]; - public function __construct($options) + public function __construct(array $options) { $this->allowedOptionValues = [ 'headerVariant' => array_merge(BootstrapGeneric::$variants, ['']), @@ -51,7 +51,7 @@ class BootstrapCard extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['headerClass'] = $this->convertToArrayIfNeeded($this->options['headerClass']); @@ -61,12 +61,12 @@ class BootstrapCard extends BootstrapGeneric $this->options['borderVariant'] = !empty($this->options['headerVariant']) ? "border-{$this->options['headerVariant']}" : ''; } - public function card() + public function card(): string { return $this->genCard(); } - private function genCard() + private function genCard(): string { $card = $this->node('div', [ 'class' => array_merge( @@ -80,7 +80,7 @@ class BootstrapCard extends BootstrapGeneric return $card; } - private function genHeader() + private function genHeader(): string { if (empty($this->options['headerHTML']) && empty($this->options['headerText'])) { return ''; @@ -98,7 +98,7 @@ class BootstrapCard extends BootstrapGeneric return $header; } - private function genBody() + private function genBody(): string { if (empty($this->options['bodyHTML']) && empty($this->options['bodyText'])) { return ''; @@ -116,7 +116,7 @@ class BootstrapCard extends BootstrapGeneric return $body; } - private function genFooter() + private function genFooter(): string { if (empty($this->options['footerHTML']) && empty($this->options['footerText'])) { return ''; diff --git a/src/View/Helper/BootstrapElements/BootstrapCollapse.php b/src/View/Helper/BootstrapElements/BootstrapCollapse.php index 4441d2d..8d90f79 100644 --- a/src/View/Helper/BootstrapElements/BootstrapCollapse.php +++ b/src/View/Helper/BootstrapElements/BootstrapCollapse.php @@ -5,6 +5,7 @@ namespace App\View\Helper\BootstrapElements; use Cake\Utility\Security; use App\View\Helper\BootstrapGeneric; +use App\View\Helper\BootstrapHelper; /** * Creates a Bootstrap collapsible component @@ -45,7 +46,7 @@ class BootstrapCollapse extends BootstrapGeneric 'attrs' => [], ]; - function __construct($options, $content, $btHelper) + function __construct(array $options, string $content, BootstrapHelper $btHelper) { $this->allowedOptionValues = []; $this->processOptions($options); @@ -53,7 +54,7 @@ class BootstrapCollapse extends BootstrapGeneric $this->btHelper = $btHelper; } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); @@ -73,12 +74,12 @@ class BootstrapCollapse extends BootstrapGeneric $this->checkOptionValidity(); } - public function collapse() + public function collapse(): string { return $this->genCollapse(); } - private function genControl() + private function genControl(): string { $attrsConfig = [ 'data-bs-toggle' => 'collapse', @@ -101,7 +102,7 @@ class BootstrapCollapse extends BootstrapGeneric return $html; } - private function genContent() + private function genContent(): string { $cardConfig = $this->options['card']; $cardConfig['bodyHTML'] = $this->content; @@ -113,7 +114,7 @@ class BootstrapCollapse extends BootstrapGeneric return $container; } - private function genCollapse() + private function genCollapse(): string { return $this->genControl() . $this->genContent(); } diff --git a/src/View/Helper/BootstrapElements/BootstrapDropdownMenu.php b/src/View/Helper/BootstrapElements/BootstrapDropdownMenu.php index 2bea216..e3a64c6 100644 --- a/src/View/Helper/BootstrapElements/BootstrapDropdownMenu.php +++ b/src/View/Helper/BootstrapElements/BootstrapDropdownMenu.php @@ -3,6 +3,7 @@ namespace App\View\Helper\BootstrapElements; use App\View\Helper\BootstrapGeneric; +use App\View\Helper\BootstrapHelper; /** * # Options @@ -65,7 +66,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric 'attrs' => [], ]; - function __construct($options, $btHelper) + function __construct(array $options, BootstrapHelper $btHelper) { $this->allowedOptionValues = [ 'direction' => ['start', 'end', 'up', 'down'], @@ -77,24 +78,24 @@ class BootstrapDropdownMenu extends BootstrapGeneric $this->btHelper = $btHelper; } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['dropdown-class'] = $this->convertToArrayIfNeeded($this->options['dropdown-class']); $this->checkOptionValidity(); } - public function dropdownMenu() + public function dropdownMenu(): string { return $this->fullDropdown(); } - public function fullDropdown() + public function fullDropdown(): string { return $this->genDropdownWrapper($this->genDropdownToggleButton(), $this->genDropdownMenu($this->menu)); } - public function genDropdownWrapper($toggle = '', $menu = '', $direction = null, $classes = null) + public function genDropdownWrapper(string $toggle = '', string $menu = '', $direction = null, $classes = null): string { $classes = !is_null($classes) ? $classes : $this->options['dropdown-class']; $direction = !is_null($direction) ? $direction : $this->options['direction']; @@ -114,7 +115,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric return $html; } - public function genDropdownToggleButton() + public function genDropdownToggleButton(): string { $defaultOptions = [ 'class' => ['dropdown-toggle'], @@ -127,7 +128,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric return $this->btHelper->button($options); } - private function genDropdownMenu($entries, $alignment = null) + private function genDropdownMenu(array $entries, $alignment = null): string { $alignment = !is_null($alignment) ? $alignment : $this->options['alignment']; $html = $this->node('div', [ @@ -136,7 +137,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric return $html; } - private function genEntries($entries) + private function genEntries(array $entries): string { $html = ''; foreach ($entries as $entry) { @@ -150,7 +151,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric return $html; } - private function genEntry($entry) + private function genEntry(array $entry): string { if (!empty($entry['html'])) { return $entry['html']; diff --git a/src/View/Helper/BootstrapElements/BootstrapIcon.php b/src/View/Helper/BootstrapElements/BootstrapIcon.php index 085d9ae..71c0c8a 100644 --- a/src/View/Helper/BootstrapElements/BootstrapIcon.php +++ b/src/View/Helper/BootstrapElements/BootstrapIcon.php @@ -26,25 +26,25 @@ class BootstrapIcon extends BootstrapGeneric 'attrs' => [], ]; - function __construct($icon, $options = []) + function __construct(string $icon, array $options = []) { $this->icon = $icon; $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); } - public function icon() + public function icon(): string { return $this->genIcon(); } - private function genIcon() + private function genIcon(): string { $html = $this->node('span', array_merge( [ diff --git a/src/View/Helper/BootstrapElements/BootstrapListGroup.php b/src/View/Helper/BootstrapElements/BootstrapListGroup.php index 59ae690..5804a3f 100644 --- a/src/View/Helper/BootstrapElements/BootstrapListGroup.php +++ b/src/View/Helper/BootstrapElements/BootstrapListGroup.php @@ -60,14 +60,14 @@ class BootstrapListGroup extends BootstrapGeneric private static $defaultClasses = ['list-group',]; private static $defaultItemClasses = ['list-group-item', 'list-group-item-action', 'd-flex', 'align-items-start', 'justify-content-between']; - function __construct($items, $options, $btHelper) + function __construct(array $items, array $options, \App\View\BootstrapHelper $btHelper) { $this->items = $items; $this->processOptions($options); $this->btHelper = $btHelper; } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); @@ -90,7 +90,7 @@ class BootstrapListGroup extends BootstrapGeneric return $html; } - private function genItem($item) + private function genItem(array $item): string { $item['class'] = !is_array($item['class']) ? [$item['class']] : $item['class']; $itemOptions = array_merge($this->defaultItemOptions, $item); @@ -109,7 +109,7 @@ class BootstrapListGroup extends BootstrapGeneric return $html; } - private function genBadge($badge) + private function genBadge(array $badge): string { if (empty($badge)) { return ''; diff --git a/src/View/Helper/BootstrapElements/BootstrapListTable.php b/src/View/Helper/BootstrapElements/BootstrapListTable.php index 77aaddb..a2ba806 100644 --- a/src/View/Helper/BootstrapElements/BootstrapListTable.php +++ b/src/View/Helper/BootstrapElements/BootstrapListTable.php @@ -5,6 +5,7 @@ namespace App\View\Helper\BootstrapElements; use Cake\Utility\Hash; use App\View\Helper\BootstrapGeneric; +use App\View\Helper\BootstrapHelper; /** * Creates a list looking like a table from 1-dimensional data $item. @@ -93,7 +94,7 @@ class BootstrapListTable extends BootstrapGeneric 'elementsRootPath' => '/genericElements/SingleViews/Fields/', ]; - function __construct($options, $data, $btHelper) + function __construct(array $options, array $data, BootstrapHelper $btHelper) { $this->allowedOptionValues = [ 'variant' => array_merge(BootstrapGeneric::$variants, ['']) @@ -105,7 +106,7 @@ class BootstrapListTable extends BootstrapGeneric $this->btHelper = $btHelper; } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->options['tableClass'] = $this->convertToArrayIfNeeded($this->options['tableClass']); @@ -113,12 +114,12 @@ class BootstrapListTable extends BootstrapGeneric $this->checkOptionValidity(); } - public function table() + public function table(): string { return $this->genTable(); } - private function genTable() + private function genTable(): string { $html = $this->nodeOpen('table', [ 'class' => [ @@ -142,7 +143,7 @@ class BootstrapListTable extends BootstrapGeneric return $html; } - private function genBody() + private function genBody(): string { $body = $this->nodeOpen('tbody', [ 'class' => $this->options['bodyClass'], @@ -154,7 +155,7 @@ class BootstrapListTable extends BootstrapGeneric return $body; } - private function genRow($field) + private function genRow(array $field): string { $rowValue = $this->genCell($field); $rowKey = $this->node('th', [ @@ -172,7 +173,7 @@ class BootstrapListTable extends BootstrapGeneric return $row; } - private function genCell($field = []) + private function genCell(array $field = []): string { if (isset($field['raw'])) { $cellContent = !empty($field['rawNoEscaping']) ? $field['raw'] : h($field['raw']); @@ -199,14 +200,14 @@ class BootstrapListTable extends BootstrapGeneric ], $cellContent); } - private function getValueFromObject($field) + private function getValueFromObject(array $field): string { $key = is_array($field) ? $field['path'] : $field; $cellValue = Hash::get($this->item, $key); - return $cellValue; + return !is_null($cellValue) ? $cellValue : ''; } - private function getElementPath($type) + private function getElementPath($type): string { return sprintf( '%s%sField', @@ -215,7 +216,7 @@ class BootstrapListTable extends BootstrapGeneric ); } - private function genCaption() + private function genCaption(): string { return !empty($this->caption) ? $this->node('caption', [], h($this->caption)) : ''; } diff --git a/src/View/Helper/BootstrapElements/BootstrapModal.php b/src/View/Helper/BootstrapElements/BootstrapModal.php index fb25b8e..a7b60fb 100644 --- a/src/View/Helper/BootstrapElements/BootstrapModal.php +++ b/src/View/Helper/BootstrapElements/BootstrapModal.php @@ -155,7 +155,7 @@ class BootstrapModal extends BootstrapGeneric 'cancelOnclick' => '' ]; - function __construct($options) + function __construct(array $options) { $this->allowedOptionValues = [ 'size' => ['sm', 'lg', 'xl', ''], @@ -167,7 +167,7 @@ class BootstrapModal extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); @@ -188,7 +188,7 @@ class BootstrapModal extends BootstrapGeneric } } - public function modal() + public function modal(): string { $modal = $this->genModal(); if ($this->options['show']) { @@ -211,7 +211,7 @@ class BootstrapModal extends BootstrapGeneric )); } - private function genModal() + private function genModal(): string { $dialog = $this->nodeOpen('div', [ 'class' => array_merge( @@ -231,7 +231,7 @@ class BootstrapModal extends BootstrapGeneric return $html; } - private function genHeader() + private function genHeader(): string { $header = $this->nodeOpen('div', ['class' => array_merge(['modal-header'], $this->options['headerClass'])]); $header .= $this->options['titleHtml'] ?? $this->node('h5', ['class' => ['modal-title']], h($this->options['title'])); @@ -242,7 +242,7 @@ class BootstrapModal extends BootstrapGeneric return $header; } - private function genBody() + private function genBody(): string { $body = $this->nodeOpen('div', ['class' => array_merge(['modal-body'], $this->options['bodyClass'])]); $body .= $this->options['bodyHtml'] ?? h($this->options['body']); @@ -250,7 +250,7 @@ class BootstrapModal extends BootstrapGeneric return $body; } - private function genFooter() + private function genFooter(): string { $footer = $this->nodeOpen('div', [ 'class' => array_merge(['modal-footer'], $this->options['footerClass']), @@ -261,7 +261,7 @@ class BootstrapModal extends BootstrapGeneric return $footer; } - private function getFooterBasedOnType() + private function getFooterBasedOnType(): string { if ($this->options['type'] == 'ok-only') { return $this->getFooterOkOnly(); @@ -274,7 +274,7 @@ class BootstrapModal extends BootstrapGeneric } } - private function getFooterOkOnly() + private function getFooterOkOnly(): string { return (new BootstrapButton([ 'variant' => 'primary', @@ -286,7 +286,7 @@ class BootstrapModal extends BootstrapGeneric ]))->button(); } - private function getFooterConfirm() + private function getFooterConfirm(): string { $buttonCancelConfig = array_merge( [ @@ -318,7 +318,7 @@ class BootstrapModal extends BootstrapGeneric return $buttonCancel . $buttonConfirm; } - private function getFooterCustom() + private function getFooterCustom(): string { $buttons = []; foreach ($this->options['footerButtons'] as $buttonConfig) { diff --git a/src/View/Helper/BootstrapElements/BootstrapNotificationBubble.php b/src/View/Helper/BootstrapElements/BootstrapNotificationBubble.php index d9744d2..e0788ab 100644 --- a/src/View/Helper/BootstrapElements/BootstrapNotificationBubble.php +++ b/src/View/Helper/BootstrapElements/BootstrapNotificationBubble.php @@ -33,7 +33,7 @@ class BootstrapNotificationBubble extends BootstrapGeneric 'attrs' => [], ]; - function __construct($options) + function __construct(array $options) { $this->allowedOptionValues = [ 'variant' => BootstrapGeneric::$variants, @@ -43,7 +43,7 @@ class BootstrapNotificationBubble extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); @@ -57,12 +57,12 @@ class BootstrapNotificationBubble extends BootstrapGeneric } } - public function notificationBubble() + public function notificationBubble(): string { return $this->genNotificationBubble(); } - private function genNotificationBubble() + private function genNotificationBubble(): string { $tmpId = 'tmp-' . mt_rand(); $defaultClasses = [ diff --git a/src/View/Helper/BootstrapElements/BootstrapProgress.php b/src/View/Helper/BootstrapElements/BootstrapProgress.php index 7bb861e..27117b5 100644 --- a/src/View/Helper/BootstrapElements/BootstrapProgress.php +++ b/src/View/Helper/BootstrapElements/BootstrapProgress.php @@ -47,18 +47,18 @@ class BootstrapProgress extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions($options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); } - public function progress() + public function progress(): string { return $this->genProgress(); } - private function genProgress() + private function genProgress(): string { $percentage = round(100 * $this->options['value'] / $this->options['total']); $heightStyle = !empty($this->options['height']) ? sprintf('height: %s;', h($this->options['height'])) : ''; diff --git a/src/View/Helper/BootstrapElements/BootstrapProgressTimeline.php b/src/View/Helper/BootstrapElements/BootstrapProgressTimeline.php index 6243a5e..89c20d0 100644 --- a/src/View/Helper/BootstrapElements/BootstrapProgressTimeline.php +++ b/src/View/Helper/BootstrapElements/BootstrapProgressTimeline.php @@ -51,18 +51,18 @@ class BootstrapProgressTimeline extends BootstrapGeneric $this->btHelper = $btHelper; } - private function processOptions($options) + private function processOptions($options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); } - public function progressTimeline() + public function progressTimeline(): string { return $this->genProgressTimeline(); } - private function getStepIcon($step, $i, $nodeActive, $lineActive) + private function getStepIcon(array $step, int $i, bool $nodeActive, bool $lineActive): string { $icon = $this->node('b', [ 'class' => [ @@ -92,7 +92,7 @@ class BootstrapProgressTimeline extends BootstrapGeneric return $html; } - private function getHorizontalLine($i, $nodeActive, $lineActive) + private function getHorizontalLine(int $i, bool $nodeActive, bool $lineActive): string { $stepCount = count($this->options['steps']); if ($i == $stepCount - 1) { @@ -114,7 +114,7 @@ class BootstrapProgressTimeline extends BootstrapGeneric return $line; } - private function getStepText($step, $isActive) + private function getStepText(array $step, bool $isActive): string { return $this->node('li', [ 'class' => [ @@ -125,7 +125,7 @@ class BootstrapProgressTimeline extends BootstrapGeneric ], h($step['text'] ?? '')); } - private function genProgressTimeline() + private function genProgressTimeline(): string { $iconLis = ''; $textLis = ''; diff --git a/src/View/Helper/BootstrapElements/BootstrapSwitch.php b/src/View/Helper/BootstrapElements/BootstrapSwitch.php index 984964c..b70ab6f 100644 --- a/src/View/Helper/BootstrapElements/BootstrapSwitch.php +++ b/src/View/Helper/BootstrapElements/BootstrapSwitch.php @@ -34,7 +34,7 @@ class BootstrapSwitch extends BootstrapGeneric 'attrs' => [], ]; - public function __construct($options) + public function __construct(array $options) { $this->allowedOptionValues = [ 'variant' => BootstrapGeneric::$variants, @@ -42,18 +42,18 @@ class BootstrapSwitch extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); } - public function switch() + public function switch(): string { return $this->genSwitch(); } - public function genSwitch() + public function genSwitch(): string { $tmpId = 'tmp-' . mt_rand(); $input = self::node('input', array_merge( diff --git a/src/View/Helper/BootstrapElements/BootstrapTable.php b/src/View/Helper/BootstrapElements/BootstrapTable.php index c69dc76..21a627f 100644 --- a/src/View/Helper/BootstrapElements/BootstrapTable.php +++ b/src/View/Helper/BootstrapElements/BootstrapTable.php @@ -6,6 +6,7 @@ use Cake\Utility\Hash; use Cake\Utility\Inflector; use App\View\Helper\BootstrapGeneric; +use App\View\Helper\BootstrapHelper; /** * Creates a table from 2-dimensional data $items. @@ -23,6 +24,7 @@ use App\View\Helper\BootstrapGeneric; * # Options for fields * - label: The name of the field to be displayed as a label * - labelHtml: The HTML of the field to be displayed as a label + * - class: Additional classes to add for that row * - path: The path to be fed to Hash::get() in order to get the value from the $item * - element: The type of element to use combined with $elementsRootPath from the table's option * - formatter: A callback function to format the value @@ -87,7 +89,7 @@ class BootstrapTable extends BootstrapGeneric 'elementsRootPath' => '/genericElements/SingleViews/Fields/', ]; - function __construct($options, $data, $btHelper) + function __construct(array $options, array $data, BootstrapHelper $btHelper) { $this->allowedOptionValues = [ 'variant' => array_merge(BootstrapGeneric::$variants, ['']) @@ -99,7 +101,7 @@ class BootstrapTable extends BootstrapGeneric $this->btHelper = $btHelper; } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->checkOptionValidity(); @@ -108,12 +110,12 @@ class BootstrapTable extends BootstrapGeneric $this->options['headerClass'] = $this->convertToArrayIfNeeded($this->options['headerClass']); } - public function table() + public function table(): string { return $this->genTable(); } - private function genTable() + private function genTable(): string { $html = $this->nodeOpen('table', [ 'class' => [ @@ -138,7 +140,7 @@ class BootstrapTable extends BootstrapGeneric return $html; } - private function genHeader() + private function genHeader(): string { $head = $this->nodeOpen('thead', [ 'class' => $this->options['headerClass'], @@ -163,7 +165,7 @@ class BootstrapTable extends BootstrapGeneric return $head; } - private function genBody() + private function genBody(): string { $body = $this->nodeOpen('tbody', [ 'class' => $this->options['bodyClass'], @@ -175,7 +177,7 @@ class BootstrapTable extends BootstrapGeneric return $body; } - private function genRow($row, $rowIndex) + private function genRow(array $row, int $rowIndex): string { $html = $this->nodeOpen('tr', [ 'class' => [ @@ -189,14 +191,14 @@ class BootstrapTable extends BootstrapGeneric } } else { // indexed array foreach ($row as $i => $cellValue) { - $html .= $this->genCell($cellValue, 'index', $row, $rowIndex); + $html .= $this->genCell($cellValue, [], $row, $rowIndex); } } $html .= $this->nodeClose('tr'); return $html; } - private function genCell($value, $field = [], $row = [], $rowIndex = 0) + private function genCell(string $value, array $field = [], array $row = [], int $rowIndex = 0): string { if (isset($field['formatter'])) { $cellContent = $field['formatter']($value, $row, $rowIndex); @@ -209,20 +211,23 @@ class BootstrapTable extends BootstrapGeneric $cellContent = h($value); } return $this->node('td', [ - 'class' => [ - !empty($field['columnVariant']) ? "table-{$field['columnVariant']}" : '' - ] + 'class' => array_merge( + [ + !empty($field['columnVariant']) ? "table-{$field['columnVariant']}" : '' + ], + $field['class'] ?? [] + ), ], $cellContent); } - private function getValueFromObject($row, $field) + private function getValueFromObject(array $row, $field): string { $path = is_array($field) ? $field['path'] : $field; $cellValue = Hash::get($row, $path); - return $cellValue; + return !is_null($cellValue) ? $cellValue : ''; } - private function getElementPath($type) + private function getElementPath(string $type): string { return sprintf( '%s%sField', @@ -231,7 +236,7 @@ class BootstrapTable extends BootstrapGeneric ); } - private function genCaption() + private function genCaption(): string { return !empty($this->caption) ? $this->node('caption', [], h($this->caption)) : ''; } diff --git a/src/View/Helper/BootstrapElements/BootstrapTabs.php b/src/View/Helper/BootstrapElements/BootstrapTabs.php index a0a7e59..e0ac5af 100644 --- a/src/View/Helper/BootstrapElements/BootstrapTabs.php +++ b/src/View/Helper/BootstrapElements/BootstrapTabs.php @@ -92,7 +92,7 @@ class BootstrapTabs extends BootstrapGeneric ]; private $bsClasses = null; - function __construct($options) + function __construct(array $options) { $this->allowedOptionValues = [ 'justify-header' => [false, 'center', 'end', 'start'], @@ -104,12 +104,12 @@ class BootstrapTabs extends BootstrapGeneric $this->processOptions($options); } - public function tabs() + public function tabs(): string { return $this->genTabs(); } - private function processOptions($options) + private function processOptions(array $options): void { $this->options = array_merge($this->defaultOptions, $options); $this->data = $this->options['data']; @@ -179,12 +179,12 @@ class BootstrapTabs extends BootstrapGeneric } } - private function genTabs() + private function genTabs(): string { return $this->options['vertical'] ? $this->genVerticalTabs() : $this->genHorizontalTabs(); } - private function genHorizontalTabs() + private function genHorizontalTabs(): string { if ($this->options['card']) { $cardOptions = [ @@ -206,7 +206,7 @@ class BootstrapTabs extends BootstrapGeneric } } - private function genVerticalTabs() + private function genVerticalTabs(): string { $header = $this->node('div', ['class' => array_merge( [ @@ -244,7 +244,7 @@ class BootstrapTabs extends BootstrapGeneric return $container; } - private function genNav() + private function genNav(): string { $html = $this->nodeOpen('ul', [ 'class' => array_merge(['nav'], $this->bsClasses['nav'], $this->options['nav-class']), @@ -257,7 +257,7 @@ class BootstrapTabs extends BootstrapGeneric return $html; } - private function genNavItem($navItem) + private function genNavItem(array $navItem): string { $html = $this->nodeOpen('li', [ 'class' => array_merge(['nav-item'], $this->bsClasses['nav-item'], $this->options['nav-item-class']), @@ -282,7 +282,7 @@ class BootstrapTabs extends BootstrapGeneric return $html; } - private function genContent() + private function genContent(): string { $html = $this->nodeOpen('div', [ 'class' => array_merge(['tab-content'], $this->options['content-class']), @@ -295,7 +295,7 @@ class BootstrapTabs extends BootstrapGeneric return $html; } - private function genContentItem($navItem, $content) + private function genContentItem(array $navItem, string $content): string { return $this->node('div', [ 'class' => array_merge(['tab-pane', 'fade'], [!empty($navItem['active']) ? 'show active' : '']), diff --git a/src/View/Helper/BootstrapElements/BootstrapToast.php b/src/View/Helper/BootstrapElements/BootstrapToast.php index fc3e264..9df3b8d 100644 --- a/src/View/Helper/BootstrapElements/BootstrapToast.php +++ b/src/View/Helper/BootstrapElements/BootstrapToast.php @@ -40,7 +40,7 @@ class BootstrapToast extends BootstrapGeneric 'closeButton' => true, ]; - function __construct($options) + function __construct(array $options) { $this->allowedOptionValues = [ 'variant' => array_merge(BootstrapGeneric::$variants, ['default']), @@ -48,7 +48,7 @@ class BootstrapToast extends BootstrapGeneric $this->processOptions($options); } - private function processOptions($options) + private function processOptions(array $options): void { $validOptions = array_filter($options, function($optionName) { return isset($this->defaultOptions[$optionName]); @@ -57,12 +57,12 @@ class BootstrapToast extends BootstrapGeneric $this->checkOptionValidity(); } - public function toast() + public function toast(): string { return $this->genToast(); } - private function genToast() + private function genToast(): string { return $this->node('script', [], sprintf( "$(document).ready(function() { diff --git a/src/View/Helper/BootstrapHelper.php b/src/View/Helper/BootstrapHelper.php index 6fc78d3..f814116 100644 --- a/src/View/Helper/BootstrapHelper.php +++ b/src/View/Helper/BootstrapHelper.php @@ -191,10 +191,10 @@ class BootstrapHelper extends Helper * Creates a Bootstrap tabs from the given options * * @param array $options See BootstrapElements\BootstrapTabs - * @param string $content See BootstrapElements\BootstrapTabs + * @param array $content See BootstrapElements\BootstrapTabs * @return string */ - public function accordion(array $options, string $content): string + public function accordion(array $options, array $content): string { $bsAccordion = new BootstrapAccordion($options, $content, $this); return $bsAccordion->accordion();