diff --git a/README.md b/README.md
index ec30c4f..5f101f9 100644
--- a/README.md
+++ b/README.md
@@ -137,6 +137,18 @@ other than '`active`', and a custom class '`form-option custom`' will be applied
> Note: Writing queries within blade templates is not recommended. This is only for simplifying
> demonstration
+### Non-object Array Collections
+You can work with collections of non-object arrays both flat and associative
+```php
+ $array1 = ["First", "Second", "Third"];
+ $array2 = ['first' => "First", 'second' => "Second", 'third' => "Third"];
+ $array3 = [['name' => 'First', 'number' => 1],['name' => 'Second', 'number' => 2],['name' => 'Third', 'number' => 3]]
+ $options = collect($array)->toSelectOptions();
+ $options2 = collect($array2)->toSelectOptions();
+ $options3 = collect($array3)->toSelectable()->withValue('number')->toSelectOptions();
+```
+
+
## Get Selectable Items
```php
$selectableItems = \App\Models\User::all()->toSelectable()->toSelectItems();
diff --git a/composer.json b/composer.json
index 40e0b8d..4579b04 100644
--- a/composer.json
+++ b/composer.json
@@ -51,7 +51,7 @@
"name": "master"
}
},
- "version": "1.0.2",
+ "version": "1.0.3",
"minimum-stability": "dev",
"prefer-stable": true
}
diff --git a/src/Selectable.php b/src/Selectable.php
index 6b0c2d3..938d01c 100644
--- a/src/Selectable.php
+++ b/src/Selectable.php
@@ -35,31 +35,36 @@ public function __construct(Collection $collection, string|Closure|null $label =
/**
* Check if the item should be selected
* @param object $item
- * @param int|null $index
+ * @param int|string|null $index
* @return bool
*/
- private function _shouldSelect(object $item, int|null $index = null): bool
+ private function _shouldSelect(mixed $item, int|string|null $index = null): bool
{
- $optionValue = ($this->_value instanceof Closure) ? call_user_func($this->_value, $item, $index) : $item->{$this->_value} ?? "";
if ($this->_selected instanceof Closure) {
- if (call_user_func($this->_selected, $item) === true) {
- return true;
- }
- } else if (is_object($this->_selected)) {
- if ((string)$this->_selected->{$this->_value} === (string)$optionValue) {
- return true;
+ return (bool)call_user_func($this->_selected, $item, $index);
+ }
+
+ if($this->_value instanceof Closure) {
+ $optionValue = call_user_func($this->_value, $item, $index);
+ } else {
+ $optionValue = (is_object($item) ? ($item->{$this->_value} ?? "") : $item);
+ if(is_array($item)) {
+ $optionValue = $item[$this->_value] ?? reset($item);
}
- } else if (is_array($this->_selected)) {
+ }
+ if (is_object($this->_selected)) {
+ return ((string)$this->_selected->{$this->_value} === (string)$optionValue);
+ }
+
+ if (is_array($this->_selected)) {
foreach ($this->_selected as $selectedItem) {
if (is_object($selectedItem)) {
- if ((string)$selectedItem->{$this->_value} === (string)$optionValue) {
- return true;
- }
- } else if (is_array($selectedItem)) {
- if (array_key_exists($this->_value, $selectedItem) && (string)$selectedItem[$this->_value] === (string)$optionValue) {
- return true;
- }
- } else if ((string)$selectedItem === (string)$optionValue) {
+ return ((string)$selectedItem->{$this->_value} === (string)$optionValue);
+ }
+ if (is_array($selectedItem)) {
+ return (array_key_exists($this->_value, $selectedItem) && (string)$selectedItem[$this->_value] === (string)$optionValue);
+ }
+ if ((string)$selectedItem === (string)$optionValue) {
return true;
}
}
@@ -71,32 +76,38 @@ private function _shouldSelect(object $item, int|null $index = null): bool
/**
* Check if the item should be selected
- * @param object $item
- * @param int|null $index
+ * @param mixed $item
+ * @param int|string|null $index
* @return bool
*/
- private function _shouldDisable(object $item, int|null $index = null): bool
+ private function _shouldDisable(mixed $item, int|string|null $index = null): bool
{
- $lineValue = $item->{$this->_value} ?? "";
- if (is_callable($this->_disabled)) {
- if (call_user_func($this->_disabled, $item) === true) {
- return true;
- }
- } else if (is_object($this->_disabled)) {
- if ((string)$this->_disabled->{$this->_value} === (string)$lineValue) {
- return true;
+ if ($this->_disabled instanceof Closure) {
+ return (bool)call_user_func($this->_disabled, $item, $index);
+ }
+
+ if ($this->_value instanceof Closure) {
+ $lineValue = call_user_func($this->_value, $item, $index);
+ } else {
+ $lineValue = (is_object($item) ? ($item->{$this->_value} ?? "") : $item);
+ if(is_array($item)){
+ $lineValue = $item[$this->_value] ??reset($item);
}
- } else if (is_array($this->_disabled)) {
+ }
+
+ if (is_object($this->_disabled)) {
+ return ((string)$this->_disabled->{$this->_value} === (string)$lineValue);
+ }
+
+ if (is_array($this->_disabled)) {
foreach ($this->_disabled as $disabledItem) {
if (is_object($disabledItem)) {
- if ((string)$disabledItem->{$this->_value} === (string)$lineValue) {
- return true;
- }
- } else if (is_array($disabledItem)) {
- if (array_key_exists($this->_value, $disabledItem) && (string)$disabledItem[$this->_value] === (string)$lineValue) {
- return true;
- }
- } else if ((string)$disabledItem === (string)$lineValue) {
+ return ((string)$disabledItem->{$this->_value} === (string)$lineValue);
+ }
+ if (is_array($disabledItem)) {
+ return (array_key_exists($this->_value, $disabledItem) && (string)$disabledItem[$this->_value] === (string)$lineValue);
+ }
+ if ((string)$disabledItem === (string)$lineValue) {
return true;
}
}
@@ -126,9 +137,10 @@ private function _getDataAttributes(mixed $item, int|null $index = null): array
/**
* Generate select options from a Collection instance
* @param Collection $collection
+ * @param int $lastIndex
* @return string
*/
- private function _generateOptions(Collection $collection): string
+ private function _generateOptions(Collection $collection, int $lastIndex = 0): string
{
$html = "";
foreach ($collection as $index => $item) {
@@ -137,8 +149,25 @@ private function _generateOptions(Collection $collection): string
$html .= $this->_generateOptions($item);
$html .= "";
} else {
- $optionLabel = ($this->_label instanceof Closure) ? call_user_func($this->_label, $item, $index) : $item->{$this->_label} ?? "N/A";
- $optionValue = ($this->_value instanceof Closure) ? call_user_func($this->_value, $item, $index) : $item->{$this->_value} ?? "";
+ if ($this->_label instanceof Closure) {
+ $optionLabel = call_user_func($this->_label, $item, $index);
+ } else {
+ $optionLabel = is_object($item) ? ($item->{$this->_label} ?? "N/A") : ($item);
+ if(is_array($item)) {
+ $optionLabel = $item[$this->_label] ?? array_keys($item)[0];
+ }
+ }
+ if ($this->_value instanceof Closure) {
+ $optionValue = call_user_func($this->_value, $item, $index);
+ } else {
+ $optionValue = is_object($item) ? ($item->{$this->_value} ?? "") : $item;
+ if(is_array($item)) {
+ $optionValue = $item[$this->_value] ?? reset($item);
+ }
+ if (is_string($index) && is_string($item)) {
+ $optionValue = $index;
+ }
+ }
$html .= "