-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.php
90 lines (77 loc) · 2.59 KB
/
data.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
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
<?php
abstract class Datatype {
protected $data;
// false if data is valid, or a string containing the error
protected $invalid;
function __construct($data) {
$this->data = $data;
$this->validate();
}
function get_data() {
return $this->data;
}
// sets data, updates $invalid
function set_data($data) {
$this->data = $data;
$this->validate();
}
function get_invalid() {
return $this->invalid;
}
// updates $invalid
protected abstract function validate();
}
class Dropdown extends Datatype {
function validate() {
if ($this->data == 0) {
$this->invalid = 'Please select an option';
} else {
$this->invalid = false;
}
}
}
class Name extends Datatype {
function validate() {
if (strlen($this->data) == 0) {
$this->invalid = 'Please enter your name';
} else if (!preg_match('/^[a-zA-Z]+$/', $this->data)) {
if (preg_match('/\d/', $this->data)) {
$this->invalid = 'Names cannot contain numbers';
} else {
$this->invalid = 'Names cannot contain special characters';
}
} else {
$this->invalid = false;
}
}
}
class Email extends Datatype {
function validate() {
if (strlen($this->data) == 0) {
$this->invalid = 'Please enter your email';
} else if (!filter_var($this->data, FILTER_VALIDATE_EMAIL)) {
$this->invalid = 'Please enter a valid email address';
} else {
$this->invalid = false;
}
}
}
class Date extends Datatype {
// could do with checking date and time separately
function validate() {
if (strlen(trim($this->data)) == 0) {
$this->invalid = 'Please enter a date';
} else if (date_create_from_format('Y-m-d H:i', $this->data)) {
$this->invalid = false;
} else {
$this->invalid = 'Date sent not valid';
}
}
}
// used for cases with no invalid data possibilities and also basic instantiation
class Valid extends Datatype {
function validate() {
$this->invalid = false;
}
}
?>