-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.questions.php
48 lines (39 loc) · 1.17 KB
/
class.questions.php
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
<?php
class Questions {
private $questions;
public function __construct() {
$this->questions = json_decode(file_get_contents('questions.json'), true);
}
public function byId($id) {
return $this->questions[$id];
}
public function validate($id, $answer) {
$question = $this->questions[$id];
switch($question['answer_type']) {
case 'regex':
if( preg_match($question['answer'], $answer, $matches) ) {
return $matches;
}
else {
return false;
}
break;
case 'string':
return $answer === $question['answer'];
break;
case 'int':
if( $answer = filter_var($answer, FILTER_VALIDATE_INT)) {
return $answer === (int)$question['answer'];
}
else {
return false;
}
break;
case 'choice':
return in_array($answer, $question['answer']);
break;
default:
return false;
}
}
}