- First WIP on form handling
This commit is contained in:
		
							parent
							
								
									cb2caf895e
								
							
						
					
					
						commit
						a8a99f1852
					
				| @ -16,6 +16,8 @@ class Form implements Extension { | ||||
| 
 | ||||
|     public string $token; | ||||
| 
 | ||||
|     public FormContext $formContext; | ||||
|      | ||||
|     public ServerRequestInterface $request; | ||||
|      | ||||
|     public function __construct(ServerRequestInterface $request, Context $context) { | ||||
| @ -27,7 +29,11 @@ class Form implements Extension { | ||||
| 
 | ||||
|     public function register(Context $context) : void | ||||
|     { | ||||
|         $this->formContext = new FormContext($this->request); | ||||
|          | ||||
|         $context->pushFunction("form", [ $this, 'formClass' ]); | ||||
|          | ||||
|         #$context->
 | ||||
|     } | ||||
| 
 | ||||
|     public function form_csrf(string $field, string $value) { | ||||
| @ -46,43 +52,8 @@ class Form implements Extension { | ||||
|         return $value; | ||||
|     } | ||||
| 
 | ||||
|     public function formClass($form) { | ||||
|         return new class($this->request, $form) { | ||||
| 
 | ||||
|             public bool $sent = false; | ||||
| 
 | ||||
|             protected ServerRequestInterface $request; | ||||
|              | ||||
|             protected /* FormInterface */ $form; | ||||
| 
 | ||||
|             public function __construct(ServerRequestInterface $request, /* FormInterface */ $form) | ||||
|             { | ||||
|                 $this->request = $request; | ||||
|                 $this->sent = $this->requestSent(); | ||||
|                 $this->form = $form; | ||||
|             } | ||||
|              | ||||
|             protected function initialize() { | ||||
|                 $this->form->prepare(); | ||||
|                      | ||||
|                 if ( $this->sent ) { | ||||
|                     if ( $this->form->validate() ) { | ||||
|                         $this->form->save(); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             protected function requestSent() : bool | ||||
|             { | ||||
|                 return in_array($this->request->getMethod(), [ | ||||
|                     "DELETE", "PATCH", "POST", "PUT", | ||||
|                 ]); | ||||
|             } | ||||
| 
 | ||||
|             protected function honeyPot() : bool | ||||
|             { | ||||
|                 $this->request->getServerParams(); | ||||
|             } | ||||
|         }; | ||||
|     public function formClass(FormInterface $form, ? FormContext $formContext = null)  | ||||
|     { | ||||
|         return new FormHandler($this->request, $form, $formContext ?: $this->formContext); | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										86
									
								
								src/Method/FormContext.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								src/Method/FormContext.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,86 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Picea\Ui\Method; | ||||
| 
 | ||||
| use Psr\Http\Message\ServerRequestInterface, | ||||
|     Psr\Http\Message\ResponseInterface; | ||||
| 
 | ||||
| class FormContext implements FormContextInterface | ||||
| { | ||||
|     public bool $formSent; | ||||
|      | ||||
|     public array $values; | ||||
| 
 | ||||
|     public array $messages = []; | ||||
|      | ||||
|     public ServerRequestInterface $request; | ||||
|      | ||||
|     public ? ResponseInterface $response = null; | ||||
| 
 | ||||
|     public function __construct(ServerRequestInterface $request)  | ||||
|     { | ||||
|         $this->request = $request; | ||||
|         $this->values = $request->getParsedBody() ?: []; | ||||
|     } | ||||
|      | ||||
|     public function valid() : bool | ||||
|     { | ||||
|         foreach($this->messages as $message) { | ||||
|             if ( $message->isError() ) { | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|          | ||||
|         return true; | ||||
|     } | ||||
|      | ||||
|     public function formSent() : bool | ||||
|     { | ||||
|         return $this->formSent; | ||||
|     } | ||||
| 
 | ||||
|     public function __set($key, $value) | ||||
|     { | ||||
|         return $this->set($key, $value); | ||||
|     } | ||||
| 
 | ||||
|     public function __get($key) | ||||
|     { | ||||
|         return $this->get($key); | ||||
|     } | ||||
| 
 | ||||
|     public function __isset($key) | ||||
|     { | ||||
|         return array_key_exists($key, $_SESSION); | ||||
|     } | ||||
| 
 | ||||
|     public function __unset($key) | ||||
|     { | ||||
|         $this->delete($key); | ||||
|     }    | ||||
|      | ||||
|     public function get(string $key, $default = null) | ||||
|     { | ||||
|         return $this->has($key) ? $this->values[$key] : $default; | ||||
|     } | ||||
| 
 | ||||
|     public function set(string $key, $value) | ||||
|     { | ||||
|         return $_SESSION[$key] = $value; | ||||
|     } | ||||
| 
 | ||||
|     public function delete(string $key) : void | ||||
|     { | ||||
|         unset($this->values[$key]); | ||||
|     } | ||||
| 
 | ||||
|     public function has(string $key) : bool | ||||
|     { | ||||
|         return array_key_exists($key, $this->values); | ||||
|     } | ||||
|      | ||||
|     public function pushMessage(FormMessage $message) : void | ||||
|     { | ||||
|         $this->messages[] = $message; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										9
									
								
								src/Method/FormContextInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/Method/FormContextInterface.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Picea\Ui\Method; | ||||
| 
 | ||||
| interface FormContextInterface { | ||||
|     public function valid() : bool; | ||||
|      | ||||
|     public function formSent() : bool; | ||||
| } | ||||
							
								
								
									
										50
									
								
								src/Method/FormHandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/Method/FormHandler.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,50 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Picea\Ui\Method; | ||||
| 
 | ||||
| use Psr\Http\Message\ServerRequestInterface; | ||||
| 
 | ||||
| class FormHandler { | ||||
|     # return new class($this->request, new FormContext(), $form) {
 | ||||
| 
 | ||||
|     public bool $sent = false; | ||||
|      | ||||
|     public FormContext $context; | ||||
| 
 | ||||
|     protected ServerRequestInterface $request; | ||||
| 
 | ||||
|     protected FormInterface $form; | ||||
| 
 | ||||
|     public function __construct(ServerRequestInterface $request, FormInterface $form, ? FormContextInterface $context = null) | ||||
|     { | ||||
|         $this->request = $request; | ||||
|         $this->sent = $this->requestSent(); | ||||
|         $this->form = $form; | ||||
|         $this->context = $context ?: new FormContext($request); | ||||
|         $this->context->formSent = $this->sent; | ||||
|         $this->initialize(); | ||||
|     } | ||||
| 
 | ||||
|     protected function initialize() : void | ||||
|     { | ||||
|         $this->form->initialize($this->context); | ||||
| 
 | ||||
|         if ( $this->sent ) { | ||||
|             if ( $this->form->validate($this->context) ) { | ||||
|                 $this->form->save($this->context); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     protected function requestSent() : bool | ||||
|     { | ||||
|         return in_array($this->request->getMethod(), [ | ||||
|             "DELETE", "PATCH", "POST", "PUT", | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     protected function honeyPot() : bool | ||||
|     { | ||||
|         $this->request->getServerParams(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										12
									
								
								src/Method/FormInterface.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/Method/FormInterface.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Picea\Ui\Method; | ||||
| 
 | ||||
| interface FormInterface  | ||||
| { | ||||
|     public function initialize(FormContextInterface $context) : void; | ||||
| 
 | ||||
|     public function validate(FormContextInterface $context) : bool; | ||||
|      | ||||
|     public function save(FormContextInterface $context) : void; | ||||
| } | ||||
							
								
								
									
										7
									
								
								src/Method/FormMessage.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/Method/FormMessage.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Picea\Ui\Method; | ||||
| 
 | ||||
| interface FormMessage {  | ||||
|     public function isError() : bool; | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user