Skip to content

Commit

Permalink
Prepare for immutability
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Nov 9, 2020
1 parent 3528bf2 commit 7f4d354
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 15 deletions.
58 changes: 50 additions & 8 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ public function getExpires() : int
}

/**
* @deprecated use withExpires()
*
* Sets the cookie expiry time.
*
* @param int $expires The unix timestamp at which the cookie expires, zero for a transient cookie.
Expand All @@ -187,6 +189,14 @@ public function setExpires(int $expires) : Cookie
return $this;
}

public function withExpires(int $expires): Cookie
{
$that = clone $this;
$that->expires = $expires;

return $that;
}

/**
* @return string|null
*/
Expand All @@ -196,21 +206,27 @@ public function getPath() : ?string
}

/**
* @deprecated use withPath()
*
* @param string|null $path
*
* @return static This cookie.
*/
public function setPath(?string $path) : Cookie
{
if ($path !== null) {
$path = (string) $path;
}

$this->path = $path;

return $this;
}

public function withPath(?string $path): Cookie
{
$that = clone $this;
$that->path = $path;

return $that;
}

/**
* @return string|null
*/
Expand All @@ -220,21 +236,27 @@ public function getDomain() : ?string
}

/**
* @deprecated use withDomain()
*
* @param string|null $domain
*
* @return static This cookie.
*/
public function setDomain(?string $domain) : Cookie
{
if ($domain !== null) {
$domain = (string) $domain;
}

$this->domain = $domain;

return $this;
}

public function withDomain(?string $domain): Cookie
{
$that = clone $this;
$that->domain = $domain;

return $that;
}

/**
* @return bool
*/
Expand All @@ -252,6 +274,8 @@ public function isSecure() : bool
}

/**
* @deprecated use withSecure()
*
* Sets whether this cookie should only be sent over a secure connection.
*
* @param bool $secure True to only send over a secure connection, false otherwise.
Expand All @@ -265,6 +289,14 @@ public function setSecure(bool $secure) : Cookie
return $this;
}

public function withSecure(bool $secure): Cookie
{
$that = clone $this;
$that->secure = $secure;

return $that;
}

/**
* Returns whether to limit the scope of this cookie to HTTP requests.
*
Expand All @@ -276,6 +308,8 @@ public function isHttpOnly() : bool
}

/**
* @deprecated use withHttpOnly()
*
* Sets whether to limit the scope of this cookie to HTTP requests.
*
* Set to true to instruct the user agent to omit the cookie when providing access to
Expand All @@ -295,6 +329,14 @@ public function setHttpOnly(bool $httpOnly) : Cookie
return $this;
}

public function withHttpOnly(bool $httpOnly): Cookie
{
$that = clone $this;
$that->httpOnly = $httpOnly;

return $that;
}

/**
* Returns whether this cookie has expired.
*
Expand Down
122 changes: 116 additions & 6 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function getProtocolVersion() : string
}

/**
* @deprecated use withProtocolVersion()
*
* @param string $version
*
* @return static
Expand All @@ -53,6 +55,17 @@ public function setProtocolVersion(string $version) : Message
return $this;
}

/**
* @return static
*/
public function withProtocolVersion(string $version): Message
{
$that = clone $this;
$that->protocolVersion = $version;

return $that;
}

/**
* Gets all message headers.
*
Expand Down Expand Up @@ -148,12 +161,14 @@ public function getHeaderAsArray(string $name) : array
}

/**
* @deprecated use withHeader()
*
* Sets a header, replacing any existing values of any headers with the same case-insensitive name.
*
* The header value MUST be a string or an array of strings.
*
* @param string $name
* @param string|array $value
* @param string $name
* @param string|string[] $value
*
* @return static
*/
Expand All @@ -166,12 +181,29 @@ public function setHeader(string $name, $value) : Message
}

/**
* @param string|string[] $value
*
* @return static
*/
public function withHeader(string $name, $value): Message
{
$that = clone $this;

$name = strtolower($name);
$that->headers[$name] = is_array($value) ? array_values($value) : [$value];

return $that;
}

/**
* @deprecated use withHeaders()
*
* Sets headers, replacing any headers that have already been set on the message.
*
* The array keys MUST be a string. The array values must be either a
* string or an array of strings.
*
* @param array $headers
* @param array<string, string|string[]> $headers
*
* @return static
*/
Expand All @@ -185,10 +217,28 @@ public function setHeaders(array $headers) : Message
}

/**
* @param array<string, string|string[]> $headers
*
* @return static
*/
public function withHeaders(array $headers): Message
{
$that = $this;

foreach ($headers as $name => $value) {
$that = $that->withHeader($name, $value);
}

return $that;
}

/**
* @deprecated use withAddedHeader()
*
* Appends a header value to any existing values associated with the given header name.
*
* @param string $name
* @param string|array $value
* @param string $name
* @param string|string[] $value
*
* @return static
*/
Expand All @@ -209,6 +259,21 @@ public function addHeader(string $name, $value) : Message
}

/**
* @param string|string[] $value
*
* @return static
*/
public function withAddedHeader(string $name, $value): Message
{
$that = clone $this;
$that->addHeader($name, $value);

return $that;
}

/**
* @deprecated use withAddedHeaders()
*
* Merges in an associative array of headers.
*
* Each array key MUST be a string representing the case-insensitive name
Expand All @@ -217,7 +282,7 @@ public function addHeader(string $name, $value) : Message
* name, or, if a header does not already exist by the given name, then the
* header is added.
*
* @param array $headers
* @param array<string, string|string[]> $headers
*
* @return static
*/
Expand All @@ -231,6 +296,24 @@ public function addHeaders(array $headers) : Message
}

/**
* @param array<string, string|string[]> $headers
*
* @return static
*/
public function withAddedHeaders(array $headers): Message
{
$that = $this;

foreach ($headers as $name => $value) {
$that = $that->withAddedHeader($name, $value);
}

return $that;
}

/**
* @deprecated use withoutHeader()
*
* Removes a specific header by case-insensitive name.
*
* @param string $name
Expand All @@ -245,6 +328,19 @@ public function removeHeader(string $name) : Message
return $this;
}

/**
* @return static
*/
public function withoutHeader(string $name) : Message
{
$that = clone $this;

$name = strtolower($name);
unset($that->headers[$name]);

return $that;
}

/**
* @return string
*/
Expand Down Expand Up @@ -272,6 +368,8 @@ public function getBody() : ?MessageBody
}

/**
* @deprecated use withBody()
*
* Sets the message body.
*
* @param MessageBody|null $body
Expand Down Expand Up @@ -300,6 +398,18 @@ public function setBody(?MessageBody $body) : Message
return $this;
}

/**
* @return static
*/
public function withBody(?MessageBody $body): Message
{
$that = clone $this;

$that->setBody($body);

return $that;
}

/**
* Returns the reported Content-Length of this Message.
*
Expand Down
Loading

0 comments on commit 7f4d354

Please sign in to comment.