-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ytake/feature/router-query-param
added request validation attributes
- Loading branch information
Showing
22 changed files
with
448 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?hh // strict | ||
|
||
namespace Nazg\Foundation\Middleware; | ||
|
||
use ReflectionMethod; | ||
use Ytake\Heredity\Heredity; | ||
use Nazg\Foundation\Validation\Attribute; | ||
use Nazg\Foundation\Validation\Validator; | ||
use Nazg\Foundation\Validation\ValidatorFactory; | ||
use Interop\Http\Server\MiddlewareInterface; | ||
use Interop\Http\Server\RequestHandlerInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
enum InterceptorMethod: string { | ||
Process = 'process'; | ||
} | ||
|
||
class Dispatcher extends Heredity { | ||
|
||
protected int $validatorIndex = 0; | ||
protected ?ContainerInterface $container; | ||
|
||
<<__Override>> | ||
protected function processor( | ||
MiddlewareInterface $middleware, | ||
ServerRequestInterface $request | ||
): ResponseInterface { | ||
$this->validateInterceptor($middleware, $request); | ||
return $middleware->process($request, $this); | ||
} | ||
|
||
protected function validateInterceptor( | ||
MiddlewareInterface $middleware, | ||
ServerRequestInterface $request | ||
): void { | ||
$rm = new ReflectionMethod($middleware, InterceptorMethod::Process); | ||
$attribute = $rm->getAttribute(Attribute::Named); | ||
if (is_array($attribute)) { | ||
if(array_key_exists($this->validatorIndex, $attribute)) { | ||
$validator = $this->container?->get((string)$attribute[$this->validatorIndex]); | ||
if($validator instanceof Validator) { | ||
$factory = new ValidatorFactory($validator, $request); | ||
$factory->validator()->validate(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function setContainer(ContainerInterface $container): void { | ||
$this->container = $container; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?hh // strict | ||
|
||
namespace Nazg\Foundation\Validation; | ||
|
||
use Exception; | ||
|
||
class ValidationException extends Exception { | ||
|
||
protected int $status = 400; | ||
|
||
protected Validator $validator; | ||
|
||
public function __construct(Validator $validator) { | ||
parent::__construct('The given data was invalid.'); | ||
$this->validator = $validator; | ||
} | ||
|
||
public function errors(): array<string> { | ||
return $this->validator->errors()->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?hh // strict | ||
|
||
namespace Nazg\Foundation\Validation; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Facebook\TypeAssert; | ||
|
||
enum Attribute: string as string { | ||
Named = 'RequestValidation'; | ||
} | ||
|
||
<<__ConsistentConstruct>> | ||
abstract class Validator { | ||
|
||
protected ?ServerRequestInterface $request; | ||
|
||
protected Vector<string> $messages = Vector{}; | ||
|
||
protected Vector<string> $validateMethods = Vector{}; | ||
|
||
protected bool $shouldThrowException = false; | ||
|
||
// disabled type assert for request parameters | ||
protected bool $skipValidateStructure = true; | ||
|
||
public function validateRequest(ServerRequestInterface $request): void { | ||
$this->request = $request; | ||
} | ||
|
||
public function validate(): bool { | ||
if (!is_null($this->request)) { | ||
$this->assertStructure(); | ||
} | ||
if($this->errors()->count()) { | ||
if ($this->shouldThrowException) { | ||
throw new ValidationException($this); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
<<__Memoize>> | ||
public function errors(): ImmVector<string> { | ||
return $this->assertValidateResult()->immutable(); | ||
} | ||
|
||
protected function assertStructure(): void { | ||
if(!$this->skipValidateStructure) { | ||
// here | ||
} | ||
} | ||
|
||
abstract protected function assertValidateResult(): Vector<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?hh // strict | ||
|
||
namespace Nazg\Foundation\Validation; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class ValidatorFactory { | ||
|
||
public function __construct( | ||
protected Validator $validatorName, | ||
protected ServerRequestInterface $request | ||
) {} | ||
|
||
public function validator(): Validator { | ||
$validator = $this->validatorName; | ||
$validator->validateRequest($this->request); | ||
return $validator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?hh | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Interop\Http\Server\MiddlewareInterface; | ||
use Interop\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\Response\JsonResponse; | ||
|
||
final class ParameterAction implements MiddlewareInterface { | ||
public function process( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler, | ||
): ResponseInterface { | ||
return new JsonResponse( | ||
$request->getAttributes() + $request->getQueryParams() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?hh | ||
|
||
use Nazg\Foundation\Validation\Attribute; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Interop\Http\Server\MiddlewareInterface; | ||
use Interop\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\Response\JsonResponse; | ||
|
||
final class ValidateAction implements MiddlewareInterface { | ||
|
||
<<RequestValidation(\MockValidateActionFaild::class)>> | ||
public function process( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler, | ||
): ResponseInterface { | ||
return new JsonResponse([]); | ||
} | ||
} |
Oops, something went wrong.