Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ other than '`active`', and a custom class '`form-option custom`' will be applied
> <small><strong>Note:</strong> Writing queries within blade templates is not recommended. This is only for simplifying
> demonstration</small>

### 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();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"name": "master"
}
},
"version": "1.0.2",
"version": "1.0.3",
"minimum-stability": "dev",
"prefer-stable": true
}
128 changes: 85 additions & 43 deletions src/Selectable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -137,8 +149,25 @@ private function _generateOptions(Collection $collection): string
$html .= $this->_generateOptions($item);
$html .= "</optgroup>";
} 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 .= "<option value=\"{$optionValue}\"";
if ($this->_shouldSelect($item, $index)) {
$html .= " selected";
Expand Down Expand Up @@ -206,9 +235,22 @@ public function toSelectOptions(): string
public function toSelectItems(): Collection
{
return $this->_collection->map(function ($item, $index) {
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 ($this->_value instanceof Closure) {
$optionValue = call_user_func($this->_value, $item, $index);
} else {
$optionValue = is_object($item) ? ($item->{$this->_value} ?? "") : $item;
if (is_string($index) && is_string($item)) {
$optionValue = $index;
}
}
return [
'value' => $item->{$this->_value} ?? "",
'label' => $index,
'value' => $optionValue,
'label' => $optionLabel,
'isSelected' => $this->_shouldSelect($item, $index),
'isDisabled' => $this->_shouldDisable($item, $index),
'data' => $this->_getDataAttributes($item, $index),
Expand Down