summaryrefslogtreecommitdiff
path: root/src/Validator.php
blob: 03d09b8a67e356d3610c5148c83c83516de5a62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace FlatFileForms;

class Validator
{
  public function __construct(
    private string $formPath
  )
  {}

  public function validateRequest(array $fields): array
  {
    /**@var Utilities $utilities*/
    global $utilities;

    $result = [];
    $hasInvalidFields = false;

    $fields = $this->validateFields($fields);

    if ($utilities->isPagedFieldSet($fields)) {
      // remove surplus field values from response
      $fields = array_map(function ($page) {
        return array_map(function ($field) {
          return array_intersect_key($field, array_flip([
            'is_valid',
          ]));
        }, $page);
      }, $fields);

      $flattened = array_merge(...array_values($fields));
      $hasInvalidFields = in_array(false, array_column($flattened, 'is_valid'));
    } else {
      // remove surplus field values from response
      $fields = array_map(function ($field) {
        return array_intersect_key($field, array_flip([
          'is_valid',
        ]));
      }, $fields);

      $hasInvalidFields = in_array(false, array_column($fields, 'is_valid'));
    }

    $result['fields'] = $fields;

    if ($hasInvalidFields) {
      $result['error'] = 'invalid fields';
    }

    return $result;
  }

  private function validateFields(array $fields): array
  {
    /**@var Utilities $utilities*/
    global $utilities;

    if ($utilities->isPagedFieldSet($fields)) {
      foreach ($fields as $pageKey => &$pageFields) {
        foreach ($pageFields as $key => &$field) {
          $field = $this->validateSingleField($field);
        }
      }
    } else {
      foreach ($fields as $key => &$field) {
        $field = $this->validateSingleField($field);
      }
    }

    return $fields;
  }

  private function validateSingleField(array $field): array
  {
    /**@var HookManager $hooks*/
    global $hooks;

    /**@var Form $form*/
    global $form;

    $value = $_POST[$field['name']] ?? '';
    $field['is_valid'] = true;

    if (isset($field['required']) && empty($value)) {
      $field['is_valid'] = false;
    }

    if (isset($field['validation']['pattern']) && preg_match_all('/' . $field['validation']['pattern'] . '/', $value) === 0) {
      $field['is_valid'] = false;
    }

    $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;
  }
}