From 0670ee42d0136cfa26a1da419c297990a0e3c92f Mon Sep 17 00:00:00 2001 From: Valentin Tsanev Date: Fri, 28 Mar 2025 10:15:49 +0200 Subject: [PATCH 1/2] Refactor prepend and getCustomKey. Extend getState method --- src/SelectTree.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index e809057..d05ec3e 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -301,7 +301,13 @@ public function multiple(Closure|bool $multiple = true): static public function prepend(Closure|array|null $prepend = null): static { - $this->prepend = $prepend; + $this->prepend = $this->evaluate($prepend); + + if (is_array($this->prepend) && isset($this->prepend['name'], $this->prepend['value'])) { + $this->prepend['value'] = (string) $this->prepend['value']; + }else{ + throw new \InvalidArgumentException('The provided prepend value must be an array with "name" and "value" keys.'); + } return $this; } @@ -441,9 +447,11 @@ public function getIndependent(): bool return $this->evaluate($this->independent); } - public function getCustomKey($record) + public function getCustomKey($record): string { - return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; + $key = is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; + + return (string) $key; } public function getWithCount(): bool @@ -614,4 +622,15 @@ public function createOptionModalHeading(string|Closure|null $heading): static return $this; } + + public function getState(): mixed + { + $state = parent::getState(); + + if (is_array($state)) { + return array_map(fn($value) => (string) $value, $state); + } + + return (string) $state; + } } From 6e72eea05a297e9268bbbc8165aa7e69709a2a57 Mon Sep 17 00:00:00 2001 From: Dennis Elsinga Date: Mon, 31 Mar 2025 16:42:22 +0200 Subject: [PATCH 2/2] Code style --- src/SelectTree.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SelectTree.php b/src/SelectTree.php index d05ec3e..8a9b7b7 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -305,7 +305,7 @@ public function prepend(Closure|array|null $prepend = null): static if (is_array($this->prepend) && isset($this->prepend['name'], $this->prepend['value'])) { $this->prepend['value'] = (string) $this->prepend['value']; - }else{ + } else { throw new \InvalidArgumentException('The provided prepend value must be an array with "name" and "value" keys.'); } @@ -449,7 +449,7 @@ public function getIndependent(): bool public function getCustomKey($record): string { - $key = is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; + $key = is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; return (string) $key; } @@ -628,7 +628,7 @@ public function getState(): mixed $state = parent::getState(); if (is_array($state)) { - return array_map(fn($value) => (string) $value, $state); + return array_map(fn ($value) => (string) $value, $state); } return (string) $state;