diff options
Diffstat (limited to 'src/Http')
-rw-r--r-- | src/Http/Exceptions/HttpNotFound.php | 23 | ||||
-rw-r--r-- | src/Http/Response.php | 27 | ||||
-rw-r--r-- | src/Http/Validation/Rules/Between.php | 10 | ||||
-rw-r--r-- | src/Http/Validation/Rules/Max.php | 10 | ||||
-rw-r--r-- | src/Http/Validation/Rules/Min.php | 10 | ||||
-rw-r--r-- | src/Http/Validation/Rules/StringInputLength.php | 44 |
6 files changed, 116 insertions, 8 deletions
diff --git a/src/Http/Exceptions/HttpNotFound.php b/src/Http/Exceptions/HttpNotFound.php new file mode 100644 index 00000000..324adaf9 --- /dev/null +++ b/src/Http/Exceptions/HttpNotFound.php @@ -0,0 +1,23 @@ +<?php + +namespace Engelsystem\Http\Exceptions; + +use Throwable; + +class HttpNotFound extends HttpException +{ + /** + * @param string $message + * @param array $headers + * @param int $code + * @param Throwable|null $previous + */ + public function __construct( + string $message = '', + array $headers = [], + int $code = 0, + Throwable $previous = null + ) { + parent::__construct(404, $message, $headers, $code, $previous); + } +} diff --git a/src/Http/Response.php b/src/Http/Response.php index 1a7c8209..a6b4ab74 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -3,6 +3,7 @@ namespace Engelsystem\Http; use Engelsystem\Renderer\Renderer; +use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; @@ -11,21 +12,21 @@ class Response extends SymfonyResponse implements ResponseInterface use MessageTrait; /** @var Renderer */ - protected $view; + protected $renderer; /** * @param string $content * @param int $status * @param array $headers - * @param Renderer $view + * @param Renderer $renderer */ public function __construct( $content = '', int $status = 200, array $headers = [], - Renderer $view = null + Renderer $renderer = null ) { - $this->view = $view; + $this->renderer = $renderer; parent::__construct($content, $status, $headers); } @@ -47,7 +48,7 @@ class Response extends SymfonyResponse implements ResponseInterface * provided status code; if none is provided, implementations MAY * use the defaults as suggested in the HTTP specification. * @return static - * @throws \InvalidArgumentException For invalid status code arguments. + * @throws InvalidArgumentException For invalid status code arguments. */ public function withStatus($code, $reasonPhrase = '') { @@ -107,12 +108,12 @@ class Response extends SymfonyResponse implements ResponseInterface */ public function withView($view, $data = [], $status = 200, $headers = []) { - if (!$this->view instanceof Renderer) { - throw new \InvalidArgumentException('Renderer not defined'); + if (!$this->renderer instanceof Renderer) { + throw new InvalidArgumentException('Renderer not defined'); } $new = clone $this; - $new->setContent($this->view->render($view, $data)); + $new->setContent($this->renderer->render($view, $data)); $new->setStatusCode($status, ($status == $this->getStatusCode() ? $this->statusText : null)); foreach ($headers as $key => $values) { @@ -144,4 +145,14 @@ class Response extends SymfonyResponse implements ResponseInterface return $response; } + + /** + * Set the renderer to use + * + * @param Renderer $renderer + */ + public function setRenderer(Renderer $renderer) + { + $this->renderer = $renderer; + } } diff --git a/src/Http/Validation/Rules/Between.php b/src/Http/Validation/Rules/Between.php new file mode 100644 index 00000000..106a93ac --- /dev/null +++ b/src/Http/Validation/Rules/Between.php @@ -0,0 +1,10 @@ +<?php + +namespace Engelsystem\Http\Validation\Rules; + +use Respect\Validation\Rules\Between as RespectBetween; + +class Between extends RespectBetween +{ + use StringInputLength; +} diff --git a/src/Http/Validation/Rules/Max.php b/src/Http/Validation/Rules/Max.php new file mode 100644 index 00000000..b1b2cfa3 --- /dev/null +++ b/src/Http/Validation/Rules/Max.php @@ -0,0 +1,10 @@ +<?php + +namespace Engelsystem\Http\Validation\Rules; + +use Respect\Validation\Rules\Max as RespectMax; + +class Max extends RespectMax +{ + use StringInputLength; +} diff --git a/src/Http/Validation/Rules/Min.php b/src/Http/Validation/Rules/Min.php new file mode 100644 index 00000000..ab8d4e1a --- /dev/null +++ b/src/Http/Validation/Rules/Min.php @@ -0,0 +1,10 @@ +<?php + +namespace Engelsystem\Http\Validation\Rules; + +use Respect\Validation\Rules\Min as RespectMin; + +class Min extends RespectMin +{ + use StringInputLength; +} diff --git a/src/Http/Validation/Rules/StringInputLength.php b/src/Http/Validation/Rules/StringInputLength.php new file mode 100644 index 00000000..7b5c248b --- /dev/null +++ b/src/Http/Validation/Rules/StringInputLength.php @@ -0,0 +1,44 @@ +<?php + +namespace Engelsystem\Http\Validation\Rules; + +use DateTime; +use Illuminate\Support\Str; +use Throwable; + +trait StringInputLength +{ + /** + * Use the input length of a string + * + * @param mixed $input + * @return bool + */ + public function validate($input): bool + { + if ( + is_string($input) + && !is_numeric($input) + && !$this->isDateTime($input) + ) { + $input = Str::length($input); + } + + return parent::validate($input); + } + + /** + * @param mixed $input + * @return bool + */ + protected function isDateTime($input): bool + { + try { + new DateTime($input); + } catch (Throwable $e) { + return false; + } + + return true; + } +} |