Skip to content

Commit c76da07

Browse files
committed
Make methods chainable
Change methods returning void to return $this, so method calls can be chained.
1 parent f9a50b4 commit c76da07

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/Query.php

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,18 @@ public function get(string|int $key): mixed
141141
/**
142142
* @param string $key
143143
* @param string|mixed[] $value
144+
* @return self
144145
* @throws Exception
145146
*/
146-
public function set(string $key, string|array $value): void
147+
public function set(string $key, string|array $value): self
147148
{
148149
$this->initArray();
149150

150151
$this->array[$key] = is_array($value) ? $this->newWithSameSettings($value) : $value;
151152

152153
$this->setDirty();
154+
155+
return $this;
153156
}
154157

155158
/**
@@ -184,7 +187,7 @@ public function isScalar(int|string $key): bool
184187
* @param string|mixed[] $value
185188
* @throws Exception
186189
*/
187-
public function appendTo(string $key, string|array $value): void
190+
public function appendTo(string $key, string|array $value): self
188191
{
189192
$currentValue = $this->get($key);
190193

@@ -195,6 +198,8 @@ public function appendTo(string $key, string|array $value): void
195198
$this->array[$key] = $this->newWithSameSettings($newQueryArray);
196199

197200
$this->setDirty();
201+
202+
return $this;
198203
}
199204

200205
/**
@@ -222,7 +227,7 @@ public function last(?string $key = null): mixed
222227
/**
223228
* @throws Exception
224229
*/
225-
public function remove(string $key): void
230+
public function remove(string $key): self
226231
{
227232
$this->initArray();
228233

@@ -231,12 +236,14 @@ public function remove(string $key): void
231236

232237
$this->setDirty();
233238
}
239+
240+
return $this;
234241
}
235242

236243
/**
237244
* @throws Exception
238245
*/
239-
public function removeValueFrom(string $fromKey, mixed $removeValue): void
246+
public function removeValueFrom(string $fromKey, mixed $removeValue): self
240247
{
241248
$fromKeyValue = $this->get($fromKey);
242249

@@ -255,15 +262,17 @@ public function removeValueFrom(string $fromKey, mixed $removeValue): void
255262
$fromKeyValue->array = array_values($fromKeyValue->array);
256263
}
257264
}
265+
266+
return $this;
258267
}
259268

260269
/**
261270
* @throws Exception
262271
*/
263-
public function boolToInt(): void
272+
public function boolToInt(): self
264273
{
265274
if ($this->boolToInt) {
266-
return;
275+
return $this;
267276
}
268277

269278
$this->boolToInt = true;
@@ -275,15 +284,17 @@ public function boolToInt(): void
275284
}
276285

277286
$this->setDirty();
287+
288+
return $this;
278289
}
279290

280291
/**
281292
* @throws Exception
282293
*/
283-
public function boolToString(): void
294+
public function boolToString(): self
284295
{
285296
if (!$this->boolToInt) {
286-
return;
297+
return $this;
287298
}
288299

289300
$this->boolToInt = false;
@@ -295,15 +306,17 @@ public function boolToString(): void
295306
}
296307

297308
$this->setDirty();
309+
310+
return $this;
298311
}
299312

300313
/**
301314
* @throws Exception
302315
*/
303-
public function separator(string $separator): void
316+
public function separator(string $separator): self
304317
{
305318
if ($this->separator === $separator) {
306-
return;
319+
return $this;
307320
}
308321

309322
$this->separator = $separator;
@@ -315,16 +328,18 @@ public function separator(string $separator): void
315328
}
316329

317330
$this->setDirty();
331+
332+
return $this;
318333
}
319334

320335
/**
321336
* @throws Exception
322337
*/
323-
public function spaceCharacterEncoding(int $spaceCharacterEncodingConst): void
338+
public function spaceCharacterEncoding(int $spaceCharacterEncodingConst): self
324339
{
325340
if (in_array($spaceCharacterEncodingConst, [PHP_QUERY_RFC1738, PHP_QUERY_RFC3986], true)) {
326341
if ($spaceCharacterEncodingConst === $this->spaceCharacterEncoding) {
327-
return;
342+
return $this;
328343
}
329344

330345
$this->spaceCharacterEncoding = $spaceCharacterEncodingConst;
@@ -337,22 +352,24 @@ public function spaceCharacterEncoding(int $spaceCharacterEncodingConst): void
337352

338353
$this->setDirty();
339354
}
355+
356+
return $this;
340357
}
341358

342359
/**
343360
* @throws Exception
344361
*/
345-
public function spaceCharacterPlus(): void
362+
public function spaceCharacterPlus(): self
346363
{
347-
$this->spaceCharacterEncoding(PHP_QUERY_RFC1738);
364+
return $this->spaceCharacterEncoding(PHP_QUERY_RFC1738);
348365
}
349366

350367
/**
351368
* @throws Exception
352369
*/
353-
public function spaceCharacterPercentTwenty(): void
370+
public function spaceCharacterPercentTwenty(): self
354371
{
355-
$this->spaceCharacterEncoding(PHP_QUERY_RFC3986);
372+
return $this->spaceCharacterEncoding(PHP_QUERY_RFC3986);
356373
}
357374

358375
/**

0 commit comments

Comments
 (0)