| 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 = $input['name']; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $usernameValidator = v::alnum()->length(1, 100); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         if (!$usernameValidator->validate($name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             throw new \Exception(self::USER_NAME_INVALID, 400); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $email = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         if (isset($input['email'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             $email = $this->validateEmail($input['email']); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $data = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             'name' => $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             'email' => $email, | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         return $data; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * Validate and sanitize a email address. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param string $emailValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     protected function validateEmail($emailValue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $email = filter_var($emailValue, FILTER_SANITIZE_EMAIL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if (!v::email()->validate($email)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             throw new \Exception(self::USER_EMAIL_INVALID, 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |