Skip to content

Commit

Permalink
close #111, close #112
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Mar 8, 2023
1 parent 5ab6cc1 commit 8e5925a
Show file tree
Hide file tree
Showing 26 changed files with 743 additions and 417 deletions.
120 changes: 6 additions & 114 deletions src/Controller/HttpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,120 +13,12 @@

namespace Chevere\Controller;

use Chevere\Controller\Interfaces\HttpControllerInterface;
use Chevere\Controller\Interfaces\HttpMiddlewareInterface;
use Chevere\Parameter\Arguments;
use function Chevere\Parameter\arrayParameter;
use Chevere\Parameter\Interfaces\ArrayParameterInterface;
use Chevere\Parameter\Interfaces\FileParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use function Chevere\Parameter\parameters;
use Chevere\HttpController\HttpController as NewHttpController;

abstract class HttpController extends Controller implements HttpControllerInterface
/**
* @deprecated use Chevere\HttpController\HttpController instead.
* This will be removed in 3.0.0
*/
abstract class HttpController extends NewHttpController
{
/**
* @var array<int|string, string>
*/
protected array $get = [];

/**
* @var array<int|string, string>
*/
protected array $post = [];

/**
* @var array<int|string, array<string, int|string>>
*/
protected array $files = [];

protected HttpMiddlewareInterface $middleware;

public function acceptGet(): ParametersInterface
{
return parameters();
}

public function acceptPost(): ParametersInterface
{
return parameters();
}

public function acceptFiles(): ArrayParameterInterface
{
return arrayParameter();
}

final public function withGet(array $get): static
{
$new = clone $this;
$arguments = new Arguments(
$new->acceptGet(),
...$get
);
/** @var array<int|string, string> */
$array = $arguments->toArray();
$new->get = $array;

return $new;
}

final public function withPost(array $post): static
{
$new = clone $this;
$arguments = new Arguments(
$new->acceptPost(),
...$post
);
/** @var array<int|string, string> */
$array = $arguments->toArray();
$new->post = $array;

return $new;
}

final public function withFiles(array $files): static
{
$new = clone $this;
$array = [];
/** @var FileParameterInterface $parameter */
foreach ($new->acceptFiles()->parameters() as $key => $parameter) {
$arguments = new Arguments(
$parameter->parameters(),
...$files[$key]
);
/** @var array<int|string, array<string, int|string>> $array */
$array[$key] = $arguments->toArray();
}
$new->files = $array;

return $new;
}

public function withMiddleware(HttpMiddlewareInterface $middleware): static
{
$new = clone $this;
$new->middleware = $middleware;

return $new;
}

public function middleware(): HttpMiddlewareInterface
{
return $this->middleware ??= new HttpMiddleware();
}

final public function get(): array
{
return $this->get;
}

final public function post(): array
{
return $this->post;
}

final public function files(): array
{
return $this->files;
}
}
8 changes: 6 additions & 2 deletions src/Controller/HttpMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@

namespace Chevere\Controller;

use Chevere\Controller\Interfaces\HttpMiddlewareInterface;
use Chevere\DataStructure\Interfaces\VectorInterface;
use Chevere\DataStructure\Traits\VectorTrait;
use Chevere\DataStructure\Vector;
use Chevere\Http\Interfaces\MiddlewaresInterface;
use Psr\Http\Server\MiddlewareInterface;

