-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
109 lines (89 loc) · 2.5 KB
/
Copy pathUser.php
File metadata and controls
109 lines (89 loc) · 2.5 KB
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
<?php
/**
* User data model class
*/
class User {
private ?string $address = null;
private ?int $age = null;
private ?string $email = null;
private ?string $name = null;
private ?string $phone = null;
public function getAddress(): ?string {
return $this->address;
}
public function getAge(): ?int {
return $this->age;
}
public function getEmail(): ?string {
return $this->email;
}
public function getName(): ?string {
return $this->name;
}
public function getPhone(): ?string {
return $this->phone;
}
public function setAddress(?string $address): void {
if ($address !== null) {
$this->address = trim($address);
}
}
public function setAge(int $age): void {
if ($age < 0 || $age > 150) {
throw new InvalidArgumentException('Age must be between 0 and 150');
}
$this->age = $age;
}
public function setEmail(string $email): void {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email format');
}
$this->email = strtolower($email);
}
public function setEmailAddress(?string $email): void {
if ($email !== null) {
$this->setEmail($email);
}
}
// Custom setters for alternative parameter names
public function setFullName(?string $name): void {
if ($name !== null) {
$this->setName($name);
}
}
public function setName(string $name): void {
$this->name = trim($name);
}
public function setPhone(?string $phone): void {
if ($phone !== null) {
$this->phone = preg_replace('/[^0-9+\-\s]/', '', $phone);
}
}
public function setUserAge(?int $age): void {
if ($age !== null) {
$this->setAge($age);
}
}
public function toArray(): array {
return [
'name' => $this->name,
'email' => $this->email,
'age' => $this->age,
'phone' => $this->phone,
'address' => $this->address
];
}
public function validate(): array {
$errors = [];
if (empty($this->name)) {
$errors[] = 'Name is required';
}
if (empty($this->email)) {
$errors[] = 'Email is required';
}
if ($this->age === null) {
$errors[] = 'Age is required';
}
return $errors;
}
}