diff options
author | Daniel Weipert <code@drogueronin.de> | 2022-03-06 11:45:31 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2022-03-06 11:45:31 +0100 |
commit | 1334236bb6059b2302a760196ce7885df379e984 (patch) | |
tree | f86e883e6ae9510454307f38be3ffd20aefa6c4c /src | |
parent | ad83d1dafac62bb28fda004f86129319a5c3e2ca (diff) |
toml to yaml
Diffstat (limited to 'src')
-rw-r--r-- | src/App.php | 19 | ||||
-rw-r--r-- | src/Builder.php | 35 | ||||
-rw-r--r-- | src/Controllers/EntriesController.php | 4 | ||||
-rw-r--r-- | src/Controllers/SubmissionController.php | 24 | ||||
-rw-r--r-- | src/Form.php | 22 | ||||
-rw-r--r-- | src/HookManager.php | 4 | ||||
-rw-r--r-- | src/Validator.php | 18 |
7 files changed, 88 insertions, 38 deletions
diff --git a/src/App.php b/src/App.php index 5fc62fa..ea9aa29 100644 --- a/src/App.php +++ b/src/App.php @@ -8,8 +8,7 @@ use FlatFileForms\Controllers\SubmissionController; use FlatFileForms\Controllers\ValidationController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Yosymfony\Toml\Toml; -use Yosymfony\Toml\TomlBuilder; +use Symfony\Component\Yaml\Yaml; class App { @@ -23,8 +22,13 @@ class App /**@var HookManager $hooks*/ global $hooks; + /**@var Form $form*/ + global $form; + $hooks->doAction('init'); + global $request; + global $response; $request = Request::createFromGlobals(); $response = new Response(); @@ -36,6 +40,7 @@ class App $method = $request->getMethod(); $path = $request->getPathInfo(); + $config = []; try { $config = $this->buildConfig($contentRoot . $path); @@ -52,6 +57,7 @@ class App if ($method == 'GET') { if (str_ends_with($path, '/fields')) { $formPath = $contentRoot . str_replace('/fields', '', $path); + $form = new Form($formPath); $builder = new Builder($formPath); @@ -66,6 +72,7 @@ class App } $formPath = $contentRoot . str_replace('/entries', '', $path); + $form = new Form($formPath); $entriesController = new EntriesController(); @@ -73,7 +80,7 @@ class App } else { - $content['data'] = Toml::parseFile($contentRoot . $path . '.toml'); + $content['data'] = Yaml::parseFile($contentRoot . $path . '.yaml'); } } @@ -81,6 +88,7 @@ class App else if ($method == 'POST') { if (str_ends_with($path, '/validate')) { $formPath = $contentRoot . str_replace('/validate', '', $path); + $form = new Form($formPath); $builder = new Builder($formPath); $validator = new Validator($formPath); @@ -92,6 +100,7 @@ class App else if (str_ends_with($path, '/submit')) { $formPath = $contentRoot . str_replace('/submit', '', $path); + $form = new Form($formPath); $builder = new Builder($formPath); $validator = new Validator($formPath); @@ -124,9 +133,9 @@ class App $config = []; $currentDirectory = $requestPath; while (true) { - $configFile = $currentDirectory . '/config/config.toml'; + $configFile = $currentDirectory . '/config/config.yaml'; if (file_exists($configFile)) { - $parsedConfig = Toml::parseFile($configFile); + $parsedConfig = Yaml::parseFile($configFile); $apiKeys = array_merge($parsedConfig['api']['keys'] ?? [], $config['api']['keys'] ?? []); $config = array_replace_recursive($parsedConfig, $config); diff --git a/src/Builder.php b/src/Builder.php index 308fa82..1172fc8 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -2,7 +2,7 @@ namespace FlatFileForms; -use Yosymfony\Toml\Toml; +use Symfony\Component\Yaml\Yaml; class Builder { @@ -13,27 +13,33 @@ class Builder public function buildFields(mixed $page = null) { - $parsed = Toml::parseFile($this->formPath . '/fields/_fields.toml'); + /**@var HookManager $hooks*/ + global $hooks; + + /**@var Form $form*/ + global $form; + + $parsed = Yaml::parseFile($this->formPath . '/fields/_fields.yaml'); $fields = []; // if a page is requested if ($page) { - if (! isset($parsed['page'])) { + if (! isset($parsed['pages'])) { throw new \Exception('Form has no pages'); } - if (! isset($parsed['page'][$page])) { + if (! isset($parsed['pages'][$page])) { throw new \Exception('Form has no page ' . $page); } - $fields = $this->buildSinglePageFields($parsed['page'][$page]); + $fields = $this->buildSinglePageFields($parsed['pages'][$page]); } // else get all fields else { // if form is paged - if (isset($parsed['page'])) { - $pages = $parsed['page']; + if (isset($parsed['pages'])) { + $pages = $parsed['pages']; foreach ($pages as $pageKey => $pageFields) { $fields[$pageKey] = $this->buildSinglePageFields($pageFields); } @@ -41,34 +47,37 @@ class Builder // if form is not paged else { - foreach ($parsed['field'] as $key => $field) { + foreach ($parsed as $key => $field) { $fields[$key] = $this->buildSingleField($key, $field); } } } + $fields = $hooks->applyFilter("builder:{$form->name}:fields", $fields); + $fields = $hooks->applyFilter('builder:fields', $fields); + return $fields; } - public function buildSinglePageFields(array $pageFields): array + private function buildSinglePageFields(array $pageFields): array { $fields = []; if (! empty($pageFields['file'])) { - $pageFields = array_replace_recursive($pageFields, Toml::parseFile($this->formPath . '/fields/' . $pageFields['file'])); + $pageFields = array_replace_recursive($pageFields, Yaml::parseFile($this->formPath . '/fields/' . $pageFields['file'])); } - foreach ($pageFields['field'] as $key => $field) { + foreach ($pageFields['fields'] as $key => $field) { $fields[$key] = $this->buildSingleField($key, $field); } return $fields; } - public function buildSingleField(string $key, array $field): array + private function buildSingleField(string $key, array $field): array { if (! empty($field['file'])) { - $field = array_replace_recursive($field, Toml::parseFile($this->formPath . '/fields/' . $field['file'])); + $field = array_replace_recursive($field, Yaml::parseFile($this->formPath . '/fields/' . $field['file'])); } if (empty($field['name'])) { diff --git a/src/Controllers/EntriesController.php b/src/Controllers/EntriesController.php index 801fe7b..f45866a 100644 --- a/src/Controllers/EntriesController.php +++ b/src/Controllers/EntriesController.php @@ -2,7 +2,7 @@ namespace FlatFileForms\Controllers; -use Yosymfony\Toml\Toml; +use Symfony\Component\Yaml\Yaml; class EntriesController { @@ -65,7 +65,7 @@ class EntriesController $entriesForDay = $utilities->scandir($dayPath); foreach ($entriesForDay as $entryForDay) { - $entry = Toml::parseFile($entryForDay); + $entry = Yaml::parseFile($entryForDay); if (isset($_GET['flat'])) { $entries[] = $entry; } else { diff --git a/src/Controllers/SubmissionController.php b/src/Controllers/SubmissionController.php index 2c3d603..8d7d9e6 100644 --- a/src/Controllers/SubmissionController.php +++ b/src/Controllers/SubmissionController.php @@ -3,13 +3,21 @@ namespace FlatFileForms\Controllers; use FlatFileForms\Builder; +use FlatFileForms\Form; +use FlatFileForms\HookManager; use FlatFileForms\Validator; -use Yosymfony\Toml\TomlBuilder; +use Symfony\Component\Yaml\Yaml; class SubmissionController { public function submit(Builder $builder, Validator $validator, string $formPath): array { + /**@var HookManager $hooks*/ + global $hooks; + + /**@var Form $form*/ + global $form; + $fields = $builder->buildFields(); // run through validation @@ -21,23 +29,19 @@ class SubmissionController if (empty($result['error'])) { $date = new \Datetime(); $entry = [ - 'fields' => $_POST, 'date' => $date->format('c'), + 'fields' => $_POST, ]; - $entryBuilder = new TomlBuilder(); - $entryBuilder->addValue('date', $entry['date']); - $entryBuilder->addTable('fields'); - foreach ($entry['fields'] as $entryKey => $entryValue) { - $entryBuilder->addValue($entryKey, $entryValue); - } + $entry = $hooks->applyFilter("submit:{$form->name}:entry", $entry); + $entry = $hooks->applyFilter('submit:entry', $entry); $entryDirectory = $formPath . '/entries/' . $date->format('Y/m/d'); @mkdir($entryDirectory, 0774, true); - $entryFilename = $date->format('Ymd_Hi_') . hash('adler32', serialize($entry)) . '.toml'; + $entryFilename = $date->format('Ymd_Hi_') . hash('adler32', serialize($entry)) . '.yaml'; file_put_contents( $entryDirectory . '/' . $entryFilename, - $entryBuilder->getTomlString() + Yaml::dump($entry, 4) ); } else { diff --git a/src/Form.php b/src/Form.php new file mode 100644 index 0000000..62527aa --- /dev/null +++ b/src/Form.php @@ -0,0 +1,22 @@ +<?php + +namespace FlatFileForms; + +class Form +{ + public string $name; + + public function __construct( + public string $path, + ) + { + $this->name = basename($path); + } + + public function getFields(mixed $page = null) + { + $builder = new Builder($this->path); + + return $builder->buildFields($page); + } +} diff --git a/src/HookManager.php b/src/HookManager.php index 0f252c8..215ff58 100644 --- a/src/HookManager.php +++ b/src/HookManager.php @@ -19,7 +19,7 @@ class HookManager public function doAction(string $name, mixed ...$arguments): void { - foreach ($this->actions[$name] as $actions) { + foreach ($this->actions[$name] ?? [] as $actions) { foreach ($actions as $action) { call_user_func_array($action['function'], $arguments); } @@ -31,7 +31,7 @@ class HookManager // set $value as first argument array_unshift($arguments, $value); - foreach ($this->filters[$name] as $filters) { + foreach ($this->filters[$name] ?? [] as $filters) { foreach ($filters as $filter) { $value = call_user_func_array($filter['function'], $arguments); } diff --git a/src/Validator.php b/src/Validator.php index 33cdf34..03d09b8 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -51,7 +51,7 @@ class Validator return $result; } - public function validateFields(array $fields): array + private function validateFields(array $fields): array { /**@var Utilities $utilities*/ global $utilities; @@ -71,8 +71,14 @@ class Validator return $fields; } - public function validateSingleField(array $field): array + private function validateSingleField(array $field): array { + /**@var HookManager $hooks*/ + global $hooks; + + /**@var Form $form*/ + global $form; + $value = $_POST[$field['name']] ?? ''; $field['is_valid'] = true; @@ -84,10 +90,10 @@ class Validator $field['is_valid'] = false; } - $validationFunctionName = 'validate_' . basename($this->formPath) . '_' . $field['name']; - if (function_exists($validationFunctionName)) { - $field = call_user_func($validationFunctionName, $field, $value); - } + $field = $hooks->applyFilter("validator:{$form->name}:field", $field, $value); + $field = $hooks->applyFilter("validator:{$form->name}:field:{$field['name']}", $field, $value); + $field = $hooks->applyFilter("validator:field:{$field['name']}", $field, $value); + $field = $hooks->applyFilter('validator:field', $field, $value); return $field; } |