From 5df8516fc8ea4386e9045d72b293d0c8f1417409 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 2 Jan 2022 01:40:14 +0100 Subject: Validation and submission logic --- src/App.php | 106 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/App.php b/src/App.php index c0df751..20cec62 100644 --- a/src/App.php +++ b/src/App.php @@ -5,13 +5,10 @@ namespace FlatFileForms; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Yosymfony\Toml\Toml; +use Yosymfony\Toml\TomlBuilder; class App { - private array $routes = [ - - ]; - /** * App constructor. */ @@ -28,33 +25,65 @@ class App $method = $request->getMethod(); $path = $request->getPathInfo(); - // GET - if ($method == 'GET') { - if (str_ends_with($path, '/fields')) { - $content['data'] = Toml::parseFile($contentRoot . $path . '/_fields.toml'); - } - else { - $content['data'] = Toml::parseFile($contentRoot . $path . '.toml'); - } - } - - // POST - else if ($method == 'POST') { - if (str_ends_with($path, 'submit')) { - $formPath = $contentRoot . str_replace('/submit', '', $path); + try { + // GET + if ($method == 'GET') { + if (str_ends_with($path, '/fields')) { + $formPath = $contentRoot . str_replace('/fields', '', $path); - // build fields - $fields = $this->buildFields($formPath); + $content['data'] = $this->buildFields($formPath); + } + else { + $content['data'] = Toml::parseFile($contentRoot . $path . '.toml'); + } + } - foreach ($fields as $field) { - if ($field['required'] ?? false) { - $content['error'] = 'REQUIRED!'; + // POST + else if ($method == 'POST') { + if (str_ends_with($path, '/submit')) { + $formPath = $contentRoot . str_replace('/submit', '', $path); + + $fields = $this->buildFields($formPath); + $fields = $this->validateFields($fields); + + // remove surplus fields from response + $fields = array_map(function ($field) { + return array_intersect_key($field, array_flip([ + 'is_valid', + ])); + }, $fields); + + $content['data'] = $fields; + + $hasInvalidFields = in_array(false, array_column($fields, 'is_valid')); + if ($hasInvalidFields) { + $content['error'] = 'Invalid fields'; + } else { + $date = new \Datetime(); + $entry = [ + 'fields' => $_POST, + 'date' => $date->format('c'), + ]; + + $builder = new TomlBuilder(); + $builder->addValue('date', $entry['date']); + $builder->addTable('fields'); + foreach ($entry['fields'] as $entryKey => $entryValue) { + $builder->addValue($entryKey, $entryValue); + } + + $entryDirectory = $formPath . '/entries/' . $date->format('Y/m/d'); + @mkdir($entryDirectory, 0774, true); + $entryFilename = $date->format('Ymd_Hi_') . hash('adler32', serialize($entry)) . '.toml'; + file_put_contents( + $entryDirectory . '/' . $entryFilename, + $builder->getTomlString() + ); } } - - $content['data'] = $fields; - $content['POST'] = $_POST; } + } catch (\Exception $exception) { + $content['error'] = $exception->getMessage(); } $response->headers->set('Content-Type', 'application/json'); @@ -69,11 +98,34 @@ class App { $fields = Toml::parseFile($formPath . '/fields/_fields.toml')['field'] ?? []; foreach ($fields as $key => $field) { - $field = array_merge($field, Toml::parseFile($formPath . '/fields/' . $field['file'])); + if (empty($field['name'])) { + $field['name'] = $key; + } + + if (! empty($field['file'])) { + $field = array_merge($field, Toml::parseFile($formPath . '/fields/' . $field['file'])); + } + $fields[$key] = $field; } return $fields; } + + /** + * @param array $fields + */ + public function validateFields($fields) + { + foreach ($fields as $key => &$field) { + $field['is_valid'] = true; + + if (($field['required'] ?? false) && empty($_POST[$field['name']])) { + $field['is_valid'] = false; + } + } + + return $fields; + } } -- cgit v1.2.3