-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature-attribute.php
140 lines (124 loc) · 3.91 KB
/
feature-attribute.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
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
<?php
// Attribute
#[Attribute]
class NotBlank {
}
// Attribut Validation Emails
#[Attribute(Attribute::TARGET_PROPERTY)]
class Emails {
private array $emails;
private string $categories;
public function __construct(array $array) {
foreach($array as $value) {
if(trim($value) == "") {
throw new Exception("Declaration email is not value");
}
$this->emails[] = $value;
}
}
public function getEmails() {
$str = "";
foreach($this->emails as $email) {
$str .= $email . "|";
}
$length = strlen($str) - 1;
$str = substr($str, 0, $length);
return $str;
}
}
// Attribut Validation Length
#[Attribute(Attribute::TARGET_PROPERTY)]
class Length {
public int $min;
public int $max;
public function __construct(int $min, int $max){
$this->min = $min;
$this->max = $max;
}
}
// Object Login Request in User
class LoginRequest {
#[NotBlank]
#[Length(min : 6, max : 10)]
public ?string $username;
#[NotBlank]
#[Length(min : 6, max : 10)]
public ?string $password;
#[Emails(["@gmail.com", "@yahoo.com", "@example.com"])]
public ?string $email;
}
// Funtion Validation Input
function validation(object $object) : void
{
// Reflection
$class = new ReflectionClass($object);
// GetProperty Return Array of Object ReflectionProperty
$properties = $class->getProperties();
// Looping Onject ReflectionProperty
foreach($properties as $property) {
// Validation Initialied
validationNotBlank($property, $object);
// Validation Length
validationLength($property, $object);
// Validation Emails
validationEmails($property, $object);
}
}
// Function ValidationEmails
function validationEmails(ReflectionProperty $property, $object){
if(!$property->isInitialized($object) || $property->getValue($object) == null){
return;
}
$emails = $property->getAttributes("Emails", ReflectionAttribute::IS_INSTANCEOF);
$value = $property->getValue($object);
foreach ($emails as $email) {
$objEmail = $email->newInstance();
$result = (bool) preg_match_all("/" . $objEmail->getEmails() . "/i", $property->getValue($object));
if(!$result) {
throw new Exception("Email {$property->getValue($object)} is in Valid!");
}
}
}
// Function ValidationLength
function validationLength(ReflectionProperty $property, $object){
if(!$property->isInitialized($object) || $property->getValue($object) == null){
return;
}
$value = $property->getValue($object);
$attributs = $property->getAttributes('Length', ReflectionAttribute::IS_INSTANCEOF);
foreach ($attributs as $attribute) {
if(count($attributs) > 0) {
$length = $attribute->newInstance();
if(strlen($value) < $length->min) {
throw new Exception("$value is to short");
} else if(strlen($value) > $length->max) {
throw new Exception("$value is to max");
}
}
}
}
// Function ValidationInitialized
function validationNotBlank(ReflectionProperty $property, $object)
{
$attributs = $property->getAttributes(NotBlank::class);
if(count($attributs) > 0){
if(!$property->isInitialized($object)) {
throw new Exception("is property {$property->getName()} is not Initialized");
} else if($property->getValue($object) == null){
throw new Exception("is property {$property->getName()} is not null");
}
}
}
// Test Validation
$najib = new LoginRequest();
$najib->username = "Najib";
$najib->password = "Rahasia123";
$najib->email = "[email protected]";
// Coba validation
try {
validation($najib);
// Tangkap Error Exception
} catch(Exception $e) {
// Tampilkan Message/Pesan
echo $e->getMessage();
}