Skip to content

Commit

Permalink
Merge pull request #277 from nbayramberdiyev/improve-header
Browse files Browse the repository at this point in the history
Improve `Header` by using PHP native types 🐘
  • Loading branch information
l0gicgate authored Mar 7, 2023
2 parents bd76383 + 7c83e37 commit a6f0cae
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 52 deletions.
35 changes: 1 addition & 34 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

namespace Slim\Psr7;

use InvalidArgumentException;

use function array_merge;
use function is_array;
use function is_string;

class Header
Expand All @@ -24,71 +21,41 @@ class Header

private array $values;

/**
* Header constructor.
*
* @param string $originalName
* @param string $normalizedName
* @param array $values
*/
public function __construct(string $originalName, string $normalizedName, array $values)
{
$this->originalName = $originalName;
$this->normalizedName = $normalizedName;
$this->values = $values;
}

/**
* @return string
*/
public function getOriginalName(): string
{
return $this->originalName;
}

/**
* @return string
*/
public function getNormalizedName(): string
{
return $this->normalizedName;
}

/**
* @param string $value
*
* @return self
*/
public function addValue(string $value): self
{
$this->values[] = $value;

return $this;
}

/**
* @param array|string $values
*
* @return self
*/
public function addValues($values): self
public function addValues(array|string $values): self
{
if (is_string($values)) {
return $this->addValue($values);
}

if (!is_array($values)) {
throw new InvalidArgumentException('Parameter 1 of Header::addValues() should be a string or an array.');
}

$this->values = array_merge($this->values, $values);

return $this;
}

/**
* @return array
*/
public function getValues(): array
{
return $this->values;
Expand Down
5 changes: 1 addition & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@

class Request extends Message implements ServerRequestInterface
{
/**
* @var string
*/
protected $method;
protected string $method;

/**
* @var UriInterface
Expand Down
18 changes: 4 additions & 14 deletions tests/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Slim\Tests\Psr7;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Slim\Psr7\Header;

Expand All @@ -30,19 +29,19 @@ protected function headerFactory(): Header
return new Header($originalName, $normalizedName, $values);
}

public function testGetOriginalName()
public function testGetOriginalName(): void
{
$header = $this->headerFactory();
$this->assertEquals('ACCEPT', $header->getOriginalName());
}

public function testGetNormalizedName()
public function testGetNormalizedName(): void
{
$header = $this->headerFactory();
$this->assertEquals('accept', $header->getNormalizedName());
}

public function testAddValue()
public function testAddValue(): void
{
$header = $this->headerFactory();
$header2 = $header->addValue('text/html');
Expand All @@ -51,21 +50,12 @@ public function testAddValue()
$this->assertSame($header2, $header);
}

public function testAddValuesString()
public function testAddValuesString(): void
{
$header = $this->headerFactory();
$header2 = $header->addValues('text/html');

$this->assertEquals(['application/json', 'text/html'], $header->getValues());
$this->assertSame($header2, $header);
}

public function testAddValuesNull()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Parameter 1 of Header::addValues() should be a string or an array.');

$header = $this->headerFactory();
$header->addValues(null);
}
}

0 comments on commit a6f0cae

Please sign in to comment.