Skip to content

Commit db7aeb6

Browse files
authored
Merge pull request #3
1.x
2 parents 38da82c + e115536 commit db7aeb6

File tree

3 files changed

+98
-44
lines changed

3 files changed

+98
-44
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ other than '`active`', and a custom class '`form-option custom`' will be applied
137137
> <small><strong>Note:</strong> Writing queries within blade templates is not recommended. This is only for simplifying
138138
> demonstration</small>
139139
140+
### Non-object Array Collections
141+
You can work with collections of non-object arrays both flat and associative
142+
```php
143+
$array1 = ["First", "Second", "Third"];
144+
$array2 = ['first' => "First", 'second' => "Second", 'third' => "Third"];
145+
$array3 = [['name' => 'First', 'number' => 1],['name' => 'Second', 'number' => 2],['name' => 'Third', 'number' => 3]]
146+
$options = collect($array)->toSelectOptions();
147+
$options2 = collect($array2)->toSelectOptions();
148+
$options3 = collect($array3)->toSelectable()->withValue('number')->toSelectOptions();
149+
```
150+
151+
140152
## Get Selectable Items
141153
```php
142154
$selectableItems = \App\Models\User::all()->toSelectable()->toSelectItems();

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"name": "master"
5252
}
5353
},
54-
"version": "1.0.2",
54+
"version": "1.0.3",
5555
"minimum-stability": "dev",
5656
"prefer-stable": true
5757
}

src/Selectable.php

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,36 @@ public function __construct(Collection $collection, string|Closure|null $label =
3535
/**
3636
* Check if the item should be selected
3737
* @param object $item
38-
* @param int|null $index
38+
* @param int|string|null $index
3939
* @return bool
4040
*/
41-
private function _shouldSelect(object $item, int|null $index = null): bool
41+
private function _shouldSelect(mixed $item, int|string|null $index = null): bool
4242
{
43-
$optionValue = ($this->_value instanceof Closure) ? call_user_func($this->_value, $item, $index) : $item->{$this->_value} ?? "";
4443
if ($this->_selected instanceof Closure) {
45-
if (call_user_func($this->_selected, $item) === true) {
46-
return true;
47-
}
48-
} else if (is_object($this->_selected)) {
49-
if ((string)$this->_selected->{$this->_value} === (string)$optionValue) {
50-
return true;
44+
return (bool)call_user_func($this->_selected, $item, $index);
45+
}
46+
47+
if($this->_value instanceof Closure) {
48+
$optionValue = call_user_func($this->_value, $item, $index);
49+
} else {
50+
$optionValue = (is_object($item) ? ($item->{$this->_value} ?? "") : $item);
51+
if(is_array($item)) {
52+
$optionValue = $item[$this->_value] ?? reset($item);
5153
}
52-
} else if (is_array($this->_selected)) {
54+
}
55+
if (is_object($this->_selected)) {
56+
return ((string)$this->_selected->{$this->_value} === (string)$optionValue);
57+
}
58+
59+
if (is_array($this->_selected)) {
5360
foreach ($this->_selected as $selectedItem) {
5461
if (is_object($selectedItem)) {
55-
if ((string)$selectedItem->{$this->_value} === (string)$optionValue) {
56-
return true;
57-
}
58-
} else if (is_array($selectedItem)) {
59-
if (array_key_exists($this->_value, $selectedItem) && (string)$selectedItem[$this->_value] === (string)$optionValue) {
60-
return true;
61-
}
62-
} else if ((string)$selectedItem === (string)$optionValue) {
62+
return ((string)$selectedItem->{$this->_value} === (string)$optionValue);
63+
}
64+
if (is_array($selectedItem)) {
65+
return (array_key_exists($this->_value, $selectedItem) && (string)$selectedItem[$this->_value] === (string)$optionValue);
66+
}
67+
if ((string)$selectedItem === (string)$optionValue) {
6368
return true;
6469
}
6570
}
@@ -71,32 +76,38 @@ private function _shouldSelect(object $item, int|null $index = null): bool
7176

7277
/**
7378
* Check if the item should be selected
74-
* @param object $item
75-
* @param int|null $index
79+
* @param mixed $item
80+
* @param int|string|null $index
7681
* @return bool
7782
*/
78-
private function _shouldDisable(object $item, int|null $index = null): bool
83+
private function _shouldDisable(mixed $item, int|string|null $index = null): bool
7984
{
80-
$lineValue = $item->{$this->_value} ?? "";
81-
if (is_callable($this->_disabled)) {
82-
if (call_user_func($this->_disabled, $item) === true) {
83-
return true;
84-
}
85-
} else if (is_object($this->_disabled)) {
86-
if ((string)$this->_disabled->{$this->_value} === (string)$lineValue) {
87-
return true;
85+
if ($this->_disabled instanceof Closure) {
86+
return (bool)call_user_func($this->_disabled, $item, $index);
87+
}
88+
89+
if ($this->_value instanceof Closure) {
90+
$lineValue = call_user_func($this->_value, $item, $index);
91+
} else {
92+
$lineValue = (is_object($item) ? ($item->{$this->_value} ?? "") : $item);
93+
if(is_array($item)){
94+
$lineValue = $item[$this->_value] ??reset($item);
8895
}
89-
} else if (is_array($this->_disabled)) {
96+
}
97+
98+
if (is_object($this->_disabled)) {
99+
return ((string)$this->_disabled->{$this->_value} === (string)$lineValue);
100+
}
101+
102+
if (is_array($this->_disabled)) {
90103
foreach ($this->_disabled as $disabledItem) {
91104
if (is_object($disabledItem)) {
92-
if ((string)$disabledItem->{$this->_value} === (string)$lineValue) {
93-
return true;
94-
}
95-
} else if (is_array($disabledItem)) {
96-
if (array_key_exists($this->_value, $disabledItem) && (string)$disabledItem[$this->_value] === (string)$lineValue) {
97-
return true;
98-
}
99-
} else if ((string)$disabledItem === (string)$lineValue) {
105+
return ((string)$disabledItem->{$this->_value} === (string)$lineValue);
106+
}
107+
if (is_array($disabledItem)) {
108+
return (array_key_exists($this->_value, $disabledItem) && (string)$disabledItem[$this->_value] === (string)$lineValue);
109+
}
110+
if ((string)$disabledItem === (string)$lineValue) {
100111
return true;
101112
}
102113
}
@@ -126,9 +137,10 @@ private function _getDataAttributes(mixed $item, int|null $index = null): array
126137
/**
127138
* Generate select options from a Collection instance
128139
* @param Collection $collection
140+
* @param int $lastIndex
129141
* @return string
130142
*/
131-
private function _generateOptions(Collection $collection): string
143+
private function _generateOptions(Collection $collection, int $lastIndex = 0): string
132144
{
133145
$html = "";
134146
foreach ($collection as $index => $item) {
@@ -137,8 +149,25 @@ private function _generateOptions(Collection $collection): string
137149
$html .= $this->_generateOptions($item);
138150
$html .= "</optgroup>";
139151
} else {
140-
$optionLabel = ($this->_label instanceof Closure) ? call_user_func($this->_label, $item, $index) : $item->{$this->_label} ?? "N/A";
141-
$optionValue = ($this->_value instanceof Closure) ? call_user_func($this->_value, $item, $index) : $item->{$this->_value} ?? "";
152+
if ($this->_label instanceof Closure) {
153+
$optionLabel = call_user_func($this->_label, $item, $index);
154+
} else {
155+
$optionLabel = is_object($item) ? ($item->{$this->_label} ?? "N/A") : ($item);
156+
if(is_array($item)) {
157+
$optionLabel = $item[$this->_label] ?? array_keys($item)[0];
158+
}
159+
}
160+
if ($this->_value instanceof Closure) {
161+
$optionValue = call_user_func($this->_value, $item, $index);
162+
} else {
163+
$optionValue = is_object($item) ? ($item->{$this->_value} ?? "") : $item;
164+
if(is_array($item)) {
165+
$optionValue = $item[$this->_value] ?? reset($item);
166+
}
167+
if (is_string($index) && is_string($item)) {
168+
$optionValue = $index;
169+
}
170+
}
142171
$html .= "<option value=\"{$optionValue}\"";
143172
if ($this->_shouldSelect($item, $index)) {
144173
$html .= " selected";
@@ -206,9 +235,22 @@ public function toSelectOptions(): string
206235
public function toSelectItems(): Collection
207236
{
208237
return $this->_collection->map(function ($item, $index) {
238+
if ($this->_label instanceof Closure) {
239+
$optionLabel = call_user_func($this->_label, $item, $index);
240+
} else {
241+
$optionLabel = is_object($item) ? ($item->{$this->_label} ?? "N/A") : ($item);
242+
}
243+
if ($this->_value instanceof Closure) {
244+
$optionValue = call_user_func($this->_value, $item, $index);
245+
} else {
246+
$optionValue = is_object($item) ? ($item->{$this->_value} ?? "") : $item;
247+
if (is_string($index) && is_string($item)) {
248+
$optionValue = $index;
249+
}
250+
}
209251
return [
210-
'value' => $item->{$this->_value} ?? "",
211-
'label' => $index,
252+
'value' => $optionValue,
253+
'label' => $optionLabel,
212254
'isSelected' => $this->_shouldSelect($item, $index),
213255
'isDisabled' => $this->_shouldDisable($item, $index),
214256
'data' => $this->_getDataAttributes($item, $index),

0 commit comments

Comments
 (0)