Skip to content

Commit

Permalink
Add ArrayColumn (BETA) (#1751)
Browse files Browse the repository at this point in the history
* Add ArrayColumn

* Fix styling

---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe committed Jul 1, 2024
1 parent 119401b commit ea2c8fc
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Views/Columns/ArrayColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Columns;

use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ArrayColumnConfiguration;
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ArrayColumnHelpers;
use Rappasoft\LaravelLivewireTables\Views\Traits\IsColumn;

class ArrayColumn extends Column
{
use IsColumn,
ArrayColumnConfiguration,
ArrayColumnHelpers { ArrayColumnHelpers::getContents insteadof IsColumn; }

public string $separator = '<br />';

public string $emptyValue = '';

protected mixed $dataCallback = null;

protected mixed $outputFormat = null;

public function __construct(string $title, ?string $from = null)
{
parent::__construct($title, $from);
if (! isset($from)) {
$this->label(fn () => null);
}
}
}
29 changes: 29 additions & 0 deletions src/Views/Traits/Configuration/ArrayColumnConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration;

use Closure;

trait ArrayColumnConfiguration
{
public function separator(string $value): self
{
$this->separator = $value;

return $this;
}

public function data(callable $callable): self
{
$this->dataCallback = $callable;

return $this;
}

public function outputFormat(callable $callable): self
{
$this->outputFormat = $callable;

return $this;
}
}
69 changes: 69 additions & 0 deletions src/Views/Traits/Helpers/ArrayColumnHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\HtmlString;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;

trait ArrayColumnHelpers
{
public function hasSeparator(): bool
{
return $this->separator !== null && is_string($this->separator);
}

public function getSeparator(): string
{
return $this->separator;
}

public function getEmptyValue(): string
{
return $this->emptyValue;
}

public function hasDataCallback(): bool
{
return isset($this->dataCallback) && is_callable($this->dataCallback);
}

public function getDataCallback(): ?callable
{
return $this->dataCallback;
}

public function hasOutputFormatCallback(): bool
{
return isset($this->outputFormat) && is_callable($this->outputFormat);
}

public function getOutputFormatCallback(): ?callable
{
return $this->outputFormat;
}

public function getContents(Model $row): null|string|\BackedEnum|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
$outputValues = [];
$value = $this->getValue($row);

if (! $this->hasSeparator()) {
throw new DataTableConfigurationException('You must set a valid separator on an ArrayColumn');
}

if (! $this->hasDataCallback()) {
throw new DataTableConfigurationException('You must set a data() method on an ArrayColumn');
}

if (! $this->hasOutputFormatCallback()) {
throw new DataTableConfigurationException('You must set an outputFormat() method on an ArrayColumn');
}

foreach (call_user_func($this->getDataCallback(), $value, $row) as $i => $v) {
$outputValues[] = call_user_func($this->getOutputFormatCallback(), $i, $v);
}

return new HtmlString((! empty($outputValues) ? implode($this->getSeparator(), $outputValues) : $this->getEmptyValue()));
}
}

0 comments on commit ea2c8fc

Please sign in to comment.