11<?php
22
3+
34namespace app \core ;
45
56class Request
67{
7- public static function method ()
8+ private static array $ params = [];
9+ private static array $ query = [];
10+ private static array $ body = [];
11+
12+ public static function capture ()
13+ {
14+ self ::$ query = $ _GET ;
15+ self ::$ body = $ _POST ;
16+
17+ // if (self::isMethod('PUT') || self::isMethod('DELETE')) {
18+ // // Handle PUT and DELETE requests
19+ // parse_str(file_get_contents('php://input'), $put_delete_data);
20+
21+ // // Check if it's a JSON request
22+ // if (self::isJsonRequest()) {
23+ // $json_data = json_decode(file_get_contents('php://input'), true);
24+ // self::$body = array_merge(self::$body, $json_data ? $json_data : []);
25+ // } else {
26+ // // Merge PUT or DELETE data into body
27+ // self::$body = array_merge(self::$body, $put_delete_data);
28+ // }
29+ // }
30+
31+ // if (self::isJsonRequest()) {
32+ // // Handle JSON requests
33+ // $json_data = json_decode(file_get_contents('php://input'), true);
34+ // self::$body = array_merge(self::$body, $json_data ? $json_data : []);
35+ // }
36+
37+ // // Handle file uploads
38+ // if (self::isMethod('PUT') && self::hasFile('file')) {
39+ // $file_data = $_FILES['file'];
40+ // self::$body['file'] = $file_data;
41+ // }
42+
43+ if (self ::isMethod ('PUT ' ) || self ::isMethod ('DELETE ' )) {
44+ parse_str (file_get_contents ('php://input ' ), $ put_delete_data );
45+ self ::$ body = array_merge (self ::$ body , $ put_delete_data );
46+ }
47+
48+ if (self ::isJsonRequest ()) {
49+ $ jsonData = json_decode (file_get_contents ('php://input ' ), true );
50+ self ::$ body = array_merge (self ::$ body , $ jsonData ? $ jsonData : []);
51+ }
52+ }
53+
54+ public static function method (): string
855 {
956 return $ _SERVER ['REQUEST_METHOD ' ];
1057 }
1158
12- public static function isPostMethod (): bool
59+ public static function isMethod ($ method ): bool
60+ {
61+ return self ::method () === strtoupper ($ method );
62+ }
63+
64+
65+ public static function isJsonRequest (): bool
1366 {
14- return self :: method () === ' POST ' ;
67+ return isset ( $ _SERVER [ " CONTENT_TYPE " ]) && strpos ( $ _SERVER [ " CONTENT_TYPE " ], ' application/json ' ) !== false ;
1568 }
1669
17- public static function isGetMethod (): bool
70+ public static function all (): array
1871 {
19- return self ::method () === ' GET ' ;
72+ return array_merge ( self ::$ query , self :: $ body , self :: $ params ) ;
2073 }
2174
22- public static function exists ($ type = 'POST ' )
75+ public static function hasFile ($ item ): bool
76+ {
77+ return isset ($ _FILES [$ item ]);
78+ }
79+ public static function file ($ item )
2380 {
24- $ requestData = ($ type === 'POST ' ) ? $ _POST : $ _GET ;
25- return !empty ($ requestData );
81+ return $ _FILES [$ item ];
2682 }
2783
2884 public static function input ($ item )
2985 {
30- $ inputs = array_merge ($ _POST , $ _GET , $ _FILES );
86+ return self ::$ body [$ item ] ?? null ;
87+ }
3188
32- // Check if the content type is JSON (safely check if header exists)
33- $ contentType = $ _SERVER ["CONTENT_TYPE " ] ?? '' ;
34- if (!empty ($ contentType ) && strpos ($ contentType , 'application/json ' ) !== false ) {
35- $ jsonData = json_decode (file_get_contents ('php://input ' ), true );
36- $ inputs = array_merge ($ inputs , $ jsonData ? $ jsonData : []);
89+ public static function getBody (): array
90+ {
91+ return self ::$ body ;
92+ }
93+
94+ public static function add ($ key , $ value )
95+ {
96+ self ::$ body [$ key ] = $ value ;
97+ }
98+
99+ public static function replace ($ key , $ value )
100+ {
101+ if (array_key_exists ($ key , self ::$ body )) {
102+ self ::$ body [$ key ] = $ value ;
103+ }
104+ }
105+ public static function exists ($ key )
106+ {
107+ return array_key_exists ($ key , self ::$ body );
108+ }
109+
110+ public static function params ($ key = null )
111+ {
112+ if ($ key === null ) {
113+ return self ::$ params ;
37114 }
38115
39- return $ inputs [ $ item ] ?? '' ;
116+ return self :: $ params [ $ key ] ?? null ;
40117 }
41118
42- public static function body ( )
119+ public static function query ( $ keys = null , $ defaults = null )
43120 {
44- $ body = file_get_contents ('php://input ' );
45- $ body = json_decode ($ body , true );
46- return $ body ;
121+ if ($ keys === null ) {
122+ return self ::$ query ;
123+ }
124+
125+ if (!is_array ($ keys )) {
126+ $ keys = [$ keys => $ defaults ];
127+ }
128+
129+ $ result = [];
130+ foreach ($ keys as $ key => $ default ) {
131+ if (!isset (self ::$ query [$ key ]) && $ default !== null ) {
132+ self ::$ query [$ key ] = $ default ;
133+ }
134+
135+ $ result [$ key ] = self ::$ query [$ key ] ?? null ;
136+ }
137+
138+ return $ result ;
139+ }
140+
141+ public static function only ($ items ): array
142+ {
143+ $ inputs = self ::all ();
144+ return array_intersect_key ($ inputs , array_flip ((array ) $ items ));
47145 }
48146
147+ public static function except ($ items ): array
148+
149+ {
150+ $ inputs = self ::all ();
151+ return array_diff_key ($ inputs , array_flip ((array ) $ items ));
152+ }
153+
154+ public static function setParams ($ matches ): void
155+ {
156+ $ matches = array_intersect_key ($ matches , array_flip (array_filter (array_keys ($ matches ), 'is_string ' )));
157+ self ::$ params = $ matches ;
158+ }
49159
50160 public static function validate ($ rules )
51161 {
52162 $ validatedData = [];
53163 $ errors = [];
164+ $ existingModel = null ;
54165
55166 foreach ($ rules as $ field => $ rule ) {
56167 $ value = self ::input ($ field );
@@ -75,6 +186,52 @@ public static function validate($rules)
75186 if ($ ruleName === 'pattern ' && !preg_match ($ ruleParameter , $ value )) {
76187 $ errors [$ field ][] = 'The ' . $ field . ' format is invalid. ' ;
77188 }
189+
190+ if ($ ruleName === 'unique ' ) {
191+ $ model = new Model ();
192+ $ model ->table ($ ruleParameter );
193+ $ existing = $ model ->find ($ field , $ value );
194+
195+
196+ if ($ existing ->single ()) {
197+ $ errors [$ field ][] = 'The ' . $ field . ' already exists in ' . $ ruleParameter ;
198+ }
199+ }
200+
201+ if ($ ruleName === 'exists ' ) {
202+ $ model = new Model ();
203+ $ model ->table ($ ruleParameter );
204+ $ existing = $ model ->find ($ field , $ value );
205+
206+ $ existingModel = $ existing ->single ();
207+
208+ if (!$ existing ->single ()) {
209+ $ errors [$ field ][] = 'The ' . $ field . ' does not exist in ' . $ ruleParameter ;
210+ }
211+ }
212+
213+ if ($ ruleName === 'verify ' ) {
214+ $ result = \app \core \utils \Password::verify ($ value , $ existingModel ['password ' ]);
215+
216+ if (!$ result ) {
217+ $ errors [$ field ][] = 'The password is incorrect. ' ;
218+ }
219+ }
220+
221+
222+ if ($ ruleName === 'confirmed ' ) {
223+ $ confirmValue = self ::input ($ ruleParameter );
224+ if ($ value !== $ confirmValue ) {
225+ $ errors [$ field ][] = 'The ' . $ field . ' does not match the ' . $ ruleParameter . '. ' ;
226+ }
227+ }
228+
229+ if ($ ruleName === 'in ' ) {
230+ $ allowedValues = explode (', ' , $ ruleParameter );
231+ if (!in_array ($ value , $ allowedValues )) {
232+ $ errors [$ field ][] = 'The ' . $ field . ' must be one of the following: ' . $ ruleParameter ;
233+ }
234+ }
78235 } else {
79236 if ($ rulePart === 'required ' && empty ($ value )) {
80237 $ errors [$ field ][] = 'The ' . $ field . ' field is required. ' ;
@@ -95,6 +252,27 @@ public static function validate($rules)
95252 }
96253 }
97254
255+ // if there's any error in specific field, add the validation/error from session
256+ foreach ($ errors as $ field => $ messages ) {
257+ foreach ($ messages as $ message ) {
258+ addValidationError ($ field , $ message );
259+ }
260+ }
261+
262+ // if there's no error in specific field, remove the validation/error from session
263+ foreach ($ validatedData as $ field => $ value ) {
264+ if (empty ($ errors [$ field ])) {
265+ removeValidationError ($ field );
266+ } else {
267+
268+ // if there's an error again, put the error message back into the session
269+ foreach ($ errors [$ field ] as $ message ) {
270+ addValidationError ($ field , $ message );
271+ }
272+ }
273+ }
274+
275+
98276 if (!empty ($ errors )) {
99277 throw new \Exception (json_encode ($ errors ));
100278 }
0 commit comments