From ecab094600aa310efe7a6c59080a0774423c5625 Mon Sep 17 00:00:00 2001 From: "P. K. Tharindu" Date: Thu, 1 Oct 2020 15:46:53 +0530 Subject: [PATCH] ability to add array, Closure or Collection to options --- src/Traits/HasOptions.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Traits/HasOptions.php b/src/Traits/HasOptions.php index bf2c01c..3c089b6 100644 --- a/src/Traits/HasOptions.php +++ b/src/Traits/HasOptions.php @@ -12,14 +12,18 @@ trait HasOptions public $callableOptions; /** - * Flat key => value based Array. + * Flat key => value based Array, Collection or Closure. * You can use a component method; ->options($this->someMethod()) - * @param array $options + * @param array|\Closure|\Illuminate\Support\Collection $options * @return $this */ - public function options(array $options = []): self + public function options($options): self { - $this->arrayFlipOrCombine($options); + if (is_callable($options)) { + $options = $options(); + } + + $this->arrayFlipOrCombine(collect($options ?? [])->toArray()); return $this; }