Skip to content

Commit 781be7e

Browse files
committed
Make container setters method chainable
1 parent aae32eb commit 781be7e

File tree

5 files changed

+73
-25
lines changed

5 files changed

+73
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
66

77
### Added
88

9-
- None
9+
- All containers `Dictionary`, `InnerList`, `OrderedList`, `Parameters` modifying methods are made chainable.
1010

1111
### Fixed
1212

src/Dictionary.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ public function pair(int $index): array
222222
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
223223
* }|bool|int|float|string $member
224224
*/
225-
public function set(string $key, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): void
225+
public function set(string $key, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): self
226226
{
227227
$this->members[self::filterKey($key)] = self::filterMember($member);
228+
229+
return $this;
228230
}
229231

230232
/**
@@ -257,19 +259,23 @@ private static function filterMember(InnerList|Item|ByteSequence|Token|array|boo
257259
/**
258260
* Delete members associated with the list of submitted keys.
259261
*/
260-
public function delete(string ...$keys): void
262+
public function delete(string ...$keys): self
261263
{
262264
foreach ($keys as $key) {
263265
unset($this->members[$key]);
264266
}
267+
268+
return $this;
265269
}
266270

267271
/**
268272
* Remove all members from the instance.
269273
*/
270-
public function clear(): void
274+
public function clear(): self
271275
{
272276
$this->members = [];
277+
278+
return $this;
273279
}
274280

275281
/**
@@ -280,11 +286,13 @@ public function clear(): void
280286
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
281287
* }|bool|int|float|string $member
282288
*/
283-
public function append(string $key, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): void
289+
public function append(string $key, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): self
284290
{
285291
unset($this->members[$key]);
286292

287293
$this->members[self::filterKey($key)] = self::filterMember($member);
294+
295+
return $this;
288296
}
289297

290298
/**
@@ -295,11 +303,13 @@ public function append(string $key, InnerList|Item|ByteSequence|Token|array|bool
295303
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
296304
* }|bool|int|float|string $member
297305
*/
298-
public function prepend(string $key, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): void
306+
public function prepend(string $key, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): self
299307
{
300308
unset($this->members[$key]);
301309

302310
$this->members = [...[self::filterKey($key) => self::filterMember($member)], ...$this->members];
311+
312+
return $this;
303313
}
304314

305315
/**
@@ -310,10 +320,12 @@ public function prepend(string $key, InnerList|Item|ByteSequence|Token|array|boo
310320
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
311321
* }|bool|int|float|string> ...$others
312322
*/
313-
public function merge(iterable ...$others): void
323+
public function merge(iterable ...$others): self
314324
{
315325
foreach ($others as $other) {
316326
$this->members = [...$this->members, ...self::fromAssociative($other)->members];
317327
}
328+
329+
return $this;
318330
}
319331
}

src/InnerList.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,29 @@ public function get(int $index): Item
125125
/**
126126
* Insert members at the beginning of the list.
127127
*/
128-
public function unshift(Item|ByteSequence|Token|bool|int|float|string ...$members): void
128+
public function unshift(Item|ByteSequence|Token|bool|int|float|string ...$members): self
129129
{
130130
$this->members = [...array_map(self::filterMember(...), $members), ...$this->members];
131+
132+
return $this;
131133
}
132134

133135
/**
134136
* Insert members at the end of the list.
135137
*/
136-
public function push(Item|ByteSequence|Token|bool|int|float|string ...$members): void
138+
public function push(Item|ByteSequence|Token|bool|int|float|string ...$members): self
137139
{
138140
$this->members = [...$this->members, ...array_map(self::filterMember(...), $members)];
141+
142+
return $this;
139143
}
140144

