-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathsolid_singlerep.php
61 lines (54 loc) · 1.26 KB
/
solid_singlerep.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
<?php
class User
{
public $nama;
public $email;
public $dob;
public function __construct($data)
{
$this->nama = $data['nama'];
$this->email = $data['email'];
$this->dob = $data['dob'];
}
}
class UserRequest
{
protected static $rules = [
'nama' => 'string',
'email' => 'string',
'dob' => 'string'
];
public static function validate($data){
foreach (static::$rules as $property => $type){
if (gettype($data[$property]) != $type){
throw new \Exception("User property {$property} must be of type {$type}" );
}
}
}
}
class Json{
public static function from ($data){
return json_encode($data);
}
}
class Age{
public static function now($data){
$dob = new DateTime($data['dob']);
$today = new Datetime(date('d.m.y'));
return [
'year' => $today->diff($dob)->y,
'month' => $today->diff($dob)->m,
'day' => $today->diff($dob)->d,
];
}
}
$data = [
'nama' => 'Ardiansyah',
'email' => 'ardian@uad.ac.id',
'dob' => '23.7.1979'
];
UserRequest::validate($data);
$user = new User($data);
print_r(Json::from($user));
echo '<br>';
print_r(Age::now($data));