1 | <?php |
||
2 | |||
3 | namespace Example\Controllers; |
||
4 | |||
5 | use Example\Models\User; |
||
6 | use GiovanniALO\DataValidation\Validation; |
||
7 | |||
8 | class TestController |
||
9 | { |
||
10 | public function withCoffeeCodeRouter(): void |
||
11 | { |
||
12 | Validation::validate([ |
||
13 | 'first_name' => ['required', 'string', 'min:2', 'max:20'], |
||
14 | 'last_name' => ['required', 'string', 'min:2', 'max:40'], |
||
15 | 'email' => ['required', 'string', 'email'], |
||
16 | 'password' => ['required', 'string', 'min:8', 'max:40', 'confirmation'], |
||
17 | ]); |
||
18 | |||
19 | echo 'Ok'; |
||
20 | } |
||
21 | |||
22 | public function withCoffeeCodeDataLayer(array $data): void |
||
23 | { |
||
24 | Validation::validate([ |
||
25 | 'first_name' => ['required', 'string', 'min:2', 'max:20'], |
||
26 | 'last_name' => ['required', 'string', 'min:2', 'max:40'], |
||
27 | 'email' => ['required', 'string', 'email', 'unique:'.User::class.',email'], |
||
28 | 'password' => ['required', 'string', 'min:8', 'max:40', 'confirmation'], |
||
29 | ]); |
||
30 | |||
31 | $user = new User(); |
||
32 | $user->first_name = $data['first_name']; |
||
33 | $user->last_name = $data['last_name']; |
||
34 | $user->email = $data['email']; |
||
35 | $user->password = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]); |
||
36 | $saved = $user->save(); |
||
37 | |||
38 | if (!$saved) { |
||
39 | http_response_code(500); |
||
40 | |||
41 | echo json_encode([ |
||
42 | 'message' => 'Erro ao cadastrar usuário.', |
||
43 | 'trace' => $user->fail()->getMessage(), |
||
44 | ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
45 | |||
46 | exit; |
||
0 ignored issues
–
show
|
|||
47 | } |
||
48 | |||
49 | echo json_encode([ |
||
50 | 'message' => 'Usuário cadastrado com sucesso.', |
||
51 | ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
52 | } |
||
53 | } |
||
54 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.