141145
/**
142146
* Replace the member associated with the index.
143147
*
144148
* @throws InvalidOffset If the index does not exist
145149
*/
146-
public function insert(int $index, Item|ByteSequence|Token|bool|int|float|string ...$members): void
150+
public function insert(int $index, Item|ByteSequence|Token|bool|int|float|string ...$members): self
147151
{
148152
$offset = $this->filterIndex($index);
149153
match (true) {
@@ -152,21 +156,25 @@ public function insert(int $index, Item|ByteSequence|Token|bool|int|float|string
152156
count($this->members) === $offset => $this->push(...$members),
153157
default => array_splice($this->members, $offset, 0, array_map(self::filterMember(...), $members)),
154158
};
159+
160+
return $this;
155161
}
156162

157-
public function replace(int $index, Item|ByteSequence|Token|bool|int|float|string $member): void
163+
public function replace(int $index, Item|ByteSequence|Token|bool|int|float|string $member): self
158164
{
159165
if (null === ($offset = $this->filterIndex($index))) {
160166
throw InvalidOffset::dueToIndexNotFound($index);
161167
}
162168

163169
$this->members[$offset] = self::filterMember($member);
170+
171+
return $this;
164172
}
165173

166174
/**
167175
* Delete members associated with the list of instance indexes.
168176
*/
169-
public function remove(int ...$indexes): void
177+
public function remove(int ...$indexes): self
170178
{
171179
$offsets = array_filter(
172180
array_map(fn (int $index): int|null => $this->filterIndex($index), $indexes),
@@ -180,13 +188,17 @@ public function remove(int ...$indexes): void
180188
if ([] !== $offsets) {
181189
$this->members = array_values($this->members);
182190
}
191+
192+
return $this;
183193
}
184194

185195
/**
186196
* Remove all members from the instance.
187197
*/
188-
public function clear(): void
198+
public function clear(): self
189199
{
190200
$this->members = [];
201+
202+
return $this;
191203
}
192204
}

src/OrderedList.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,11 @@ public function get(int $index): Item|InnerList
145145
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
146146
* }|bool|int|float|string ...$members
147147
*/
148-
public function unshift(InnerList|Item|ByteSequence|Token|array|bool|int|float|string ...$members): void
148+
public function unshift(InnerList|Item|ByteSequence|Token|array|bool|int|float|string ...$members): self
149149
{
150150
$this->members = [...array_map(self::filterMember(...), $members), ...$this->members];
151+
152+
return $this;
151153
}
152154

153155
/**
@@ -158,9 +160,11 @@ public function unshift(InnerList|Item|ByteSequence|Token|array|bool|int|float|s
158160
* 1:array<string,Item|ByteSequence|Token|bool|int|float|string>
159161
* }|bool|int|float|string ...$members
160162
*/
161-
public function push(InnerList|Item|ByteSequence|Token|array|bool|int|float|string ...$members): void
163+
public function push(InnerList|Item|ByteSequence|Token|array|bool|int|float|string ...$members): self
162164
{
163165
$this->members = [...$this->members, ...array_map(self::filterMember(...), $members)];
166+
167+
return $this;
164168
}
165169

166170
/**
@@ -176,14 +180,16 @@ public function push(InnerList|Item|ByteSequence|Token|array|bool|int|float|stri
176180
public function insert(
177181
int $index,
178182
InnerList|Item|ByteSequence|Token|array|bool|int|float|string ...$members
179-
): void {
183+
): self {
180184
$offset = $this->filterIndex($index);
181185
match (true) {
182186
null === $offset => throw InvalidOffset::dueToIndexNotFound($index),
183187
0 === $offset => $this->unshift(...$members),
184188
count($this->members) === $offset => $this->push(...$members),
185189
default => array_splice($this->members, $offset, 0, array_map(self::filterMember(...), $members)),
186190
};
191+
192+
return $this;
187193
}
188194

189195
/**
@@ -196,19 +202,21 @@ public function insert(
196202
*
197203
* @throws InvalidOffset If the index does not exist
198204
*/
199-
public function replace(int $index, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): void
205+
public function replace(int $index, InnerList|Item|ByteSequence|Token|array|bool|int|float|string $member): self
200206
{
201207
if (null === ($offset = $this->filterIndex($index))) {
202208
throw InvalidOffset::dueToIndexNotFound($index);
203209
}
204210

205211
$this->members[$offset] = self::filterMember($member);
212+
213+
return $this;
206214
}
207215

