Skip to content

Commit

Permalink
feat(form): updates for form processing #25
Browse files Browse the repository at this point in the history
- add Template variables.
- use _self_value instead of _self.value
- use _self_fields instead of _self.fields
- use _self_messages instead of _self.messages
- use _self_redirect instead of _self.redirect
  • Loading branch information
Awilum committed Jun 3, 2021
1 parent 7d5b321 commit f3a691c
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions app/Models/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class Form
*/
private $process = [];

/**
* Template variables.
*
* @var array
* @access private
*/
private $vars = [];

/**
* Form raw data.
*
Expand All @@ -44,10 +52,15 @@ public function __construct(array $data)
{
foreach($data as $processForm => $processStatements) {
if (strings($processForm)->contains('__process_form')) {
$this->process = json_decode($processStatements, true);
$this->data = $data;
$this->process = json_decode($processStatements, true);
}

if (strings($processForm)->contains('__process_vars')) {
$this->vars = json_decode($processStatements, true);
}
}

$this->data = $data;
}

/**
Expand Down Expand Up @@ -78,7 +91,7 @@ public function getRedirect(array $values = []): string
foreach($args as $key => $value) {
$key === array_key_first($args) and $redirect .= '?';

$redirect .= $key . '=' . flextype('twig')->fetchFromString((empty($values) ? $value : strtr($value, $values)));
$redirect .= $key . '=' . flextype('twig')->fetchFromString((empty($values) ? $value : strtr($value, $values)), $this->vars);

$key != array_key_last($args) and $redirect .= '&';
}
Expand Down Expand Up @@ -106,37 +119,37 @@ public function getFields(): array
switch ($field['type']) {
case 'bool':
if (isset($field['value'])) {
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self.value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : []))->toBoolean();
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self_value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : $this->vars))->toBoolean();
} else {
$data[$field['name']] = strings(arrays($this->data)->get($field['name']))->toBoolean();
}
break;
case 'float':
if (isset($field['value'])) {
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self.value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : []))->toFloat();
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self_value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : $this->vars))->toFloat();
} else {
$data[$field['name']] = strings(arrays($this->data)->get($field['name']))->toFloat();
}
break;
case 'int':
if (isset($field['value'])) {
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self.value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : []))->toInteger();
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self_value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : $this->vars))->toInteger();
} else {
$data[$field['name']] = strings(arrays($this->data)->get($field['name']))->toInteger();
}
break;
default:
case 'string':
if (isset($field['value'])) {
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self.value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : []))->toString();
$data[$field['name']] = strings(flextype('twig')->fetchFromString(strings($field['value'])->replace('_self_value', "'" . arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : $this->vars))->toString();
} else {
$data[$field['name']] = strings(arrays($this->data)->get($field['name']))->toString();
}
break;
}
} else {
if (isset($field['value'])) {
$data[$field['name']] = flextype('twig')->fetchFromString(strings($field['value'])->replace('_self.value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : []);
$data[$field['name']] = flextype('twig')->fetchFromString(strings($field['value'])->replace('_self_value', "'". arrays($this->data)->get($field['name']) . "'"), isset($field['data']) ? $field['data'] : $this->vars);
} else {
$data[$field['name']] = arrays($this->data)->get($field['name']);
}
Expand All @@ -159,7 +172,7 @@ public function getFields(): array
*/
public function getMessages(string $type, array $data = []): string
{
return isset($this->process['messages'][$type]) ? flextype('twig')->fetchFromString($this->process['messages'][$type], $data) : '';
return isset($this->process['messages'][$type]) ? flextype('twig')->fetchFromString($this->process['messages'][$type], (count($data) > 0 ? $data : $this->vars)) : '';
}

/**
Expand All @@ -178,17 +191,17 @@ public function getActions()
$properties = array_values($action['properties']);
foreach ($properties as $key => $field) {
switch ($field) {
case '_self.fields':
case '_self_fields':
$properties[$key] = $this->getFields();
break;
case '_self.messages':
case '_self_messages':
$properties[$key] = $this->getMessages();
break;
case '_self.redirect':
case '_self_redirect':
$properties[$key] = $this->getFields();
break;
default:
$properties[$key] = flextype('twig')->fetchFromString($field);
$properties[$key] = flextype('twig')->fetchFromString($field, $this->vars);
break;
}
}
Expand Down

0 comments on commit f3a691c

Please sign in to comment.