chg: [helper:BootstrapListTable] Refactored to remove warning + Linted

3.x-inbox
Sami Mokaddem 2024-01-09 10:20:48 +01:00
parent 234d92c80b
commit b02fa216d6
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 81 additions and 56 deletions

View File

@ -2,10 +2,9 @@
namespace App\View\Helper\BootstrapElements; namespace App\View\Helper\BootstrapElements;
use Cake\Utility\Hash;
use App\View\Helper\BootstrapGeneric; use App\View\Helper\BootstrapGeneric;
use App\View\Helper\BootstrapHelper; use App\View\Helper\BootstrapHelper;
use Cake\Utility\Hash;
/** /**
* Creates a list looking like a table from 1-dimensional data $item. * Creates a list looking like a table from 1-dimensional data $item.
@ -101,7 +100,12 @@ class BootstrapListTable extends BootstrapGeneric
'elementsRootPath' => '/genericElements/SingleViews/Fields/', 'elementsRootPath' => '/genericElements/SingleViews/Fields/',
]; ];
function __construct(array $options, array $data, BootstrapHelper $btHelper) private $fields;
private $item;
private $caption;
private $btHelper;
public function __construct(array $options, array $data, BootstrapHelper $btHelper)
{ {
$this->allowedOptionValues = [ $this->allowedOptionValues = [
'variant' => array_merge(BootstrapGeneric::$variants, ['']) 'variant' => array_merge(BootstrapGeneric::$variants, [''])
@ -129,7 +133,9 @@ class BootstrapListTable extends BootstrapGeneric
private function genTable(): string private function genTable(): string
{ {
$html = $this->nodeOpen('table', [ $html = $this->nodeOpen(
'table',
[
'class' => [ 'class' => [
'table', 'table',
"table-{$this->options['variant']}", "table-{$this->options['variant']}",
@ -142,7 +148,8 @@ class BootstrapListTable extends BootstrapGeneric
!empty($this->options['variant']) ? "table-{$this->options['variant']}" : '', !empty($this->options['variant']) ? "table-{$this->options['variant']}" : '',
], ],
'id' => $this->options['id'] ?? '' 'id' => $this->options['id'] ?? ''
]); ]
);
$html .= $this->genCaption(); $html .= $this->genCaption();
$html .= $this->genBody(); $html .= $this->genBody();
@ -153,9 +160,12 @@ class BootstrapListTable extends BootstrapGeneric
private function genBody(): string private function genBody(): string
{ {
$body = $this->nodeOpen('tbody', [ $body = $this->nodeOpen(
'tbody',
[
'class' => $this->options['bodyClass'], 'class' => $this->options['bodyClass'],
]); ]
);
foreach ($this->fields as $i => $field) { foreach ($this->fields as $i => $field) {
$body .= $this->genRow($field); $body .= $this->genRow($field);
} }
@ -168,15 +178,21 @@ class BootstrapListTable extends BootstrapGeneric
$allKeyClass = $this->convertToArrayIfNeeded($this->options['keyClass'] ?? []); $allKeyClass = $this->convertToArrayIfNeeded($this->options['keyClass'] ?? []);
$keyClass = $this->convertToArrayIfNeeded($field['keyClass'] ?? []); $keyClass = $this->convertToArrayIfNeeded($field['keyClass'] ?? []);
$rowValue = $this->genCell($field); $rowValue = $this->genCell($field);
$rowKey = $this->node('th', [ $rowKey = $this->node(
'th',
[
'class' => array_merge( 'class' => array_merge(
$allKeyClass, $allKeyClass,
$keyClass, $keyClass,
!empty($this->options['fluid']) ? ['col flex-shrink-1'] : ['col-4 col-sm-3'], !empty($this->options['fluid']) ? ['col flex-shrink-1'] : ['col-4 col-sm-3'],
), ),
'scope' => 'row' 'scope' => 'row'
], $field['keyHtml'] ?? h($field['key'])); ],
$row = $this->node('tr', [ $field['keyHtml'] ?? h($field['key'])
);
$row = $this->node(
'tr',
[
'class' => array_merge( 'class' => array_merge(
$this->options['rowClass'], $this->options['rowClass'],
[ [
@ -184,7 +200,9 @@ class BootstrapListTable extends BootstrapGeneric
!empty($field['rowVariant']) ? "table-{$field['rowVariant']}" : '' !empty($field['rowVariant']) ? "table-{$field['rowVariant']}" : ''
] ]
), ),
], [$rowKey, $rowValue]); ],
[$rowKey, $rowValue]
);
return $row; return $row;
} }
@ -195,10 +213,13 @@ class BootstrapListTable extends BootstrapGeneric
} else if (isset($field['formatter'])) { } else if (isset($field['formatter'])) {
$cellContent = $field['formatter']($this->getValueFromObject($field), $this->item); $cellContent = $field['formatter']($this->getValueFromObject($field), $this->item);
} else if (isset($field['type'])) { } else if (isset($field['type'])) {
$cellContent = $this->btHelper->getView()->element($this->getElementPath($field['type']), [ $cellContent = $this->btHelper->getView()->element(
$this->getElementPath($field['type']),
[
'data' => $this->item, 'data' => $this->item,
'field' => $field 'field' => $field
]); ]
);
} else { } else {
$cellContent = h($this->getValueFromObject($field)); $cellContent = h($this->getValueFromObject($field));
} }
@ -209,7 +230,9 @@ class BootstrapListTable extends BootstrapGeneric
} }
$allValueClass = $this->convertToArrayIfNeeded($this->options['valueClass'] ?? []); $allValueClass = $this->convertToArrayIfNeeded($this->options['valueClass'] ?? []);
$valueClass = $this->convertToArrayIfNeeded($field['valueClass'] ?? []); $valueClass = $this->convertToArrayIfNeeded($field['valueClass'] ?? []);
return $this->node('td', [ return $this->node(
'td',
[
'class' => array_merge( 'class' => array_merge(
$allValueClass, $allValueClass,
$valueClass, $valueClass,
@ -218,7 +241,9 @@ class BootstrapListTable extends BootstrapGeneric
!empty($field['cellVariant']) ? "bg-{$field['cellVariant']}" : '' !empty($field['cellVariant']) ? "bg-{$field['cellVariant']}" : ''
] ]
), ),
], $cellContent); ],
$cellContent
);
} }
private function getValueFromObject(array $field): string private function getValueFromObject(array $field): string