Skip to content

Commit

Permalink
Add locale-independent lowercasing
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas authored and Nyholm committed Apr 21, 2020
1 parent 47d6826 commit 89ca021
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/LowercaseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Nyholm\Psr7;

/**
* Trait implementing a locale-independent lowercasing logic.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal should not be used outside of Nyholm/Psr7 as it does not fall under our BC promise
*/
trait LowercaseTrait
{
private static function lowercase(string $value): string
{
return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
}
}
12 changes: 7 additions & 5 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
trait MessageTrait
{
use LowercaseTrait;

/** @var array Map of all registered headers, as original name => array of values */
private $headers = [];

Expand Down Expand Up @@ -53,12 +55,12 @@ public function getHeaders(): array

public function hasHeader($header): bool
{
return isset($this->headerNames[\strtolower($header)]);
return isset($this->headerNames[self::lowercase($header)]);
}

public function getHeader($header): array
{
$header = \strtolower($header);
$header = self::lowercase($header);
if (!isset($this->headerNames[$header])) {
return [];
}
Expand All @@ -76,7 +78,7 @@ public function getHeaderLine($header): string
public function withHeader($header, $value): self
{
$value = $this->validateAndTrimHeader($header, $value);
$normalized = \strtolower($header);
$normalized = self::lowercase($header);

$new = clone $this;
if (isset($new->headerNames[$normalized])) {
Expand All @@ -102,7 +104,7 @@ public function withAddedHeader($header, $value): self

public function withoutHeader($header): self
{
$normalized = \strtolower($header);
$normalized = self::lowercase($header);
if (!isset($this->headerNames[$normalized])) {
return $this;
}
Expand Down Expand Up @@ -139,7 +141,7 @@ private function setHeaders(array $headers): void
{
foreach ($headers as $header => $value) {
$value = $this->validateAndTrimHeader($header, $value);
$normalized = \strtolower($header);
$normalized = self::lowercase($header);
if (isset($this->headerNames[$normalized])) {
$header = $this->headerNames[$normalized];
$this->headers[$header] = \array_merge($this->headers[$header], $value);
Expand Down
10 changes: 6 additions & 4 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
final class Uri implements UriInterface
{
use LowercaseTrait;

private const SCHEMES = ['http' => 80, 'https' => 443];

private const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~';
Expand Down Expand Up @@ -52,9 +54,9 @@ public function __construct(string $uri = '')
}

// Apply parse_url parts to a URI.
$this->scheme = isset($parts['scheme']) ? \strtolower($parts['scheme']) : '';
$this->scheme = isset($parts['scheme']) ? self::lowercase($parts['scheme']) : '';
$this->userInfo = $parts['user'] ?? '';
$this->host = isset($parts['host']) ? \strtolower($parts['host']) : '';
$this->host = isset($parts['host']) ? self::lowercase($parts['host']) : '';
$this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null;
$this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : '';
$this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : '';
Expand Down Expand Up @@ -129,7 +131,7 @@ public function withScheme($scheme): self
throw new \InvalidArgumentException('Scheme must be a string');
}

if ($this->scheme === $scheme = \strtolower($scheme)) {
if ($this->scheme === $scheme = self::lowercase($scheme)) {
return $this;
}

Expand Down Expand Up @@ -163,7 +165,7 @@ public function withHost($host): self
throw new \InvalidArgumentException('Host must be a string');
}

if ($this->host === $host = \strtolower($host)) {
if ($this->host === $host = self::lowercase($host)) {
return $this;
}

Expand Down

0 comments on commit 89ca021

Please # to comment.