Skip to content

Commit b5d3bfa

Browse files
committed
Documentation corrections
1 parent 03fecf1 commit b5d3bfa

File tree

4 files changed

+74
-39
lines changed

4 files changed

+74
-39
lines changed
File renamed without changes.

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,50 @@ php artisan vendor:publish --tag="laravel-selectable-views"
4646
```
4747
The output for this code will be as follows
4848

49-
```html
49+
```bladehtml
5050
<option value="{{$user->id}}">{{$user->name}}</option>
5151
...
5252
```
5353

5454
### 2. Custom text and value for options
55-
```html
55+
```bladehtml
5656
<select name="user_id" >
5757
{!! \\App\\Model\\User::all()->toSelectOptions('email', 'uuid'); !!}}
5858
</select>
5959
```
6060
The output for this code will be as follows
6161

62-
```html
62+
```bladehtml
6363
<option value="{{$user->uuid}}">{{$user->email}}</option>
6464
...
6565
```
6666

67+
### 3. Advanced use
68+
The method `toSelectable` is used to convert the collection into a `Selectable` object. The `Selectable` object has several methods that allow you to customize the options and their properties.
69+
70+
```bladehtml
71+
<select name="user_id" multiple="multiple">
72+
{!!
73+
$x = \App\Models\User::all()
74+
->toSelectable()
75+
->withValue('id')
76+
->withLabel(fn($user) => "{$user->first_name} {$user->last_name}")
77+
->withSelected([4, 5])
78+
->withDisabled(fn($item) => $item->status = 'inactive')
79+
->toSelectOptions();
80+
!!}
81+
</select>
82+
```
83+
### 4. Available methods
84+
- `toSelectable()`: This method returns the current selectable object.
85+
- `withLabel(string|callable $label)`: This method allows you to customize the label for each option. A string will be used as the collection field from which the label will be generated, while a callable will be used to generate the label.
86+
- `withValue(string|callable $value)`: This method allows you to customize the value for each option. A string will be used as the collection field from which the value will be generated, while a callable will be used to generate the value.
87+
- `withSelected(mixed|callable $selected)`: This method allows you to customize the selected options. Can be a `string`, `int`, an array of `string`/`int`, a `model` or a callable that returns a boolean value.
88+
- `withDisabled(mixed|callable $disabled)`: This method allows you to customize the disabled options. Can be a `string`, `int`, an array of `string`/`int`, a `model` or a callable that returns a boolean value.
89+
- `toSelectOptions()`: This method converts the selectable collection to an HTML select options string.
90+
- `toSelectItems()`: This method converts the selectable collection to an array of selectable items. Useful for Ajax responses or SPA.
91+
-
92+
6793
> <small><strong>Note:</strong> Writing queries within blade templates is not recommended. This is only for simplifying demonstration</small>
6894
6995
## Testing

SECURITY.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Supported Versions
44

5-
The following versions of laravel process approval package are
5+
The following versions of laravel selectable package are
66
currently being supported with security updates.
77

88
| Version | Supported |
@@ -11,7 +11,7 @@ currently being supported with security updates.
1111

1212
## Reporting a Vulnerability
1313

14-
We take the security of the Laravel Process Approval package seriously. If you have discovered a vulnerability, we appreciate your assistance in disclosing it responsibly. This ensures that we can address and resolve the issue promptly to maintain the security of our users.
14+
We take the security of the Laravel Selectable package seriously. If you have discovered a vulnerability, we appreciate your assistance in disclosing it responsibly. This ensures that we can address and resolve the issue promptly to maintain the security of our users.
1515

1616
### Reporting Process
1717
To report a vulnerability, please follow these steps:
@@ -29,7 +29,7 @@ We strive to acknowledge all vulnerability reports promptly. Our team will revie
2929

3030
### Vulnerability Handling
3131
#### Accepted Vulnerabilities
32-
If the reported vulnerability is accepted, we will work on a fix and release a new version of the Laravel Process Approval package. We will credit you for the responsible disclosure unless you prefer to remain anonymous.
32+
If the reported vulnerability is accepted, we will work on a fix and release a new version of the Laravel Selectable package. We will credit you for the responsible disclosure unless you prefer to remain anonymous.
3333

