Skip to content

Commit e69bc94

Browse files
authored
Merge pull request #2 from ray-di/docs/improve-array-documentation
docs: Improve array support documentation
2 parents a788bf0 + bc129bc commit e69bc94

File tree

1 file changed

+64
-39
lines changed

1 file changed

+64
-39
lines changed

README.md

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,81 @@ final class UserListController
153153
}
154154
```
155155

156-
Query data format for arrays:
156+
#### Query data format for arrays
157+
158+
Arrays should be submitted as indexed arrays. Here's how to structure HTML forms and the resulting data:
159+
160+
```html
161+
<!-- HTML Form -->
162+
<form method="post">
163+
<input name="users[0][id]" value="1">
164+
<input name="users[0][name]" value="Jingu">
165+
166+
<input name="users[1][id]" value="2">
167+
<input name="users[1][name]" value="Horikawa">
168+
</form>
169+
170+
```
171+
172+
This will be received as:
157173

158174
```php
159175
$data = [
160176
'users' => [
161-
['id' => '1', 'name' => 'John'],
162-
['id' => '2', 'name' => 'Jane'],
163-
['id' => '3', 'name' => 'Bob']
177+
['id' => '1', 'name' => 'Jingu'],
178+
['id' => '2', 'name' => 'Horikawa']
164179
]
165180
];
166181

167182
$args = $inputQuery->getArguments($method, $data);
168183
// $args[0] will be an array of UserInput objects
169184
```
170185

171-
**ArrayObject Inheritance Support:**
186+
#### Simple array values (e.g., checkboxes)
187+
188+
For simple arrays like checkboxes or multi-select:
189+
190+
```html
191+
<form method="post">
192+
<!-- Checkbox group -->
193+
<input name="hobbies[]" type="checkbox" value="music">
194+
<input name="hobbies[]" type="checkbox" value="sports">
195+
<input name="hobbies[]" type="checkbox" value="reading">
196+
197+
<!-- Multi-select -->
198+
<select name="categories[]" multiple>
199+
<option value="tech">Technology</option>
200+
<option value="business">Business</option>
201+
<option value="lifestyle">Lifestyle</option>
202+
</select>
203+
</form>
204+
```
205+
206+
This will be received as:
207+
208+
```php
209+
$data = [
210+
'hobbies' => ['music', 'sports'], // Only checked values
211+
'categories' => ['tech', 'lifestyle'] // Only selected values
212+
];
213+
214+
// In your controller
215+
public function updatePreferences(
216+
#[Input] array $hobbies, // Simple string array
217+
#[Input] array $categories // Simple string array
218+
) {
219+
// Direct array of strings, no object conversion needed
220+
}
221+
```
222+
223+
**Note**: For non-array parameters, use flat naming without brackets:
224+
```html
225+
<!-- Single object properties -->
226+
<input name="customerName" value="Jingu">
227+
<input name="customerEmail" value="jingu@example.com">
228+
```
229+
230+
#### ArrayObject Inheritance Support
172231

173232
Custom ArrayObject subclasses are also supported:
174233

@@ -214,40 +273,6 @@ All query keys are normalized to camelCase:
214273
- `user-name``userName`
215274
- `UserName``userName`
216275

217-
### Input Format Requirements
218-
219-
Ray.InputQuery expects **flat key-value pairs** as input. Nested array structures are not supported:
220-
221-
```php
222-
// ✅ Correct - Flat structure
223-
$data = [
224-
'customerName' => 'John Doe',
225-
'customerEmail' => 'john@example.com',
226-
'shippingCity' => 'Tokyo'
227-
];
228-
229-
// ❌ Wrong - Nested arrays (e.g., from customer[name] form fields)
230-
$data = [
231-
'customer' => [
232-
'name' => 'John Doe',
233-
'email' => 'john@example.com'
234-
]
235-
];
236-
```
237-
238-
**Why this restriction?** When nested objects are flattened for database operations, all property names must be globally unique to avoid conflicts. This design ensures predictable parameter binding and prevents naming collisions.
239-
For HTML forms, use flat naming:
240-
241-
```html
242-
<!-- ✅ Correct -->
243-
<input name="customerName">
244-
<input name="customerEmail">
245-
246-
<!-- ❌ Avoid -->
247-
<input name="customer[name]">
248-
<input name="customer[email]">
249-
```
250-
251276
## Integration
252277

253278
Ray.InputQuery is designed as a foundation library to be used by:

0 commit comments

Comments
 (0)