summaryrefslogtreecommitdiff
path: root/src/App.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2022-01-02 01:40:14 +0100
committerDaniel Weipert <code@drogueronin.de>2022-01-10 11:29:13 +0100
commit5df8516fc8ea4386e9045d72b293d0c8f1417409 (patch)
treed49311bb1e562966587953bcec0315a730fde23c /src/App.php
parent4ab2f2df7613747b218116f3c00df36ddbba5bc0 (diff)
Validation and submission logic
Diffstat (limited to 'src/App.php')
-rw-r--r--src/App.php106
1 files changed, 79 insertions, 27 deletions
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;
+ }
}