|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Base Class. |
|
7
|
|
|
*/ |
|
8
|
|
|
abstract class Base |
|
9
|
|
|
{ |
|
10
|
|
|
const API_VERSION = '17.04'; |
|
11
|
|
|
|
|
12
|
|
|
const USER_NOT_FOUND = 'El usuario solicitado no existe.'; |
|
13
|
|
|
const USER_NAME_NOT_FOUND = 'No se encontraron usuarios con ese nombre.'; |
|
14
|
|
|
const USER_NAME_REQUIRED = 'Ingrese el nombre del usuario.'; |
|
15
|
|
|
const USER_INFO_REQUIRED = 'Ingrese los datos a actualizar del usuario.'; |
|
16
|
|
|
const USER_DELETED = 'El usuario fue eliminado correctamente.'; |
|
17
|
|
|
const USER_EMAIL_INVALID = 'El email ingresado no es correcto.'; |
|
18
|
|
|
|
|
19
|
|
|
const TASK_NOT_FOUND = 'La tarea solicitada no existe.'; |
|
20
|
|
|
const TASK_NAME_NOT_FOUND = 'No se encontraron tareas con ese nombre.'; |
|
21
|
|
|
const TASK_NAME_REQUIRED = 'Ingrese el nombre de la tarea.'; |
|
22
|
|
|
const TASK_INFO_REQUIRED = 'Ingrese los datos a actualizar de la tarea.'; |
|
23
|
|
|
const TASK_DELETED = 'La tarea fue eliminada correctamente.'; |
|
24
|
|
|
|
|
25
|
|
|
protected $database; |
|
26
|
|
|
protected $request; |
|
27
|
|
|
protected $response; |
|
28
|
|
|
protected $args; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param Request $request |
|
32
|
|
|
* @param Response $response |
|
33
|
|
|
* @param array $args |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function setParams($request, $response, $args) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->request = $request; |
|
38
|
|
|
$this->response = $response; |
|
39
|
|
|
$this->args = $args; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Send response with json as standard format. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $status |
|
46
|
|
|
* @param mixed $message |
|
47
|
|
|
* @param int $code |
|
48
|
|
|
* @return array $response |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function jsonResponse($status, $message, $code) |
|
51
|
|
|
{ |
|
52
|
|
|
$result = [ |
|
53
|
|
|
'code' => $code, |
|
54
|
|
|
'status' => $status, |
|
55
|
|
|
'message' => $message, |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
return $this->response->withJson($result, $code, JSON_PRETTY_PRINT); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Validate and sanitize a email address. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $emailValue |
|
65
|
|
|
* @return string |
|
66
|
|
|
* @throws \Exception |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function validateEmail($emailValue) |
|
69
|
|
|
{ |
|
70
|
|
|
$email = filter_var($emailValue, FILTER_SANITIZE_EMAIL); |
|
71
|
|
|
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { |
|
72
|
|
|
throw new \Exception(self::USER_EMAIL_INVALID, 400); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $email; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|