Skip to content

Commit

Permalink
Merge pull request #6 from nimbly/3.0
Browse files Browse the repository at this point in the history
Adding HttpMethod string backed enum for HTTP methods.
  • Loading branch information
brentscheffler committed Sep 21, 2024
2 parents 997595c + 9f46a6f commit 91a0546
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 nimbly.io
Copyright (c) 2024 nimbly.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 18 additions & 0 deletions src/HttpMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Nimbly\Capsule;

enum HttpMethod: string
{
case GET = "GET";
case POST = "POST";
case PUT = "PUT";
case PATCH = "PATCH";
case DELETE = "DELETE";

case OPTIONS = "OPTIONS";
case HEAD = "HEAD";

case CONNECT = "CONNECT";
case TRACE = "TRACE";
}
16 changes: 8 additions & 8 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Request extends MessageAbstract implements RequestInterface
/**
* HTTP method
*
* @var string
* @var HttpMethod
*/
protected string $method;
protected HttpMethod $method;

/**
* Request URI
Expand All @@ -36,20 +36,20 @@ class Request extends MessageAbstract implements RequestInterface
protected ?string $requestTarget = null;

/**
* @param string $method The HTTP method to use for the request. For example, "POST", "GET", etc.
* @param string|HttpMethod $method The HTTP method to use for the request. For example, "POST", "GET", etc.
* @param string|UriInterface $uri The URI of the resource you are trying to call. For example: "https://api.example.com/books/12345"
* @param string|StreamInterface|null $body The body of the request. If request does not contain a body, you can use a null or empty string value.
* @param array<string,string> $headers An array of key & value pairs for headers to be included in the request. For example, ["Content-Type" => "application/json"]
* @param string $httpVersion The HTTP protocol version to use for this request. Defaults to "1.1".
*/
public function __construct(
string $method,
string|HttpMethod $method,
string|UriInterface $uri,
string|StreamInterface|null $body = null,
array $headers = [],
string $httpVersion = "1.1")
{
$this->method = \strtoupper($method);
$this->method = \is_string($method) ? HttpMethod::from(\strtoupper($method)) : $method;
$this->uri = $uri instanceof UriInterface ? $uri : UriFactory::createFromString($uri);
$this->body = $body instanceof StreamInterface ? $body : StreamFactory::createFromString((string) $body);

Expand All @@ -64,13 +64,13 @@ public function __construct(

/**
* @inheritDoc
* @param string $method
* @param string|HttpMethod $method
* @return static
*/
public function withMethod($method): static
{
$instance = clone $this;
$instance->method = \strtoupper($method);
$instance->method = \is_string($method) ? HttpMethod::from(\strtoupper($method)) : $method;
return $instance;
}

Expand All @@ -79,7 +79,7 @@ public function withMethod($method): static
*/
public function getMethod(): string
{
return $this->method;
return $this->method->value;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ServerRequest extends Request implements ServerRequestInterface
protected array $serverParams = [];

/**
* @param string $method The HTTP method of the request. For example, "POST", "GET", etc.
* @param string|HttpMethod $method The HTTP method of the request. For example, "POST", "GET", etc.
* @param string|UriInterface $uri The URI of the resource to be called. For example: "https://api.example.com/books/12345"
* @param string|StreamInterface $body The body of the request. If request does not contain a body, you can use a null or empty string value.
* @param array<string,mixed> $query An array of key & value pairs for the query params. For example: ["q" => "red socks", "p" => 2]
Expand All @@ -71,7 +71,7 @@ class ServerRequest extends Request implements ServerRequestInterface
* @param string $version The HTTP protocol version used for this request. Defaults to "1.1".
*/
public function __construct(
string $method,
string|HttpMethod $method,
string|UriInterface $uri,
string|StreamInterface|null $body = null,
array $query = [],
Expand Down
15 changes: 15 additions & 0 deletions tests/StreamFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@ public function test_create_stream_from_resource(): void
$this->assertInstanceOf(ResourceStream::class, $stream);
$this->assertEquals("test_create_stream_from_resource", $stream->getContents());
}

public function test_create_from_resource(): void
{
if( !\is_dir(__DIR__ . "/tmp") ){
\mkdir(__DIR__ . "/tmp");
}

\file_put_contents(__DIR__ . "/tmp/tmp_file", "test_create_stream_from_resource");

$streamFactory = new StreamFactory;
$stream = $streamFactory::createFromResource(\fopen(__DIR__ . "/tmp/tmp_file", "r"));

$this->assertInstanceOf(ResourceStream::class, $stream);
$this->assertEquals("test_create_stream_from_resource", $stream->getContents());
}
}

0 comments on commit 91a0546

Please sign in to comment.