From ea2c8fc3cfa6e8c2bb808946d349147b9589bffe Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Mon, 1 Jul 2024 13:48:45 +0100 Subject: [PATCH] Add ArrayColumn (BETA) (#1751) * Add ArrayColumn * Fix styling --------- Co-authored-by: lrljoe --- src/Views/Columns/ArrayColumn.php | 31 +++++++++ .../ArrayColumnConfiguration.php | 29 ++++++++ .../Traits/Helpers/ArrayColumnHelpers.php | 69 +++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 src/Views/Columns/ArrayColumn.php create mode 100644 src/Views/Traits/Configuration/ArrayColumnConfiguration.php create mode 100644 src/Views/Traits/Helpers/ArrayColumnHelpers.php diff --git a/src/Views/Columns/ArrayColumn.php b/src/Views/Columns/ArrayColumn.php new file mode 100644 index 000000000..be9366db0 --- /dev/null +++ b/src/Views/Columns/ArrayColumn.php @@ -0,0 +1,31 @@ +'; + + 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); + } + } +} diff --git a/src/Views/Traits/Configuration/ArrayColumnConfiguration.php b/src/Views/Traits/Configuration/ArrayColumnConfiguration.php new file mode 100644 index 000000000..72188fd79 --- /dev/null +++ b/src/Views/Traits/Configuration/ArrayColumnConfiguration.php @@ -0,0 +1,29 @@ +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; + } +} diff --git a/src/Views/Traits/Helpers/ArrayColumnHelpers.php b/src/Views/Traits/Helpers/ArrayColumnHelpers.php new file mode 100644 index 000000000..7c32f7c70 --- /dev/null +++ b/src/Views/Traits/Helpers/ArrayColumnHelpers.php @@ -0,0 +1,69 @@ +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())); + } +}