1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Respect\Validation\Validator as v; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base Class. |
9
|
|
|
*/ |
10
|
|
|
abstract class Base |
11
|
|
|
{ |
12
|
|
|
const API_VERSION = '17.04'; |
13
|
|
|
|
14
|
|
|
const USER_NOT_FOUND = 'El usuario solicitado no existe.'; |
15
|
|
|
const USER_NAME_NOT_FOUND = 'No se encontraron usuarios con ese nombre.'; |
16
|
|
|
const USER_NAME_REQUIRED = 'Ingrese el nombre del usuario.'; |
17
|
|
|
const USER_INFO_REQUIRED = 'Ingrese los datos a actualizar del usuario.'; |
18
|
|
|
const USER_DELETED = 'El usuario fue eliminado correctamente.'; |
19
|
|
|
const USER_NAME_INVALID = 'El nombre ingresado no es correcto.'; |
20
|
|
|
const USER_EMAIL_INVALID = 'El email ingresado no es correcto.'; |
21
|
|
|
|
22
|
|
|
const TASK_NOT_FOUND = 'La tarea solicitada no existe.'; |
23
|
|
|
const TASK_NAME_NOT_FOUND = 'No se encontraron tareas con ese nombre.'; |
24
|
|
|
const TASK_NAME_REQUIRED = 'Ingrese el nombre de la tarea.'; |
25
|
|
|
const TASK_INFO_REQUIRED = 'Ingrese los datos a actualizar de la tarea.'; |
26
|
|
|
const TASK_DELETED = 'La tarea fue eliminada correctamente.'; |
27
|
|
|
|
28
|
|
|
protected $database; |
29
|
|
|
protected $request; |
30
|
|
|
protected $response; |
31
|
|
|
protected $args; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Request $request |
35
|
|
|
* @param Response $response |
36
|
|
|
* @param array $args |
37
|
|
|
*/ |
38
|
|
|
protected function setParams($request, $response, $args) |
39
|
|
|
{ |
40
|
|
|
$this->request = $request; |
41
|
|
|
$this->response = $response; |
42
|
|
|
$this->args = $args; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Send response with json as standard format. |
47
|
|
|
* |
48
|
|
|
* @param string $status |
49
|
|
|
* @param mixed $message |
50
|
|
|
* @param int $code |
51
|
|
|
* @return array $response |
52
|
|
|
*/ |
53
|
|
|
protected function jsonResponse($status, $message, $code) |
54
|
|
|
{ |
55
|
|
|
$result = [ |
56
|
|
|
'code' => $code, |
57
|
|
|
'status' => $status, |
58
|
|
|
'message' => $message, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
return $this->response->withJson($result, $code, JSON_PRETTY_PRINT); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Validate and sanitize a user input. |
66
|
|
|
* |
67
|
|
|
* @param array $input |
68
|
|
|
* @return string |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
protected function validateInput($input) |
72
|
|
|
{ |
73
|
|
|
if (!isset($input['name'])) { |
74
|
|
|
throw new \Exception(self::USER_NAME_REQUIRED, 400); |
75
|
|
|
} |
76
|
|
|
$name = $this->validateName($input['name']); |
77
|
|
|
$email = null; |
78
|
|
|
if (isset($input['email'])) { |
79
|
|
|
$email = $this->validateEmail($input['email']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return ['name' => $name, 'email' => $email]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Validate and sanitize a user input. |
87
|
|
|
* |
88
|
|
|
* @param array $input |
89
|
|
|
* @param object $user |
90
|
|
|
* @return string |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
protected function validateInputUpdate($input, $user) |
94
|
|
|
{ |
95
|
|
|
if (!isset($input['name']) && !isset($input['email'])) { |
96
|
|
|
throw new \Exception(self::USER_INFO_REQUIRED, 400); |
97
|
|
|
} |
98
|
|
|
$name = $user->name; |
99
|
|
|
if (isset($input['name'])) { |
100
|
|
|
$name = $this->validateName($input['name']); |
101
|
|
|
} |
102
|
|
|
$email = $user->email; |
103
|
|
|
if (isset($input['email'])) { |
104
|
|
|
$email = $this->validateEmail($input['email']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return ['name' => $name, 'email' => $email]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Validate and sanitize a username. |
112
|
|
|
* |
113
|
|
|
* @param string $name |
114
|
|
|
* @return string |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
|
|
protected function validateName($name) |
118
|
|
|
{ |
119
|
|
|
if (!v::alnum()->length(2, 100)->validate($name)) { |
120
|
|
|
throw new \Exception(self::USER_NAME_INVALID, 400); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Validate and sanitize a email address. |
128
|
|
|
* |
129
|
|
|
* @param string $emailValue |
130
|
|
|
* @return string |
131
|
|
|
* @throws \Exception |
132
|
|
|
*/ |
133
|
|
|
protected function validateEmail($emailValue) |
134
|
|
|
{ |
135
|
|
|
$email = filter_var($emailValue, FILTER_SANITIZE_EMAIL); |
136
|
|
|
if (!v::email()->validate($email)) { |
137
|
|
|
throw new \Exception(self::USER_EMAIL_INVALID, 400); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $email; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|