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,15 +2,14 @@
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.
* Perfect to display the Key-Values of an object. * Perfect to display the Key-Values of an object.
* *
* # Options for table * # Options for table
* - striped, bordered, borderless, hover, small: Default bootstrap behavior * - striped, bordered, borderless, hover, small: Default bootstrap behavior
* - variant: Variant to apply on the entire table * - variant: Variant to apply on the entire table
@ -21,10 +20,10 @@ use App\View\Helper\BootstrapHelper;
* - id: The ID to use for the table * - id: The ID to use for the table
* - caption: Optional table caption * - caption: Optional table caption
* - elementsRootPath: Root path to use when item are relying on cakephp's element. See options for fields * - elementsRootPath: Root path to use when item are relying on cakephp's element. See options for fields
* *
* # Items * # Items
* - They have the content that's used to generate the table. Typically and array<array> or array<entity> * - They have the content that's used to generate the table. Typically and array<array> or array<entity>
* *
* # Options for fields * # Options for fields
* - key: The name of the field to be displayed as a label * - key: The name of the field to be displayed as a label
* - keyHtml: The HTML of the field to be displayed as a label * - keyHtml: The HTML of the field to be displayed as a label
@ -39,7 +38,7 @@ use App\View\Helper\BootstrapHelper;
* - rowClass: A list of class to be added to the row * - rowClass: A list of class to be added to the row
* - keyClass: A list of class to be added to the key cell * - keyClass: A list of class to be added to the key cell
* - valueClass: A list of class to be added to the value cell * - valueClass: A list of class to be added to the value cell
* *
* # Usage: * # Usage:
* $this->Bootstrap->listTable( * $this->Bootstrap->listTable(
* [ * [
@ -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,20 +133,23 @@ class BootstrapListTable extends BootstrapGeneric
private function genTable(): string private function genTable(): string
{ {
$html = $this->nodeOpen('table', [ $html = $this->nodeOpen(
'class' => [ 'table',
'table', [
"table-{$this->options['variant']}", 'class' => [
$this->options['striped'] ? 'table-striped' : '', 'table',
$this->options['bordered'] ? 'table-bordered' : '', "table-{$this->options['variant']}",
$this->options['borderless'] ? 'table-borderless' : '', $this->options['striped'] ? 'table-striped' : '',
$this->options['hover'] ? 'table-hover' : '', $this->options['bordered'] ? 'table-bordered' : '',
$this->options['small'] ? 'table-sm' : '', $this->options['borderless'] ? 'table-borderless' : '',
implode(' ', $this->options['tableClass']), $this->options['hover'] ? 'table-hover' : '',
!empty($this->options['variant']) ? "table-{$this->options['variant']}" : '', $this->options['small'] ? 'table-sm' : '',
], implode(' ', $this->options['tableClass']),
'id' => $this->options['id'] ?? '' !empty($this->options['variant']) ? "table-{$this->options['variant']}" : '',
]); ],
'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(
'class' => $this->options['bodyClass'], 'tbody',
]); [
'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,23 +178,31 @@ 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(
'class' => array_merge( 'th',
$allKeyClass, [
$keyClass, 'class' => array_merge(
!empty($this->options['fluid']) ? ['col flex-shrink-1'] : ['col-4 col-sm-3'], $allKeyClass,
), $keyClass,
'scope' => 'row' !empty($this->options['fluid']) ? ['col flex-shrink-1'] : ['col-4 col-sm-3'],
], $field['keyHtml'] ?? h($field['key'])); ),
$row = $this->node('tr', [ 'scope' => 'row'
'class' => array_merge( ],
$this->options['rowClass'], $field['keyHtml'] ?? h($field['key'])
[ );
'd-flex', $row = $this->node(
!empty($field['rowVariant']) ? "table-{$field['rowVariant']}" : '' 'tr',
] [
), 'class' => array_merge(
], [$rowKey, $rowValue]); $this->options['rowClass'],
[
'd-flex',
!empty($field['rowVariant']) ? "table-{$field['rowVariant']}" : ''
]
),
],
[$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(
'data' => $this->item, $this->getElementPath($field['type']),
'field' => $field [
]); 'data' => $this->item,
'field' => $field
]
);
} else { } else {
$cellContent = h($this->getValueFromObject($field)); $cellContent = h($this->getValueFromObject($field));
} }
@ -209,16 +230,20 @@ 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(
'class' => array_merge( 'td',
$allValueClass, [
$valueClass, 'class' => array_merge(
[ $allValueClass,
(!empty($this->options['fluid']) ? 'col flex-grow-1' : 'col-8 col-sm-9'), $valueClass,
!empty($field['cellVariant']) ? "bg-{$field['cellVariant']}" : '' [
] (!empty($this->options['fluid']) ? 'col flex-grow-1' : 'col-8 col-sm-9'),
), !empty($field['cellVariant']) ? "bg-{$field['cellVariant']}" : ''
], $cellContent); ]
),
],
$cellContent
);
} }
private function getValueFromObject(array $field): string private function getValueFromObject(array $field): string
@ -241,4 +266,4 @@ class BootstrapListTable extends BootstrapGeneric
{ {
return !empty($this->caption) ? $this->node('caption', [], h($this->caption)) : ''; return !empty($this->caption) ? $this->node('caption', [], h($this->caption)) : '';
} }
} }