final class HttpMiddleware implements HttpMiddlewareInterface
/**
* @deprecated use Chevere\HttpController\HttpMiddleware instead.
* This will be removed in 3.0.0
*/
final class HttpMiddleware implements MiddlewaresInterface
{
use VectorTrait;

Expand Down
88 changes: 6 additions & 82 deletions src/Controller/HttpRedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,88 +13,12 @@

namespace Chevere\Controller;

use Chevere\Controller\Interfaces\HttpRedirectControllerInterface;
use function Chevere\Message\message;
use function Chevere\Parameter\integerParameter;
use Chevere\Parameter\Interfaces\ParametersInterface;
use function Chevere\Parameter\objectParameter;
use function Chevere\Parameter\parameters;
use Chevere\Throwable\Exceptions\InvalidArgumentException;
use Chevere\Throwable\Exceptions\LogicException;
use Psr\Http\Message\UriInterface;
use Chevere\HttpController\HttpRedirectController as NewHttpRedirectController;

abstract class HttpRedirectController extends HttpController implements HttpRedirectControllerInterface
/**
* @deprecated use Chevere\HttpController\HttpRedirectController instead.
* This will be removed in 3.0.0
*/
abstract class HttpRedirectController extends NewHttpRedirectController
{
private ?UriInterface $uri;

private int $status = 302;

public function getResponseParameters(): ParametersInterface
{
return parameters(
uri: objectParameter(UriInterface::class),
status: integerParameter()
->withAccept(...static::STATUSES),
);
}

final public function withUri(UriInterface $uri): static
{
$new = clone $this;
$new->setUri($uri);

return $new;
}

final public function withStatus(int $status): static
{
$new = clone $this;
$new->setStatus($status);

return $new;
}

final public function uri(): UriInterface
{
return $this->uri
?? throw new LogicException(message('No uri set'));
}

final public function status(): int
{
return $this->status;
}

/**
* @return array<string, mixed>
*/
protected function data(): array
{
return [
'uri' => $this->uri(),
'status' => $this->status(),
];
}

final protected function assertStatus(): void
{
if (! in_array($this->status, static::STATUSES, true)) {
throw new InvalidArgumentException(
message('Invalid status code %status% provided, must be one of %statuses%')
->withCode('%status%', strval($this->status))
->withCode('%statuses%', implode(', ', static::STATUSES))
);
}
}

final protected function setUri(UriInterface $uri): void
{
$this->uri = $uri;
}

final protected function setStatus(int $status): void
{
$this->status = $status;
$this->assertStatus();
}
}
16 changes: 7 additions & 9 deletions src/Controller/HttpRedirectStaticController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@

namespace Chevere\Controller;

final class HttpRedirectStaticController extends HttpRedirectController
use Chevere\HttpController\HttpRedirectStaticController as NewHttpRedirectStaticController;

/**
* @deprecated use Chevere\HttpController\HttpRedirectStaticController instead.
* This will be removed in 3.0.0
*/
final class HttpRedirectStaticController extends NewHttpRedirectStaticController
{
/**
* @return array<string, mixed>
* @codeCoverageIgnore
*/
public function run(): array
{
return $this->data();
}
}
75 changes: 4 additions & 71 deletions src/Controller/Interfaces/HttpControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,79 +13,12 @@

namespace Chevere\Controller\Interfaces;

use Chevere\Parameter\Interfaces\ArrayParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\HttpController\Interfaces\HttpControllerInterface as NewHttpControllerInterface;

/**
* Describes the component in charge of defining an Http Controller which adds methods for handling HTTP requests.
* @deprecated Use Chevere\HttpController\Interfaces\HttpControllerInterface instead.
* This will be removed in 3.0.0
*/
interface HttpControllerInterface extends ControllerInterface
interface HttpControllerInterface extends NewHttpControllerInterface
{
/**
* @var int
*/
public const STATUS_SUCCESS = 200;

/**
* @var int
*/
public const STATUS_ERROR = 500;

/**
* Defines the GET parameters accepted.
*/
public function acceptGet(): ParametersInterface;

/**
* Defines the POST parameters accepted.
*/
public function acceptPost(): ParametersInterface;

/**
* Defines the FILES parameters accepted.
*/
public function acceptFiles(): ArrayParameterInterface;

/**
* @param array<int|string, string> $get
*/
public function withGet(array $get): static;

/**
* @param array<int|string, string> $post
*/
public function withPost(array $post): static;

/**
* @param array<int|string, array<string, int|string>> $files
*/
public function withFiles(array $files): static;

/**
* Return an instance with the specified middleware.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified middleware.
*/
public function withMiddleware(HttpMiddlewareInterface $middleware): static;

/**
* Provides access to the controller middleware.
*/
public function middleware(): HttpMiddlewareInterface;

/**
* @return array<int|string, string>
*/
public function get(): array;

/**
* @return array<int|string, string>
*/
public function post(): array;

/**
* @return array<int|string, array<string, int|string>>
*/
public function files(): array;
}
33 changes: 4 additions & 29 deletions src/Controller/Interfaces/HttpMiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,12 @@

namespace Chevere\Controller\Interfaces;

use Countable;
use Iterator;
use Psr\Http\Server\MiddlewareInterface;
use Chevere\Http\Interfaces\MiddlewaresInterface;

/**
* Describes the component in charge of collecting middleware.
* @deprecated Use Chevere\Http\Interfaces\MiddlewaresInterface instead.
* This will be removed in 3.0.0
*/
interface HttpMiddlewareInterface extends Countable
interface HttpMiddlewareInterface extends MiddlewaresInterface
{
/**
* Return an instance with the specified appended `$middleware`.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified appended `$middleware`.
*
* This middleware will be added to the end of the middleware stack.
*/
public function withAppend(MiddlewareInterface ...$middleware): self;

/**
* Return an instance with the specified prepend `$middleware`.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified prepend `$middleware`.
*
* This middleware will be added to the beginning of the stack.
*/
public function withPrepend(MiddlewareInterface ...$middleware): self;

/**
* @return Iterator<int, MiddlewareInterface>
*/
public function getIterator(): Iterator;
}

0 comments on commit 8e5925a

Please sign in to comment.