3434
#### Declined Vulnerabilities
3535
If the reported vulnerability is deemed not to be a valid issue, we will provide a detailed explanation of our findings. You can appeal our decision if you believe there has been a misunderstanding.
@@ -40,4 +40,4 @@ We understand the sensitivity of security-related information. We will handle al
4040
### Responsible Disclosure
4141
We appreciate your responsible disclosure of security vulnerabilities. We encourage you to avoid publicizing the vulnerability until we have had an opportunity to address it. We will keep you informed of the progress and coordinate the public disclosure timeline.
4242

43-
Thank you for helping us keep Laravel Process Approval secure for everyone. Your efforts contribute to the safety and well-being of our user community.
43+
Thank you for helping us keep Laravel Selectable secure for everyone. Your efforts contribute to the safety and well-being of our user community.

src/Selectable.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22

33
namespace RingleSoft\LaravelSelectable;
44

5+
use Closure;
56
use Illuminate\Support\Collection;
7+
use ReflectionFunction;
68

79
class Selectable
810
{
911
private Collection $_collection;
10-
private string $_value;
11-
private string $_label;
12+
private string|Closure $_value;
13+
private string|Closure $_label;
1214
private mixed $_selected = null;
1315
private mixed $_disabled = null;
1416

15-
public function __construct(Collection $collection, string|null $label = null, string|null $value = null, mixed $selected = null, mixed $disabled = null)
17+
/**
18+
* @param Collection $collection
19+
* @param string|Closure|null $label
20+
* @param string|Closure|null $value
21+
* @param mixed|null $selected
22+
* @param mixed|null $disabled
23+
*/
24+
public function __construct(Collection $collection, string|Closure|null $label = null, string|Closure|null $value = null, mixed $selected = null, mixed $disabled = null)
1625
{
1726
$this->_collection = $collection;
1827
$this->_label = $label ?? 'name';
@@ -49,55 +58,54 @@ public static function collectionToSelectOptions(
4958
*/
5059
public function toSelectOptions(): string
5160
{
52-
53-
5461
$html = "";
5562
foreach ($this->_collection as $index => $item) {
56-
$lineLabel = $item->{$this->_label} ?? "N/A";
57-
$lineValue = $item->{$this->_value} ?? "";
58-
$html .= "<option value=\"{$lineValue}\"";
59-
if($this->_shouldSelect($item)){
63+
$optionLabel = ($this->_label instanceof Closure) ? call_user_func($this->_label, $item, $index) : $item->{$this->_label} ?? "N/A";
64+
$optionValue = ($this->_value instanceof Closure) ? call_user_func($this->_value, $item, $index) : $item->{$this->_value} ?? "";
65+
$html .= "<option value=\"{$optionValue}\"";
66+
if ($this->_shouldSelect($item, $index)) {
6067
$html .= " selected";
6168
}
62-
if($this->_shouldDisable($item)){
69+
if ($this->_shouldDisable($item, $index)) {
6370
$html .= " disabled";
6471
}
65-
$html .= " >{$lineLabel}</option>";
72+
$html .= " >{$optionLabel}</option>";
6673
}
6774
return $html;
6875
}
6976

7077
/**
7178
* Check if the item should be selected
7279
* @param object $item
80+
* @param int|null $index
7381
* @return bool
7482
*/
75-
private function _shouldSelect(object $item): bool
83+
private function _shouldSelect(object $item, int|null $index = null): bool
7684
{
77-
$lineValue = $item->{$this->_value} ?? "";
78-
if (is_callable($this->_selected)) {
85+
$optionValue = ($this->_value instanceof Closure) ? call_user_func($this->_value, $item, $index) : $item->{$this->_value} ?? "";
86+
if ($this->_selected instanceof Closure) {
7987
if (call_user_func($this->_selected, $item) === true) {
8088
return true;
8189
}
82-
} else if(is_object($this->_selected)){
83-
if((string)$this->_selected->{$this->_value} === (string)$lineValue){
90+
} else if (is_object($this->_selected)) {
91+
if ((string)$this->_selected->{$this->_value} === (string)$optionValue) {
8492
return true;
8593
}
8694
} else if (is_array($this->_selected)) {
8795
foreach ($this->_selected as $selectedItem) {
8896
if (is_object($selectedItem)) {
89-
if ((string)$selectedItem->{$this->_value} === (string)$lineValue) {
97+
if ((string)$selectedItem->{$this->_value} === (string)$optionValue) {
9098
return true;
9199
}
92100
} else if (is_array($selectedItem)) {
93-
if (array_key_exists($this->_value, $selectedItem) && (string)$selectedItem[$this->_value] === (string)$lineValue) {
101+
if (array_key_exists($this->_value, $selectedItem) && (string)$selectedItem[$this->_value] === (string)$optionValue) {
94102
return true;
95103
}
96-
} else if ((string)$selectedItem === (string)$lineValue) {
104+
} else if ((string)$selectedItem === (string)$optionValue) {
97105
return true;
98106
}
99107
}
100-
} else if ((string)$this->_selected === (string)$lineValue) {
108+
} else if ((string)$this->_selected === (string)$optionValue) {
101109
return true;
102110
}
103111
return false;
@@ -106,17 +114,18 @@ private function _shouldSelect(object $item): bool
106114
/**
107115
* Check if the item should be selected
108116
* @param object $item
117+
* @param int|null $index
109118
* @return bool
110119
*/
111-
private function _shouldDisable(object $item): bool
120+
private function _shouldDisable(object $item, int|null $index = null): bool
112121
{
113122
$lineValue = $item->{$this->_value} ?? "";
114123
if (is_callable($this->_disabled)) {
115124
if (call_user_func($this->_disabled, $item) === true) {
116125
return true;
117126
}
118-
} else if(is_object($this->_disabled)){
119-
if((string)$this->_disabled->{$this->_value} === (string)$lineValue){
127+
} else if (is_object($this->_disabled)) {
128+
if ((string)$this->_disabled->{$this->_value} === (string)$lineValue) {
120129
return true;
121130
}
122131
} else if (is_array($this->_disabled)) {
@@ -133,7 +142,7 @@ private function _shouldDisable(object $item): bool
133142
return true;
134143
}
135144
}
136-
} else if ((string) $this->_disabled === (string) $lineValue) {
145+
} else if ((string)$this->_disabled === (string)$lineValue) {
137146
return true;
138147
}
139148
return false;
@@ -145,33 +154,33 @@ private function _shouldDisable(object $item): bool
145154
*/
146155
public function toSelectItems(): Collection
147156
{
148-
return $this->_collection->map(function ($item) {
157+
return $this->_collection->map(function ($item, $index) {
149158
return [
150159
'value' => $item->{$this->_value} ?? "",
151-
'label' => $item->{$this->_label},
152-
'isSelected' => $this->_shouldSelect($item),
153-
'isDisabled' => $this->_shouldDisable($item),
160+
'label' => $index,
161+
'isSelected' => $this->_shouldSelect($item, $index),
162+
'isDisabled' => $this->_shouldDisable($item, $index),
154163
];
155164
});
156165
}
157166

158167
//// Builders
159168

160169
/**
161-
* @param string $label name of the field to be used as label
170+
* @param string|Closure $label name of the field to be used as label
162171
* @return $this
163172
*/
164-
public function withLabel(string $label): self
173+
public function withLabel(string|Closure $label): self
165174
{
166175
$this->_label = $label;
167176
return $this;
168177
}
169178

170179
/**
171-
* @param string $value name of the field to be used as value
180+
* @param string|Closure $value name of the field to be used as value
172181
* @return $this
173182
*/
174-
public function withValue(string $value): self
183+
public function withValue(string|Closure $value): self
175184
{
176185
$this->_value = $value;
177186
return $this;

0 commit comments

Comments
 (0)