208216
/**
209217
* Delete members associated with the list of instance indexes.
210218
*/
211-
public function remove(int ...$indexes): void
219+
public function remove(int ...$indexes): self
212220
{
213221
$offsets = array_filter(
214222
array_map(fn (int $index): int|null => $this->filterIndex($index), $indexes),
@@ -222,13 +230,17 @@ public function remove(int ...$indexes): void
222230
if ([] !== $offsets) {
223231
$this->members = array_values($this->members);
224232
}
233+
234+
return $this;
225235
}
226236

227237
/**
228238
* Remove all members from the instance.
229239
*/
230-
public function clear(): void
240+
public function clear(): self
231241
{
232242
$this->members = [];
243+
244+
return $this;
233245
}
234246
}

src/Parameters.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,27 +314,33 @@ public function pair(int $index): array
314314
* @throws SyntaxError If the string key is not a valid
315315
* @throws ForbiddenStateError if the found item is in invalid state
316316
*/
317-
public function set(string $key, Item|ByteSequence|Token|bool|int|float|string $member): void
317+
public function set(string $key, Item|ByteSequence|Token|bool|int|float|string $member): self
318318
{
319319
$this->members[self::filterKey($key)] = self::formatMember($member);
320+
321+
return $this;
320322
}
321323

322324
/**
323325
* Delete members associated with the list of submitted keys.
324326
*/
325-
public function delete(string ...$keys): void
327+
public function delete(string ...$keys): self
326328
{
327329
foreach ($keys as $key) {
328330
unset($this->members[$key]);
329331
}
332+
333+
return $this;
330334
}
331335

332336
/**
333337
* Remove all members from the instance.
334338
*/
335-
public function clear(): void
339+
public function clear(): self
336340
{
337341
$this->members = [];
342+
343+
return $this;
338344
}
339345

340346
/**
@@ -343,11 +349,13 @@ public function clear(): void
343349
* @throws SyntaxError If the string key is not a valid
344350
* @throws ForbiddenStateError if the found item is in invalid state
345351
*/
346-
public function append(string $key, Item|ByteSequence|Token|bool|int|float|string $member): void
352+
public function append(string $key, Item|ByteSequence|Token|bool|int|float|string $member): self
347353
{
348354
unset($this->members[$key]);
349355

350356
$this->members[self::filterKey($key)] = self::formatMember($member);
357+
358+
return $this;
351359
}
352360

353361
/**
@@ -356,11 +364,13 @@ public function append(string $key, Item|ByteSequence|Token|bool|int|float|strin
356364
* @throws SyntaxError If the string key is not a valid
357365
* @throws ForbiddenStateError if the found item is in invalid state
358366
*/
359-
public function prepend(string $key, Item|ByteSequence|Token|bool|int|float|string $member): void
367+
public function prepend(string $key, Item|ByteSequence|Token|bool|int|float|string $member): self
360368
{
361369
unset($this->members[$key]);
362370

363371
$this->members = [...[self::filterKey($key) => self::formatMember($member)], ...$this->members];
372+
373+
return $this;
364374
}
365375

366376
/**
@@ -369,10 +379,12 @@ public function prepend(string $key, Item|ByteSequence|Token|bool|int|float|stri
369379
* @param iterable<array-key, Item|Token|ByteSequence|float|int|bool|string> ...$others
370380
* @throws ForbiddenStateError if the found item is in invalid state
371381
*/
372-
public function merge(iterable ...$others): void
382+
public function merge(iterable ...$others): self
373383
{
374384
foreach ($others as $other) {
375385
$this->members = [...$this->members, ...self::fromAssociative($other)->members];
376386
}
387+
388+
return $this;
377389
}
378390
}

0 commit comments

Comments
 (0)