chg: [helper:bootstrap] Added more documentation and typing

develop-unstable
Sami Mokaddem 2022-11-28 10:01:18 +01:00
parent b7a446cd56
commit abd9e04a0f
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
19 changed files with 131 additions and 122 deletions

View File

@ -3,6 +3,7 @@
namespace App\View\Helper\BootstrapElements; namespace App\View\Helper\BootstrapElements;
use App\View\Helper\BootstrapGeneric; use App\View\Helper\BootstrapGeneric;
use App\View\Helper\BootstrapHelper;
/** /**
* Creates an collapsible accordion component * Creates an collapsible accordion component
@ -53,7 +54,7 @@ class BootstrapAccordion extends BootstrapGeneric
'class' => [], 'class' => [],
]; ];
function __construct($options, $content, $btHelper) function __construct(array $options, array $content, BootstrapHelper $btHelper)
{ {
$this->allowedOptionValues = []; $this->allowedOptionValues = [];
$this->content = $content; $this->content = $content;
@ -61,7 +62,7 @@ class BootstrapAccordion extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
@ -78,12 +79,12 @@ class BootstrapAccordion extends BootstrapGeneric
} }
} }
public function accordion() public function accordion(): string
{ {
return $this->genAccordion(); return $this->genAccordion();
} }
private function genHeader($accordionItem, $i) private function genHeader(array $accordionItem, int $i): string
{ {
$html = $this->nodeOpen('h2', [ $html = $this->nodeOpen('h2', [
'class' => ['accordion-header'], 'class' => ['accordion-header'],
@ -110,7 +111,7 @@ class BootstrapAccordion extends BootstrapGeneric
return $html; return $html;
} }
private function genBody($accordionItem, $i) private function genBody(array $accordionItem, int $i): string
{ {
$content = $this->node('div', [ $content = $this->node('div', [
'class' => ['accordion-body'] 'class' => ['accordion-body']
@ -134,7 +135,7 @@ class BootstrapAccordion extends BootstrapGeneric
return $html; return $html;
} }
private function genAccordion() private function genAccordion(): string
{ {
$html = $this->nodeOpen('div', [ $html = $this->nodeOpen('div', [
'class' => array_merge(['accordion'], $this->options['class']), 'class' => array_merge(['accordion'], $this->options['class']),

View File

@ -34,7 +34,7 @@ class BootstrapAlert extends BootstrapGeneric
'class' => [], 'class' => [],
]; ];
function __construct($options) function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => BootstrapGeneric::$variants, 'variant' => BootstrapGeneric::$variants,
@ -42,19 +42,19 @@ class BootstrapAlert extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']);
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function alert() public function alert(): string
{ {
return $this->genAlert(); return $this->genAlert();
} }
private function genAlert() private function genAlert(): string
{ {
$html = $this->nodeOpen('div', [ $html = $this->nodeOpen('div', [
'class' => array_merge([ 'class' => array_merge([
@ -72,7 +72,7 @@ class BootstrapAlert extends BootstrapGeneric
return $html; return $html;
} }
private function genCloseButton() private function genCloseButton(): string
{ {
$html = ''; $html = '';
if ($this->options['dismissible']) { if ($this->options['dismissible']) {

View File

@ -34,7 +34,7 @@ class BootstrapBadge extends BootstrapGeneric
'class' => [], 'class' => [],
]; ];
function __construct($options) function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => BootstrapGeneric::$variants, 'variant' => BootstrapGeneric::$variants,
@ -42,19 +42,19 @@ class BootstrapBadge extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']);
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function badge() public function badge(): string
{ {
return $this->genBadge(); return $this->genBadge();
} }
private function genBadge() private function genBadge(): string
{ {
$html = $this->node('span', [ $html = $this->node('span', [
'class' => array_merge($this->options['class'], [ 'class' => array_merge($this->options['class'], [

View File

@ -53,7 +53,7 @@ class BootstrapButton extends BootstrapGeneric
private $bsClasses = []; private $bsClasses = [];
function __construct($options) function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => array_merge(BootstrapGeneric::$variants, ['link', 'text']), 'variant' => array_merge(BootstrapGeneric::$variants, ['link', 'text']),
@ -63,7 +63,7 @@ class BootstrapButton extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $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(); return $this->genButton();
} }
private function genButton() private function genButton(): string
{ {
$html = $this->nodeOpen($this->options['nodeType'], array_merge($this->options['attrs'], [ $html = $this->nodeOpen($this->options['nodeType'], array_merge($this->options['attrs'], [
'class' => array_merge($this->options['class'], $this->bsClasses), 'class' => array_merge($this->options['class'], $this->bsClasses),
@ -116,7 +116,7 @@ class BootstrapButton extends BootstrapGeneric
return $html; return $html;
} }
private function genIcon() private function genIcon(): string
{ {
if (!empty($this->options['icon'])) { if (!empty($this->options['icon'])) {
$bsIcon = new BootstrapIcon($this->options['icon'], [ $bsIcon = new BootstrapIcon($this->options['icon'], [
@ -127,7 +127,7 @@ class BootstrapButton extends BootstrapGeneric
return ''; return '';
} }
private function genImage() private function genImage(): string
{ {
if (!empty($this->options['image'])) { if (!empty($this->options['image'])) {
return $this->node('img', [ return $this->node('img', [

View File

@ -41,7 +41,7 @@ class BootstrapCard extends BootstrapGeneric
'footerClass' => '', 'footerClass' => '',
]; ];
public function __construct($options) public function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'headerVariant' => array_merge(BootstrapGeneric::$variants, ['']), 'headerVariant' => array_merge(BootstrapGeneric::$variants, ['']),
@ -51,7 +51,7 @@ class BootstrapCard extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['headerClass'] = $this->convertToArrayIfNeeded($this->options['headerClass']); $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']}" : ''; $this->options['borderVariant'] = !empty($this->options['headerVariant']) ? "border-{$this->options['headerVariant']}" : '';
} }
public function card() public function card(): string
{ {
return $this->genCard(); return $this->genCard();
} }
private function genCard() private function genCard(): string
{ {
$card = $this->node('div', [ $card = $this->node('div', [
'class' => array_merge( 'class' => array_merge(
@ -80,7 +80,7 @@ class BootstrapCard extends BootstrapGeneric
return $card; return $card;
} }
private function genHeader() private function genHeader(): string
{ {
if (empty($this->options['headerHTML']) && empty($this->options['headerText'])) { if (empty($this->options['headerHTML']) && empty($this->options['headerText'])) {
return ''; return '';
@ -98,7 +98,7 @@ class BootstrapCard extends BootstrapGeneric
return $header; return $header;
} }
private function genBody() private function genBody(): string
{ {
if (empty($this->options['bodyHTML']) && empty($this->options['bodyText'])) { if (empty($this->options['bodyHTML']) && empty($this->options['bodyText'])) {
return ''; return '';
@ -116,7 +116,7 @@ class BootstrapCard extends BootstrapGeneric
return $body; return $body;
} }
private function genFooter() private function genFooter(): string
{ {
if (empty($this->options['footerHTML']) && empty($this->options['footerText'])) { if (empty($this->options['footerHTML']) && empty($this->options['footerText'])) {
return ''; return '';

View File

@ -5,6 +5,7 @@ namespace App\View\Helper\BootstrapElements;
use Cake\Utility\Security; use Cake\Utility\Security;
use App\View\Helper\BootstrapGeneric; use App\View\Helper\BootstrapGeneric;
use App\View\Helper\BootstrapHelper;
/** /**
* Creates a Bootstrap collapsible component * Creates a Bootstrap collapsible component
@ -45,7 +46,7 @@ class BootstrapCollapse extends BootstrapGeneric
'attrs' => [], 'attrs' => [],
]; ];
function __construct($options, $content, $btHelper) function __construct(array $options, string $content, BootstrapHelper $btHelper)
{ {
$this->allowedOptionValues = []; $this->allowedOptionValues = [];
$this->processOptions($options); $this->processOptions($options);
@ -53,7 +54,7 @@ class BootstrapCollapse extends BootstrapGeneric
$this->btHelper = $btHelper; $this->btHelper = $btHelper;
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']);
@ -73,12 +74,12 @@ class BootstrapCollapse extends BootstrapGeneric
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function collapse() public function collapse(): string
{ {
return $this->genCollapse(); return $this->genCollapse();
} }
private function genControl() private function genControl(): string
{ {
$attrsConfig = [ $attrsConfig = [
'data-bs-toggle' => 'collapse', 'data-bs-toggle' => 'collapse',
@ -101,7 +102,7 @@ class BootstrapCollapse extends BootstrapGeneric
return $html; return $html;
} }
private function genContent() private function genContent(): string
{ {
$cardConfig = $this->options['card']; $cardConfig = $this->options['card'];
$cardConfig['bodyHTML'] = $this->content; $cardConfig['bodyHTML'] = $this->content;
@ -113,7 +114,7 @@ class BootstrapCollapse extends BootstrapGeneric
return $container; return $container;
} }
private function genCollapse() private function genCollapse(): string
{ {
return $this->genControl() . $this->genContent(); return $this->genControl() . $this->genContent();
} }

View File

@ -3,6 +3,7 @@
namespace App\View\Helper\BootstrapElements; namespace App\View\Helper\BootstrapElements;
use App\View\Helper\BootstrapGeneric; use App\View\Helper\BootstrapGeneric;
use App\View\Helper\BootstrapHelper;
/** /**
* # Options * # Options
@ -65,7 +66,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric
'attrs' => [], 'attrs' => [],
]; ];
function __construct($options, $btHelper) function __construct(array $options, BootstrapHelper $btHelper)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'direction' => ['start', 'end', 'up', 'down'], 'direction' => ['start', 'end', 'up', 'down'],
@ -77,24 +78,24 @@ class BootstrapDropdownMenu extends BootstrapGeneric
$this->btHelper = $btHelper; $this->btHelper = $btHelper;
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['dropdown-class'] = $this->convertToArrayIfNeeded($this->options['dropdown-class']); $this->options['dropdown-class'] = $this->convertToArrayIfNeeded($this->options['dropdown-class']);
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function dropdownMenu() public function dropdownMenu(): string
{ {
return $this->fullDropdown(); return $this->fullDropdown();
} }
public function fullDropdown() public function fullDropdown(): string
{ {
return $this->genDropdownWrapper($this->genDropdownToggleButton(), $this->genDropdownMenu($this->menu)); 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']; $classes = !is_null($classes) ? $classes : $this->options['dropdown-class'];
$direction = !is_null($direction) ? $direction : $this->options['direction']; $direction = !is_null($direction) ? $direction : $this->options['direction'];
@ -114,7 +115,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric
return $html; return $html;
} }
public function genDropdownToggleButton() public function genDropdownToggleButton(): string
{ {
$defaultOptions = [ $defaultOptions = [
'class' => ['dropdown-toggle'], 'class' => ['dropdown-toggle'],
@ -127,7 +128,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric
return $this->btHelper->button($options); 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']; $alignment = !is_null($alignment) ? $alignment : $this->options['alignment'];
$html = $this->node('div', [ $html = $this->node('div', [
@ -136,7 +137,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric
return $html; return $html;
} }
private function genEntries($entries) private function genEntries(array $entries): string
{ {
$html = ''; $html = '';
foreach ($entries as $entry) { foreach ($entries as $entry) {
@ -150,7 +151,7 @@ class BootstrapDropdownMenu extends BootstrapGeneric
return $html; return $html;
} }
private function genEntry($entry) private function genEntry(array $entry): string
{ {
if (!empty($entry['html'])) { if (!empty($entry['html'])) {
return $entry['html']; return $entry['html'];

View File

@ -26,25 +26,25 @@ class BootstrapIcon extends BootstrapGeneric
'attrs' => [], 'attrs' => [],
]; ];
function __construct($icon, $options = []) function __construct(string $icon, array $options = [])
{ {
$this->icon = $icon; $this->icon = $icon;
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
$this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']);
} }
public function icon() public function icon(): string
{ {
return $this->genIcon(); return $this->genIcon();
} }
private function genIcon() private function genIcon(): string
{ {
$html = $this->node('span', array_merge( $html = $this->node('span', array_merge(
[ [

View File

@ -60,14 +60,14 @@ class BootstrapListGroup extends BootstrapGeneric
private static $defaultClasses = ['list-group',]; private static $defaultClasses = ['list-group',];
private static $defaultItemClasses = ['list-group-item', 'list-group-item-action', 'd-flex', 'align-items-start', 'justify-content-between']; 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->items = $items;
$this->processOptions($options); $this->processOptions($options);
$this->btHelper = $btHelper; $this->btHelper = $btHelper;
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']); $this->options['class'] = $this->convertToArrayIfNeeded($this->options['class']);
@ -90,7 +90,7 @@ class BootstrapListGroup extends BootstrapGeneric
return $html; return $html;
} }
private function genItem($item) private function genItem(array $item): string
{ {
$item['class'] = !is_array($item['class']) ? [$item['class']] : $item['class']; $item['class'] = !is_array($item['class']) ? [$item['class']] : $item['class'];
$itemOptions = array_merge($this->defaultItemOptions, $item); $itemOptions = array_merge($this->defaultItemOptions, $item);
@ -109,7 +109,7 @@ class BootstrapListGroup extends BootstrapGeneric
return $html; return $html;
} }
private function genBadge($badge) private function genBadge(array $badge): string
{ {
if (empty($badge)) { if (empty($badge)) {
return ''; return '';

View File

@ -5,6 +5,7 @@ namespace App\View\Helper\BootstrapElements;
use Cake\Utility\Hash; use Cake\Utility\Hash;
use App\View\Helper\BootstrapGeneric; use App\View\Helper\BootstrapGeneric;
use App\View\Helper\BootstrapHelper;
/** /**
* Creates a list looking like a table from 1-dimensional data $item. * Creates a list looking like a table from 1-dimensional data $item.
@ -93,7 +94,7 @@ class BootstrapListTable extends BootstrapGeneric
'elementsRootPath' => '/genericElements/SingleViews/Fields/', 'elementsRootPath' => '/genericElements/SingleViews/Fields/',
]; ];
function __construct($options, $data, $btHelper) function __construct(array $options, array $data, BootstrapHelper $btHelper)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => array_merge(BootstrapGeneric::$variants, ['']) 'variant' => array_merge(BootstrapGeneric::$variants, [''])
@ -105,7 +106,7 @@ class BootstrapListTable extends BootstrapGeneric
$this->btHelper = $btHelper; $this->btHelper = $btHelper;
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->options['tableClass'] = $this->convertToArrayIfNeeded($this->options['tableClass']); $this->options['tableClass'] = $this->convertToArrayIfNeeded($this->options['tableClass']);
@ -113,12 +114,12 @@ class BootstrapListTable extends BootstrapGeneric
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function table() public function table(): string
{ {
return $this->genTable(); return $this->genTable();
} }
private function genTable() private function genTable(): string
{ {
$html = $this->nodeOpen('table', [ $html = $this->nodeOpen('table', [
'class' => [ 'class' => [
@ -142,7 +143,7 @@ class BootstrapListTable extends BootstrapGeneric
return $html; return $html;
} }
private function genBody() private function genBody(): string
{ {
$body = $this->nodeOpen('tbody', [ $body = $this->nodeOpen('tbody', [
'class' => $this->options['bodyClass'], 'class' => $this->options['bodyClass'],
@ -154,7 +155,7 @@ class BootstrapListTable extends BootstrapGeneric
return $body; return $body;
} }
private function genRow($field) private function genRow(array $field): string
{ {
$rowValue = $this->genCell($field); $rowValue = $this->genCell($field);
$rowKey = $this->node('th', [ $rowKey = $this->node('th', [
@ -172,7 +173,7 @@ class BootstrapListTable extends BootstrapGeneric
return $row; return $row;
} }
private function genCell($field = []) private function genCell(array $field = []): string
{ {
if (isset($field['raw'])) { if (isset($field['raw'])) {
$cellContent = !empty($field['rawNoEscaping']) ? $field['raw'] : h($field['raw']); $cellContent = !empty($field['rawNoEscaping']) ? $field['raw'] : h($field['raw']);
@ -199,14 +200,14 @@ class BootstrapListTable extends BootstrapGeneric
], $cellContent); ], $cellContent);
} }
private function getValueFromObject($field) private function getValueFromObject(array $field): string
{ {
$key = is_array($field) ? $field['path'] : $field; $key = is_array($field) ? $field['path'] : $field;
$cellValue = Hash::get($this->item, $key); $cellValue = Hash::get($this->item, $key);
return $cellValue; return !is_null($cellValue) ? $cellValue : '';
} }
private function getElementPath($type) private function getElementPath($type): string
{ {
return sprintf( return sprintf(
'%s%sField', '%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)) : ''; return !empty($this->caption) ? $this->node('caption', [], h($this->caption)) : '';
} }

View File

@ -155,7 +155,7 @@ class BootstrapModal extends BootstrapGeneric
'cancelOnclick' => '' 'cancelOnclick' => ''
]; ];
function __construct($options) function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'size' => ['sm', 'lg', 'xl', ''], 'size' => ['sm', 'lg', 'xl', ''],
@ -167,7 +167,7 @@ class BootstrapModal extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
@ -188,7 +188,7 @@ class BootstrapModal extends BootstrapGeneric
} }
} }
public function modal() public function modal(): string
{ {
$modal = $this->genModal(); $modal = $this->genModal();
if ($this->options['show']) { if ($this->options['show']) {
@ -211,7 +211,7 @@ class BootstrapModal extends BootstrapGeneric
)); ));
} }
private function genModal() private function genModal(): string
{ {
$dialog = $this->nodeOpen('div', [ $dialog = $this->nodeOpen('div', [
'class' => array_merge( 'class' => array_merge(
@ -231,7 +231,7 @@ class BootstrapModal extends BootstrapGeneric
return $html; return $html;
} }
private function genHeader() private function genHeader(): string
{ {
$header = $this->nodeOpen('div', ['class' => array_merge(['modal-header'], $this->options['headerClass'])]); $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'])); $header .= $this->options['titleHtml'] ?? $this->node('h5', ['class' => ['modal-title']], h($this->options['title']));
@ -242,7 +242,7 @@ class BootstrapModal extends BootstrapGeneric
return $header; return $header;
} }
private function genBody() private function genBody(): string
{ {
$body = $this->nodeOpen('div', ['class' => array_merge(['modal-body'], $this->options['bodyClass'])]); $body = $this->nodeOpen('div', ['class' => array_merge(['modal-body'], $this->options['bodyClass'])]);
$body .= $this->options['bodyHtml'] ?? h($this->options['body']); $body .= $this->options['bodyHtml'] ?? h($this->options['body']);
@ -250,7 +250,7 @@ class BootstrapModal extends BootstrapGeneric
return $body; return $body;
} }
private function genFooter() private function genFooter(): string
{ {
$footer = $this->nodeOpen('div', [ $footer = $this->nodeOpen('div', [
'class' => array_merge(['modal-footer'], $this->options['footerClass']), 'class' => array_merge(['modal-footer'], $this->options['footerClass']),
@ -261,7 +261,7 @@ class BootstrapModal extends BootstrapGeneric
return $footer; return $footer;
} }
private function getFooterBasedOnType() private function getFooterBasedOnType(): string
{ {
if ($this->options['type'] == 'ok-only') { if ($this->options['type'] == 'ok-only') {
return $this->getFooterOkOnly(); return $this->getFooterOkOnly();
@ -274,7 +274,7 @@ class BootstrapModal extends BootstrapGeneric
} }
} }
private function getFooterOkOnly() private function getFooterOkOnly(): string
{ {
return (new BootstrapButton([ return (new BootstrapButton([
'variant' => 'primary', 'variant' => 'primary',
@ -286,7 +286,7 @@ class BootstrapModal extends BootstrapGeneric
]))->button(); ]))->button();
} }
private function getFooterConfirm() private function getFooterConfirm(): string
{ {
$buttonCancelConfig = array_merge( $buttonCancelConfig = array_merge(
[ [
@ -318,7 +318,7 @@ class BootstrapModal extends BootstrapGeneric
return $buttonCancel . $buttonConfirm; return $buttonCancel . $buttonConfirm;
} }
private function getFooterCustom() private function getFooterCustom(): string
{ {
$buttons = []; $buttons = [];
foreach ($this->options['footerButtons'] as $buttonConfig) { foreach ($this->options['footerButtons'] as $buttonConfig) {

View File

@ -33,7 +33,7 @@ class BootstrapNotificationBubble extends BootstrapGeneric
'attrs' => [], 'attrs' => [],
]; ];
function __construct($options) function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => BootstrapGeneric::$variants, 'variant' => BootstrapGeneric::$variants,
@ -43,7 +43,7 @@ class BootstrapNotificationBubble extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
@ -57,12 +57,12 @@ class BootstrapNotificationBubble extends BootstrapGeneric
} }
} }
public function notificationBubble() public function notificationBubble(): string
{ {
return $this->genNotificationBubble(); return $this->genNotificationBubble();
} }
private function genNotificationBubble() private function genNotificationBubble(): string
{ {
$tmpId = 'tmp-' . mt_rand(); $tmpId = 'tmp-' . mt_rand();
$defaultClasses = [ $defaultClasses = [

View File

@ -47,18 +47,18 @@ class BootstrapProgress extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions($options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function progress() public function progress(): string
{ {
return $this->genProgress(); return $this->genProgress();
} }
private function genProgress() private function genProgress(): string
{ {
$percentage = round(100 * $this->options['value'] / $this->options['total']); $percentage = round(100 * $this->options['value'] / $this->options['total']);
$heightStyle = !empty($this->options['height']) ? sprintf('height: %s;', h($this->options['height'])) : ''; $heightStyle = !empty($this->options['height']) ? sprintf('height: %s;', h($this->options['height'])) : '';

View File

@ -51,18 +51,18 @@ class BootstrapProgressTimeline extends BootstrapGeneric
$this->btHelper = $btHelper; $this->btHelper = $btHelper;
} }
private function processOptions($options) private function processOptions($options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function progressTimeline() public function progressTimeline(): string
{ {
return $this->genProgressTimeline(); 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', [ $icon = $this->node('b', [
'class' => [ 'class' => [
@ -92,7 +92,7 @@ class BootstrapProgressTimeline extends BootstrapGeneric
return $html; return $html;
} }
private function getHorizontalLine($i, $nodeActive, $lineActive) private function getHorizontalLine(int $i, bool $nodeActive, bool $lineActive): string
{ {
$stepCount = count($this->options['steps']); $stepCount = count($this->options['steps']);
if ($i == $stepCount - 1) { if ($i == $stepCount - 1) {
@ -114,7 +114,7 @@ class BootstrapProgressTimeline extends BootstrapGeneric
return $line; return $line;
} }
private function getStepText($step, $isActive) private function getStepText(array $step, bool $isActive): string
{ {
return $this->node('li', [ return $this->node('li', [
'class' => [ 'class' => [
@ -125,7 +125,7 @@ class BootstrapProgressTimeline extends BootstrapGeneric
], h($step['text'] ?? '')); ], h($step['text'] ?? ''));
} }
private function genProgressTimeline() private function genProgressTimeline(): string
{ {
$iconLis = ''; $iconLis = '';
$textLis = ''; $textLis = '';

View File

@ -34,7 +34,7 @@ class BootstrapSwitch extends BootstrapGeneric
'attrs' => [], 'attrs' => [],
]; ];
public function __construct($options) public function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => BootstrapGeneric::$variants, 'variant' => BootstrapGeneric::$variants,
@ -42,18 +42,18 @@ class BootstrapSwitch extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function switch() public function switch(): string
{ {
return $this->genSwitch(); return $this->genSwitch();
} }
public function genSwitch() public function genSwitch(): string
{ {
$tmpId = 'tmp-' . mt_rand(); $tmpId = 'tmp-' . mt_rand();
$input = self::node('input', array_merge( $input = self::node('input', array_merge(

View File

@ -6,6 +6,7 @@ use Cake\Utility\Hash;
use Cake\Utility\Inflector; use Cake\Utility\Inflector;
use App\View\Helper\BootstrapGeneric; use App\View\Helper\BootstrapGeneric;
use App\View\Helper\BootstrapHelper;
/** /**
* Creates a table from 2-dimensional data $items. * Creates a table from 2-dimensional data $items.
@ -23,6 +24,7 @@ use App\View\Helper\BootstrapGeneric;
* # Options for fields * # Options for fields
* - label: The name of the field to be displayed as a label * - label: The name of the field to be displayed as a label
* - labelHtml: The HTML 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 * - 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 * - element: The type of element to use combined with $elementsRootPath from the table's option
* - formatter: A callback function to format the value * - formatter: A callback function to format the value
@ -87,7 +89,7 @@ class BootstrapTable extends BootstrapGeneric
'elementsRootPath' => '/genericElements/SingleViews/Fields/', 'elementsRootPath' => '/genericElements/SingleViews/Fields/',
]; ];
function __construct($options, $data, $btHelper) function __construct(array $options, array $data, BootstrapHelper $btHelper)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => array_merge(BootstrapGeneric::$variants, ['']) 'variant' => array_merge(BootstrapGeneric::$variants, [''])
@ -99,7 +101,7 @@ class BootstrapTable extends BootstrapGeneric
$this->btHelper = $btHelper; $this->btHelper = $btHelper;
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->checkOptionValidity(); $this->checkOptionValidity();
@ -108,12 +110,12 @@ class BootstrapTable extends BootstrapGeneric
$this->options['headerClass'] = $this->convertToArrayIfNeeded($this->options['headerClass']); $this->options['headerClass'] = $this->convertToArrayIfNeeded($this->options['headerClass']);
} }
public function table() public function table(): string
{ {
return $this->genTable(); return $this->genTable();
} }
private function genTable() private function genTable(): string
{ {
$html = $this->nodeOpen('table', [ $html = $this->nodeOpen('table', [
'class' => [ 'class' => [
@ -138,7 +140,7 @@ class BootstrapTable extends BootstrapGeneric
return $html; return $html;
} }
private function genHeader() private function genHeader(): string
{ {
$head = $this->nodeOpen('thead', [ $head = $this->nodeOpen('thead', [
'class' => $this->options['headerClass'], 'class' => $this->options['headerClass'],
@ -163,7 +165,7 @@ class BootstrapTable extends BootstrapGeneric
return $head; return $head;
} }
private function genBody() private function genBody(): string
{ {
$body = $this->nodeOpen('tbody', [ $body = $this->nodeOpen('tbody', [
'class' => $this->options['bodyClass'], 'class' => $this->options['bodyClass'],
@ -175,7 +177,7 @@ class BootstrapTable extends BootstrapGeneric
return $body; return $body;
} }
private function genRow($row, $rowIndex) private function genRow(array $row, int $rowIndex): string
{ {
$html = $this->nodeOpen('tr', [ $html = $this->nodeOpen('tr', [
'class' => [ 'class' => [
@ -189,14 +191,14 @@ class BootstrapTable extends BootstrapGeneric
} }
} else { // indexed array } else { // indexed array
foreach ($row as $i => $cellValue) { foreach ($row as $i => $cellValue) {
$html .= $this->genCell($cellValue, 'index', $row, $rowIndex); $html .= $this->genCell($cellValue, [], $row, $rowIndex);
} }
} }
$html .= $this->nodeClose('tr'); $html .= $this->nodeClose('tr');
return $html; 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'])) { if (isset($field['formatter'])) {
$cellContent = $field['formatter']($value, $row, $rowIndex); $cellContent = $field['formatter']($value, $row, $rowIndex);
@ -209,20 +211,23 @@ class BootstrapTable extends BootstrapGeneric
$cellContent = h($value); $cellContent = h($value);
} }
return $this->node('td', [ return $this->node('td', [
'class' => [ 'class' => array_merge(
!empty($field['columnVariant']) ? "table-{$field['columnVariant']}" : '' [
] !empty($field['columnVariant']) ? "table-{$field['columnVariant']}" : ''
],
$field['class'] ?? []
),
], $cellContent); ], $cellContent);
} }
private function getValueFromObject($row, $field) private function getValueFromObject(array $row, $field): string
{ {
$path = is_array($field) ? $field['path'] : $field; $path = is_array($field) ? $field['path'] : $field;
$cellValue = Hash::get($row, $path); $cellValue = Hash::get($row, $path);
return $cellValue; return !is_null($cellValue) ? $cellValue : '';
} }
private function getElementPath($type) private function getElementPath(string $type): string
{ {
return sprintf( return sprintf(
'%s%sField', '%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)) : ''; return !empty($this->caption) ? $this->node('caption', [], h($this->caption)) : '';
} }

View File

@ -92,7 +92,7 @@ class BootstrapTabs extends BootstrapGeneric
]; ];
private $bsClasses = null; private $bsClasses = null;
function __construct($options) function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'justify-header' => [false, 'center', 'end', 'start'], 'justify-header' => [false, 'center', 'end', 'start'],
@ -104,12 +104,12 @@ class BootstrapTabs extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
public function tabs() public function tabs(): string
{ {
return $this->genTabs(); return $this->genTabs();
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$this->options = array_merge($this->defaultOptions, $options); $this->options = array_merge($this->defaultOptions, $options);
$this->data = $this->options['data']; $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(); return $this->options['vertical'] ? $this->genVerticalTabs() : $this->genHorizontalTabs();
} }
private function genHorizontalTabs() private function genHorizontalTabs(): string
{ {
if ($this->options['card']) { if ($this->options['card']) {
$cardOptions = [ $cardOptions = [
@ -206,7 +206,7 @@ class BootstrapTabs extends BootstrapGeneric
} }
} }
private function genVerticalTabs() private function genVerticalTabs(): string
{ {
$header = $this->node('div', ['class' => array_merge( $header = $this->node('div', ['class' => array_merge(
[ [
@ -244,7 +244,7 @@ class BootstrapTabs extends BootstrapGeneric
return $container; return $container;
} }
private function genNav() private function genNav(): string
{ {
$html = $this->nodeOpen('ul', [ $html = $this->nodeOpen('ul', [
'class' => array_merge(['nav'], $this->bsClasses['nav'], $this->options['nav-class']), 'class' => array_merge(['nav'], $this->bsClasses['nav'], $this->options['nav-class']),
@ -257,7 +257,7 @@ class BootstrapTabs extends BootstrapGeneric
return $html; return $html;
} }
private function genNavItem($navItem) private function genNavItem(array $navItem): string
{ {
$html = $this->nodeOpen('li', [ $html = $this->nodeOpen('li', [
'class' => array_merge(['nav-item'], $this->bsClasses['nav-item'], $this->options['nav-item-class']), 'class' => array_merge(['nav-item'], $this->bsClasses['nav-item'], $this->options['nav-item-class']),
@ -282,7 +282,7 @@ class BootstrapTabs extends BootstrapGeneric
return $html; return $html;
} }
private function genContent() private function genContent(): string
{ {
$html = $this->nodeOpen('div', [ $html = $this->nodeOpen('div', [
'class' => array_merge(['tab-content'], $this->options['content-class']), 'class' => array_merge(['tab-content'], $this->options['content-class']),
@ -295,7 +295,7 @@ class BootstrapTabs extends BootstrapGeneric
return $html; return $html;
} }
private function genContentItem($navItem, $content) private function genContentItem(array $navItem, string $content): string
{ {
return $this->node('div', [ return $this->node('div', [
'class' => array_merge(['tab-pane', 'fade'], [!empty($navItem['active']) ? 'show active' : '']), 'class' => array_merge(['tab-pane', 'fade'], [!empty($navItem['active']) ? 'show active' : '']),

View File

@ -40,7 +40,7 @@ class BootstrapToast extends BootstrapGeneric
'closeButton' => true, 'closeButton' => true,
]; ];
function __construct($options) function __construct(array $options)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => array_merge(BootstrapGeneric::$variants, ['default']), 'variant' => array_merge(BootstrapGeneric::$variants, ['default']),
@ -48,7 +48,7 @@ class BootstrapToast extends BootstrapGeneric
$this->processOptions($options); $this->processOptions($options);
} }
private function processOptions($options) private function processOptions(array $options): void
{ {
$validOptions = array_filter($options, function($optionName) { $validOptions = array_filter($options, function($optionName) {
return isset($this->defaultOptions[$optionName]); return isset($this->defaultOptions[$optionName]);
@ -57,12 +57,12 @@ class BootstrapToast extends BootstrapGeneric
$this->checkOptionValidity(); $this->checkOptionValidity();
} }
public function toast() public function toast(): string
{ {
return $this->genToast(); return $this->genToast();
} }
private function genToast() private function genToast(): string
{ {
return $this->node('script', [], sprintf( return $this->node('script', [], sprintf(
"$(document).ready(function() { "$(document).ready(function() {

View File

@ -191,10 +191,10 @@ class BootstrapHelper extends Helper
* Creates a Bootstrap tabs from the given options * Creates a Bootstrap tabs from the given options
* *
* @param array $options See BootstrapElements\BootstrapTabs * @param array $options See BootstrapElements\BootstrapTabs
* @param string $content See BootstrapElements\BootstrapTabs * @param array $content See BootstrapElements\BootstrapTabs
* @return string * @return string
*/ */
public function accordion(array $options, string $content): string public function accordion(array $options, array $content): string
{ {
$bsAccordion = new BootstrapAccordion($options, $content, $this); $bsAccordion = new BootstrapAccordion($options, $content, $this);
return $bsAccordion->accordion(); return $bsAccordion->accordion();