Skip to content

Commit

Permalink
Make methods chainable
Browse files Browse the repository at this point in the history
Change methods returning void to return $this, so method calls can be
chained.
  • Loading branch information
otsch committed Jun 1, 2022
1 parent f9a50b4 commit c76da07
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,18 @@ public function get(string|int $key): mixed
/**
* @param string $key
* @param string|mixed[] $value
* @return self
* @throws Exception
*/
public function set(string $key, string|array $value): void
public function set(string $key, string|array $value): self
{
$this->initArray();

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

$this->setDirty();

return $this;
}

/**
Expand Down Expand Up @@ -184,7 +187,7 @@ public function isScalar(int|string $key): bool
* @param string|mixed[] $value
* @throws Exception
*/
public function appendTo(string $key, string|array $value): void
public function appendTo(string $key, string|array $value): self
{
$currentValue = $this->get($key);

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

$this->setDirty();

return $this;
}

/**
Expand Down Expand Up @@ -222,7 +227,7 @@ public function last(?string $key = null): mixed
/**
* @throws Exception
*/
public function remove(string $key): void
public function remove(string $key): self
{
$this->initArray();

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

$this->setDirty();
}

return $this;
}

/**
* @throws Exception
*/
public function removeValueFrom(string $fromKey, mixed $removeValue): void
public function removeValueFrom(string $fromKey, mixed $removeValue): self
{
$fromKeyValue = $this->get($fromKey);

Expand All @@ -255,15 +262,17 @@ public function removeValueFrom(string $fromKey, mixed $removeValue): void
$fromKeyValue->array = array_values($fromKeyValue->array);
}
}

return $this;
}

/**
* @throws Exception
*/
public function boolToInt(): void
public function boolToInt(): self
{
if ($this->boolToInt) {
return;
return $this;
}

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

$this->setDirty();

return $this;
}

/**
* @throws Exception
*/
public function boolToString(): void
public function boolToString(): self
{
if (!$this->boolToInt) {
return;
return $this;
}

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

$this->setDirty();

return $this;
}

/**
* @throws Exception
*/
public function separator(string $separator): void
public function separator(string $separator): self
{
if ($this->separator === $separator) {
return;
return $this;
}

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

$this->setDirty();

return $this;
}

/**
* @throws Exception
*/
public function spaceCharacterEncoding(int $spaceCharacterEncodingConst): void
public function spaceCharacterEncoding(int $spaceCharacterEncodingConst): self
{
if (in_array($spaceCharacterEncodingConst, [PHP_QUERY_RFC1738, PHP_QUERY_RFC3986], true)) {
if ($spaceCharacterEncodingConst === $this->spaceCharacterEncoding) {
return;
return $this;
}

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

$this->setDirty();
}

return $this;
}

/**
* @throws Exception
*/
public function spaceCharacterPlus(): void
public function spaceCharacterPlus(): self
{
$this->spaceCharacterEncoding(PHP_QUERY_RFC1738);
return $this->spaceCharacterEncoding(PHP_QUERY_RFC1738);
}

/**
* @throws Exception
*/
public function spaceCharacterPercentTwenty(): void
public function spaceCharacterPercentTwenty(): self
{
$this->spaceCharacterEncoding(PHP_QUERY_RFC3986);
return $this->spaceCharacterEncoding(PHP_QUERY_RFC3986);
}

/**
Expand Down

0 comments on commit c76da07

Please sign in to comment.