-
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.
- Loading branch information
Showing
11 changed files
with
164 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?hh // strict | ||
|
||
namespace Nazg\Foundation\Middleware; | ||
|
||
use ReflectionMethod; | ||
use Ytake\Heredity\Heredity; | ||
use Nazg\Foundation\Validation\Attribute; | ||
use Nazg\Foundation\Validation\Validation; | ||
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->findAttributse($middleware, $request); | ||
return $middleware->process($request, $this); | ||
} | ||
|
||
protected function findAttributse( | ||
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)) { | ||
$v = new ValidatorFactory( | ||
$this->container?->get((string)$attribute[$this->validatorIndex]) | ||
); | ||
var_dump($v->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 Psr\Http\Message\ServerRequestInterface; | ||
|
||
enum Attribute: string as string { | ||
Named = 'RequestValidation'; | ||
} | ||
|
||
<<__ConsistentConstruct>> | ||
abstract class Validation { | ||
|
||
public function __construct( | ||
protected ServerRequestInterface $request | ||
) {} | ||
|
||
public function validate(): bool { | ||
return true; | ||
} | ||
} |
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,17 @@ | ||
<?hh // strict | ||
|
||
namespace Nazg\Foundation\Validation; | ||
|
||
class ValidatorFactory { | ||
|
||
public function __construct( | ||
protected mixed $validatorName | ||
) {} | ||
|
||
public function validate(): bool { | ||
if($this->validatorName instanceof Validation) { | ||
return $this->validatorName->validate(); | ||
} | ||
return true; | ||
} | ||
} |
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 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(\ValidateActionValid::class)>> | ||
public function process( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler, | ||
): ResponseInterface { | ||
return new JsonResponse([]); | ||
} | ||
} |
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
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,7 @@ | ||
<?hh // strict | ||
|
||
use Nazg\Foundation\Validation\Validation; | ||
|
||
final class ValidateActionValid extends Validation { | ||
|
||
} |
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