-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ArrayColumn * Fix styling --------- Co-authored-by: lrljoe <[email protected]>
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/Views/Traits/Configuration/ArrayColumnConfiguration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |