File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Kms/Core/Attributes/DataTable.php
<?php
namespace App\Komma\Kms\Core\Attributes;
use App\Komma\Kms\Core\Attributes\Models\DataTableColumn;
use App\Komma\Kms\Core\Attributes\Traits\ExplanationTrait;
use App\Komma\Kms\Core\Attributes\Traits\LabelTrait;
use App\Komma\Kms\Core\Attributes\Traits\ReadOnlyTrait;
/**
* Class TextField
*/
class DataTable extends Attribute
{
use LabelTrait;
use ReadOnlyTrait;
use ExplanationTrait;
private $tableColumnAmount = 0;
private $tableHasHeading = false;
private $tableStructure = [];
private $allowAddRow = true;
public $hasRowNumbers = true;
/**
* Returns a view that visually represents this attribute
*/
public function render()
{
return view('kms/attributes.dataTable', [
'attribute' => $this,
]);
}
/**
* Decode the value which is json from string to array
*
* @return array
*/
public function getDecodedValue(): array
{
$jsonString = $this->getValue();
if (empty($jsonString)) {
return [];
}
$dataRows = json_decode($jsonString);
// Validate that the data row have the same amount of columns as defined.
// Else there has been tempered within the database...
foreach ($dataRows as $dataRow) {
if (count($dataRow) !== $this->tableColumnAmount) {
throw new \UnexpectedValueException(self::class.": The database json of the data table doesn't match the given structure. Someone tempered with this data table within the database.");
}
}
return $dataRows;
}
/**
* Set the structure for the table
*
* @param array $tableStructure
* @return DataTable
*/
public function setTableStructure(array $tableStructure): self
{
$this->tableColumnAmount = count($tableStructure);
/** @var DataTableColumn $tableRow */
foreach ($tableStructure as $tableRow) {
if (! is_a($tableRow, DataTableColumn::class)) {
throw new \LogicException(self::class.": A row withing the given table structure isn't a DataTableColumn.");
}
// Check if any table row has a name, then we enable the table heading
if (! empty($tableRow->getName())) {
$this->tableHasHeading = true;
}
$this->tableStructure[] = $tableRow;
}
return $this;
}
/**
* @return bool
*/
public function hasHeading(): bool
{
return $this->tableHasHeading;
}
/**
* @return array
*/
public function getTableStructure(): array
{
return $this->tableStructure;
}
/**
* @return DataTable
*/
public function disableAddingRows(): self
{
$this->allowAddRow = false;
return $this;
}
/**
* @return bool
*/
public function addingRowsIsEnabled(): bool
{
return $this->allowAddRow;
}
}