From a8a99f18524837759083de329e1bfdeced23b768 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 5 Feb 2020 16:21:23 -0500 Subject: [PATCH] - First WIP on form handling --- src/Method/Form.php | 47 +++------------- src/Method/FormContext.php | 86 +++++++++++++++++++++++++++++ src/Method/FormContextInterface.php | 9 +++ src/Method/FormHandler.php | 50 +++++++++++++++++ src/Method/FormInterface.php | 12 ++++ src/Method/FormMessage.php | 7 +++ 6 files changed, 173 insertions(+), 38 deletions(-) create mode 100644 src/Method/FormContext.php create mode 100644 src/Method/FormContextInterface.php create mode 100644 src/Method/FormHandler.php create mode 100644 src/Method/FormInterface.php create mode 100644 src/Method/FormMessage.php diff --git a/src/Method/Form.php b/src/Method/Form.php index 845587e..fa48bb8 100644 --- a/src/Method/Form.php +++ b/src/Method/Form.php @@ -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); } } diff --git a/src/Method/FormContext.php b/src/Method/FormContext.php new file mode 100644 index 0000000..efc0394 --- /dev/null +++ b/src/Method/FormContext.php @@ -0,0 +1,86 @@ +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; + } +} \ No newline at end of file diff --git a/src/Method/FormContextInterface.php b/src/Method/FormContextInterface.php new file mode 100644 index 0000000..13fc1ea --- /dev/null +++ b/src/Method/FormContextInterface.php @@ -0,0 +1,9 @@ +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(); + } +} diff --git a/src/Method/FormInterface.php b/src/Method/FormInterface.php new file mode 100644 index 0000000..0f5971f --- /dev/null +++ b/src/Method/FormInterface.php @@ -0,0 +1,12 @@ +