aboutsummaryrefslogtreecommitdiff
path: root/lib/ParameterValidator.php
blob: e27835862103fa784663c9e53157b2e83678b651 (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

class ParameterValidator
{
    /**
     * Validate and sanitize user inputs against configured bridge parameters (contexts)
     */
    public function validateInput(array &$input, $contexts): array
    {
        $errors = [];

        foreach ($input as $name => $value) {
            $registered = false;
            foreach ($contexts as $contextName => $contextParameters) {
                if (!array_key_exists($name, $contextParameters)) {
                    continue;
                }
                $registered = true;
                if (!isset($contextParameters[$name]['type'])) {
                    // Default type is text
                    $contextParameters[$name]['type'] = 'text';
                }

                switch ($contextParameters[$name]['type']) {
                    case 'number':
                        $input[$name] = $this->validateNumberValue($value);
                        break;
                    case 'checkbox':
                        $input[$name] = $this->validateCheckboxValue($value);
                        break;
                    case 'list':
                        $input[$name] = $this->validateListValue($value, $contextParameters[$name]['values']);
                        break;
                    default:
                    case 'text':
                        if (isset($contextParameters[$name]['pattern'])) {
                            $input[$name] = $this->validateTextValue($value, $contextParameters[$name]['pattern']);
                        } else {
                            $input[$name] = $this->validateTextValue($value);
                        }
                        break;
                }

                if (
                    is_null($input[$name])
                    && isset($contextParameters[$name]['required'])
                    && $contextParameters[$name]['required']
                ) {
                    $errors[] = ['name' => $name, 'reason' => 'Parameter is invalid!'];
                }
            }

            if (!$registered) {
                $errors[] = ['name' => $name, 'reason' => 'Parameter is not registered!'];
            }
        }

        return $errors;
    }

    /**
     * Get the name of the context matching the provided inputs
     *
     * @param array $input Associative array of user data
     * @param array $contexts Array of bridge parameters
     * @return string|null Returns the context name or null if no match was found
     */
    public function getQueriedContext(array $input, array $contexts)
    {
        $queriedContexts = [];

        // Detect matching context
        foreach ($contexts as $contextName => $contextParameters) {
            $queriedContexts[$contextName] = null;

            // Ensure all user data exist in the current context
            $notInContext = array_diff_key($input, $contextParameters);
            if (array_key_exists('global', $contexts)) {
                $notInContext = array_diff_key($notInContext, $contexts['global']);
            }
            if (count($notInContext) > 0) {
                continue;
            }

            // Check if all parameters of the context are satisfied
            foreach ($contextParameters as $id => $properties) {
                if (!empty($input[$id])) {
                    $queriedContexts[$contextName] = true;
                } elseif (
                    isset($properties['type'])
                    && ($properties['type'] === 'checkbox' || $properties['type'] === 'list')
                ) {
                    continue;
                } elseif (isset($properties['required']) && $properties['required'] === true) {
                    $queriedContexts[$contextName] = false;
                    break;
                }
            }
        }

        // Abort if one of the globally required parameters is not satisfied
        if (
            array_key_exists('global', $contexts)
            && $queriedContexts['global'] === false
        ) {
            return null;
        }
        unset($queriedContexts['global']);

        switch (array_sum($queriedContexts)) {
            case 0:
                // Found no match, is there a context without parameters?
                if (isset($input['context'])) {
                    return $input['context'];
                }
                foreach ($queriedContexts as $context2 => $queried) {
                    if (is_null($queried)) {
                        return $context2;
                    }
                }
                return null;
            case 1:
                // Found unique match
                return array_search(true, $queriedContexts);
            default:
                return false;
        }
    }

    private function validateTextValue($value, $pattern = null)
    {
        if (is_null($pattern)) {
            // No filtering taking place
            $filteredValue = filter_var($value);
        } else {
            $filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^' . $pattern . '$/']]);
        }
        if ($filteredValue === false) {
            return null;
        }
        return $filteredValue;
    }

    private function validateNumberValue($value)
    {
        $filteredValue = filter_var($value, FILTER_VALIDATE_INT);
        if ($filteredValue === false) {
            return null;
        }
        return $filteredValue;
    }

    private function validateCheckboxValue($value)
    {
        return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
    }

    private function validateListValue($value, $expectedValues)
    {
        $filteredValue = filter_var($value);
        if ($filteredValue === false) {
            return null;
        }
        if (!in_array($filteredValue, $expectedValues)) {
            // Check sub-values?
            foreach ($expectedValues as $subName => $subValue) {
                if (is_array($subValue) && in_array($filteredValue, $subValue)) {
                    return $filteredValue;
                }
            }
            return null;
        }
        return $filteredValue;
    }
}