Skip to content

Commit f612077

Browse files
committed
docs: document writing values in the method reference
displaying-fields told readers to see the Method Reference for writes and the Method Reference had nothing on them. Adds a Writing Values section covering assignment, arrays becoming nested collections, Smart values unwrapping into the target's mode, and unset(), plus the note that row positions are stamped at build time. Leaves out $rows[] = [...], which is deprecated in favour of an explicit key. The cross-reference now deep-links to the section.
1 parent 503f351 commit f612077

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

docs/displaying-fields.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ echo $row->{0}; // zero (numeric keys)
167167
```
168168

169169
SmartArrays are writable too, with the same syntax in reverse:
170-
`$user->status = 'Active'`. Template code rarely needs it; see the
171-
[Method Reference](method-reference.md) when you do.
170+
`$user->status = 'Active'`. Template code rarely needs it; see
171+
[Writing Values](method-reference.md#writing-values) when you do.
172172

173173
---
174174

docs/method-reference.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ and objects are always truthy in PHP, so test for missing values with
5050
| `->last()` | Returns the last element |
5151
| `->at($index)` | Returns the element at a position, ignoring keys: 0 is first, negatives count from the end |
5252

53+
### Writing Values
54+
55+
Collections are writable with the same syntax in reverse. A plain value is
56+
stored as-is, an array becomes a nested collection, and a Smart value from
57+
another collection unwraps and re-wraps for this one's mode. Reading a field
58+
back gives you a field, so output still encodes.
59+
60+
```php
61+
$user->status = 'Active'; // sets a field
62+
$user->tags = ['staff', 'admin']; // stored as a nested collection
63+
unset($user->nickname); // removes the key
64+
65+
echo $user->status; // Active
66+
```
67+
68+
Row positions are set when a collection is built, so `isFirst()`, `isLast()`,
69+
and `position()` don't change when you add or remove rows afterward.
70+
5371
### [Collection Checks](displaying-fields.md#showing-a-no-results-message)
5472

5573
*These return plain values, typically used in if statements.*

tests/Integration/DocsExamplesTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,21 @@ public function testMethodReferenceGetRawValueUnwrapsAnythingYouHandIt(): void
10191019
$this->assertSame('plain', SmartArray::getRawValue('plain'));
10201020
}
10211021

1022+
/** The Writing Values block: assignment, nested arrays, unset. */
1023+
public function testMethodReferenceWritingValuesStoresFieldsRowsAndRemovesKeys(): void
1024+
{
1025+
$user = SmartArrayHtml::new(['status' => 'Pending', 'nickname' => 'Jay']);
1026+
1027+
$user->status = 'Active';
1028+
$user->tags = ['staff', 'admin'];
1029+
unset($user->nickname);
1030+
1031+
$this->assertSame('Active', (string)$user->status);
1032+
$this->assertInstanceOf(SmartArrayHtml::class, $user->tags);
1033+
$this->assertSame(['staff', 'admin'], $user->tags->toArray());
1034+
$this->assertSame(['status' => 'Active', 'tags' => ['staff', 'admin']], $user->toArray());
1035+
}
1036+
10221037
//endregion
10231038
//region Troubleshooting: empty() and if() checks
10241039

0 commit comments

Comments
 (0)