1 | <?php |
2 | |
3 | const BAD_REQUEST = 400; |
4 |
1 | <?php |
2 | |
3 | function calculaArregloDeParametros(array $arreglo) |
4 | { |
5 | $parametros = []; |
6 | foreach ($arreglo as $llave => $valor) { |
7 | $parametros[":$llave"] = $valor; |
8 | } |
9 | return $parametros; |
10 | } |
11 |
1 | <?php |
2 | |
3 | function calculaSqlDeAsignaciones(string $separador, array $arreglo) |
4 | { |
5 | $primerElemento = true; |
6 | $sqlDeAsignacion = ""; |
7 | foreach ($arreglo as $llave => $valor) { |
8 | $sqlDeAsignacion .= |
9 | ($primerElemento === true ? "" : $separador) . "$llave=:$llave"; |
10 | $primerElemento = false; |
11 | } |
12 | return $sqlDeAsignacion; |
13 | } |
14 |
1 | <?php |
2 | |
3 | function calculaSqlDeCamposDeInsert(array $values) |
4 | { |
5 | $primerCampo = true; |
6 | $sqlDeCampos = ""; |
7 | foreach ($values as $nombreDeValue => $valorDeValue) { |
8 | $sqlDeCampos .= ($primerCampo === true ? "" : ",") . "$nombreDeValue"; |
9 | $primerCampo = false; |
10 | } |
11 | return $sqlDeCampos; |
12 | } |
13 |
1 | <?php |
2 | |
3 | function calculaSqlDeValues(array $values) |
4 | { |
5 | $primerValue = true; |
6 | $sqlDeValues = ""; |
7 | foreach ($values as $nombreDeValue => $valorDeValue) { |
8 | $sqlDeValues .= ($primerValue === true ? "" : ",") . ":$nombreDeValue"; |
9 | $primerValue = false; |
10 | } |
11 | return $sqlDeValues; |
12 | } |
13 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/calculaArregloDeParametros.php"; |
4 | require_once __DIR__ . "/calculaSqlDeAsignaciones.php"; |
5 | |
6 | function delete(PDO $pdo, string $from, array $where) |
7 | { |
8 | $sql = "DELETE FROM $from"; |
9 | |
10 | if (sizeof($where) === 0) { |
11 | $pdo->exec($sql); |
12 | } else { |
13 | $sqlDeWhere = calculaSqlDeAsignaciones(" AND ", $where); |
14 | $sql .= " WHERE $sqlDeWhere"; |
15 | |
16 | $statement = $pdo->prepare($sql); |
17 | $parametros = calculaArregloDeParametros($where); |
18 | $statement->execute($parametros); |
19 | } |
20 | } |
21 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
4 | |
5 | function devuelveCreated($urlDelNuevo, $resultado) |
6 | { |
7 | |
8 | $json = json_encode($resultado); |
9 | |
10 | if ($json === false) { |
11 | |
12 | devuelveResultadoNoJson(); |
13 | } else { |
14 | |
15 | http_response_code(201); |
16 | header("Location: {$urlDelNuevo}"); |
17 | header("Content-Type: application/json"); |
18 | echo $json; |
19 | } |
20 | } |
21 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
4 | require_once __DIR__ . "/devuelveProblemDetails.php"; |
5 | require_once __DIR__ . "/devuelveProblemDetails.php"; |
6 | |
7 | function devuelveErrorInterno(Throwable $error) |
8 | { |
9 | devuelveProblemDetails(new ProblemDetails( |
10 | status: INTERNAL_SERVER_ERROR, |
11 | title: $error->getMessage(), |
12 | type: "/error/errorinterno.html" |
13 | )); |
14 | } |
15 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
4 | |
5 | function devuelveJson($resultado) |
6 | { |
7 | |
8 | $json = json_encode($resultado); |
9 | |
10 | if ($json === false) { |
11 | |
12 | devuelveResultadoNoJson(); |
13 | } else { |
14 | |
15 | http_response_code(200); |
16 | header("Content-Type: application/json"); |
17 | echo $json; |
18 | } |
19 | } |
20 |
1 | <?php |
2 | |
3 | function devuelveNoContent() |
4 | { |
5 | http_response_code(204); |
6 | } |
7 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
4 | require_once __DIR__ . "/ProblemDetails.php"; |
5 | |
6 | function devuelveProblemDetails(ProblemDetails $details) |
7 | { |
8 | |
9 | $body = ["title" => $details->title]; |
10 | if ($details->type !== null) { |
11 | $body["type"] = $details->type; |
12 | } |
13 | if ($details->detail !== null) { |
14 | $body["detail"] = $details->detail; |
15 | } |
16 | |
17 | $json = json_encode($body); |
18 | |
19 | if ($json === false) { |
20 | |
21 | devuelveResultadoNoJson(); |
22 | } else { |
23 | |
24 | http_response_code($details->status); |
25 | header("Content-Type: application/problem+json"); |
26 | echo $json; |
27 | } |
28 | } |
29 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
4 | |
5 | function devuelveResultadoNoJson() |
6 | { |
7 | |
8 | http_response_code(INTERNAL_SERVER_ERROR); |
9 | header("Content-Type: application/problem+json"); |
10 | echo '{' . |
11 | '"title": "El resultado no puede representarse como JSON."' . |
12 | '"type": "/error/resultadonojson.html"' . |
13 | '}'; |
14 | } |
15 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/ProblemDetails.php"; |
4 | require_once __DIR__ . "/devuelveProblemDetails.php"; |
5 | require_once __DIR__ . "/devuelveErrorInterno.php"; |
6 | |
7 | function ejecutaServicio(callable $codigo) |
8 | { |
9 | try { |
10 | $codigo(); |
11 | } catch (ProblemDetails $details) { |
12 | devuelveProblemDetails($details); |
13 | } catch (Throwable $error) { |
14 | devuelveErrorInterno($error); |
15 | } |
16 | } |
17 |
1 | <?php |
2 | |
3 | function fetch( |
4 | PDOStatement|false $statement, |
5 | $parametros = [], |
6 | int $mode = PDO::FETCH_ASSOC, |
7 | $opcional = null |
8 | ) { |
9 | |
10 | if ($statement === false) { |
11 | |
12 | return false; |
13 | } else { |
14 | |
15 | if (sizeof($parametros) > 0) { |
16 | $statement->execute($parametros); |
17 | } |
18 | |
19 | if ($opcional === null) { |
20 | return $statement->fetch($mode); |
21 | } else { |
22 | $statement->setFetchMode($mode, $opcional); |
23 | return $statement->fetch(); |
24 | } |
25 | } |
26 | } |
27 |
1 | <?php |
2 | |
3 | function fetchAll( |
4 | PDOStatement|false $statement, |
5 | $parametros = [], |
6 | int $mode = PDO::FETCH_ASSOC, |
7 | $opcional = null |
8 | ): array { |
9 | |
10 | if ($statement === false) { |
11 | |
12 | return []; |
13 | } else { |
14 | |
15 | if (sizeof($parametros) > 0) { |
16 | $statement->execute($parametros); |
17 | } |
18 | |
19 | $resultado = $opcional === null |
20 | ? $statement->fetchAll($mode) |
21 | : $statement->fetchAll($mode, $opcional); |
22 | |
23 | if ($resultado === false) { |
24 | return []; |
25 | } else { |
26 | return $resultado; |
27 | } |
28 | } |
29 | } |
30 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/calculaSqlDeCamposDeInsert.php"; |
4 | require_once __DIR__ . "/calculaSqlDeValues.php"; |
5 | require_once __DIR__ . "/calculaArregloDeParametros.php"; |
6 | |
7 | function insert(PDO $pdo, string $into, array $values) |
8 | { |
9 | $sqlDeCampos = calculaSqlDeCamposDeInsert($values); |
10 | $sqlDeValues = calculaSqlDeValues($values); |
11 | $sql = "INSERT INTO $into ($sqlDeCampos) VALUES ($sqlDeValues)"; |
12 | $parametros = calculaArregloDeParametros($values); |
13 | $pdo->prepare($sql)->execute($parametros); |
14 | } |
15 |
1 | <?php |
2 | |
3 | const INTERNAL_SERVER_ERROR = 500; |
1 | <?php |
2 | |
3 | const NOT_FOUND = 404; |
4 |
1 | <?php |
2 | |
3 | /** Detalle de los errores devueltos por un servicio. */ |
4 | class ProblemDetails extends Exception |
5 | { |
6 | |
7 | public int $status; |
8 | public string $title; |
9 | public ?string $type; |
10 | public ?string $detail; |
11 | |
12 | public function __construct( |
13 | int $status, |
14 | string $title, |
15 | ?string $type = null, |
16 | ?string $detail = null, |
17 | Throwable $previous = null |
18 | ) { |
19 | parent::__construct($title, $status, $previous); |
20 | $this->status = $status; |
21 | $this->type = $type; |
22 | $this->title = $title; |
23 | $this->detail = $detail; |
24 | } |
25 | } |
26 |
1 | <?php |
2 | |
3 | use function PHPSTORM_META\type; |
4 | |
5 | require_once __DIR__ . "/BAD_REQUEST.php"; |
6 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
7 | require_once __DIR__ . "/ProblemDetails.php"; |
8 | |
9 | function recuperaBytes(string $parametro): false|string |
10 | { |
11 | if (isset($_FILES[$parametro])) { |
12 | $path = $_FILES[$parametro]["tmp_name"]; |
13 | |
14 | if ($path === "") { |
15 | return ""; |
16 | } elseif (is_uploaded_file($path)) { |
17 | |
18 | $contents = file_get_contents($path); |
19 | |
20 | if ($contents === false) { |
21 | |
22 | switch ($_FILES[$parametro]['error']) { |
23 | |
24 | case UPLOAD_ERR_OK: |
25 | |
26 | return $contents; |
27 | |
28 | case UPLOAD_ERR_INI_SIZE: |
29 | case UPLOAD_ERR_FORM_SIZE: |
30 | |
31 | throw new ProblemDetails( |
32 | status: BAD_REQUEST, |
33 | title: "Archivo demasiado largo.", |
34 | type: "/error/archivodemasiadolargo.html", |
35 | detail: "El archivo " - $parametro . |
36 | " excede el tamaño máximo que el servidor puede recibir." |
37 | ); |
38 | |
39 | case UPLOAD_ERR_PARTIAL: |
40 | |
41 | throw new ProblemDetails( |
42 | status: INTERNAL_SERVER_ERROR, |
43 | title: "Carga incompleta de archivo.", |
44 | type: "/error/archivocargaincompleta.html", |
45 | detail: "Por una razón desconocida, el archivo " - $parametro . |
46 | " no se cargó completamente." |
47 | ); |
48 | |
49 | case UPLOAD_ERR_NO_FILE: |
50 | |
51 | throw creaArchivoNoEnviado($parametro); |
52 | |
53 | case UPLOAD_ERR_NO_TMP_DIR: |
54 | |
55 | throw new ProblemDetails( |
56 | status: INTERNAL_SERVER_ERROR, |
57 | title: "Falta la carpeta temporal.", |
58 | type: "/error/faltacarpetatemporal.html", |
59 | detail: "Por una razón desconocida, falta la carpeta temporal " . |
60 | "para cargar el archivo $parametro.", |
61 | ); |
62 | |
63 | case UPLOAD_ERR_CANT_WRITE: |
64 | |
65 | throw new ProblemDetails( |
66 | status: INTERNAL_SERVER_ERROR, |
67 | title: "El archivo no se guardó.", |
68 | type: "/error/archivonoguardado.html", |
69 | detail: "Por una razón desconocida, el archivo " - $parametro . |
70 | " no se pudo guardar en disco.", |
71 | ); |
72 | |
73 | case UPLOAD_ERR_EXTENSION: |
74 | |
75 | throw new ProblemDetails( |
76 | status: BAD_REQUEST, |
77 | title: "Extensión no permitida.", |
78 | type: "/error/extensionprohibida.html", |
79 | detail: "La extensión del archivo " - $parametro . |
80 | " no está permitida en el servidor." |
81 | ); |
82 | |
83 | default: |
84 | |
85 | throw new Exception("Error no identificado recuperando el archivo " . |
86 | $parametro . "."); |
87 | } |
88 | } else { |
89 | |
90 | return $contents; |
91 | } |
92 | } else { |
93 | |
94 | throw creaArchivoNoEnviado($parametro); |
95 | } |
96 | } else { |
97 | return false; |
98 | } |
99 | } |
100 | |
101 | function creaArchivoNoEnviado(string $parametro) |
102 | { |
103 | return new ProblemDetails( |
104 | status: BAD_REQUEST, |
105 | title: "Archivo no enviado.", |
106 | type: "/error/archivonoenviado.html", |
107 | detail: "El archivo $parametro no fué recibido por el servidor." |
108 | ); |
109 | } |
110 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/recuperaTexto.php"; |
4 | |
5 | /** |
6 | * Devuelve el valor entero de un parámetro recibido en el |
7 | * servidor por medio de GET, POST o cookie. |
8 | * |
9 | * Si el parámetro no se recibe, devuekve false |
10 | * |
11 | * Si se recibe una cadena vacía, se devuelve null. |
12 | * |
13 | * Si parámetro no se puede convertir a entero, se genera |
14 | * un error. |
15 | */ |
16 | function recuperaEntero(string $parametro): false|null|int |
17 | { |
18 | $valor = recuperaTexto($parametro); |
19 | if ($valor === false) { |
20 | return false; |
21 | } elseif ($valor === "") { |
22 | return null; |
23 | } else { |
24 | return (int) trim($valor); |
25 | } |
26 | } |
27 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/BAD_REQUEST.php"; |
4 | require_once __DIR__ . "/recuperaEntero.php"; |
5 | require_once __DIR__ . "/ProblemDetails.php"; |
6 | |
7 | function recuperaIdEntero(string $parametro): int |
8 | { |
9 | |
10 | $id = recuperaEntero($parametro); |
11 | |
12 | if ($id === false) |
13 | throw new ProblemDetails( |
14 | status: BAD_REQUEST, |
15 | title: "Falta el id.", |
16 | type: "/error/faltaid.html", |
17 | detail: "La solicitud no tiene el valor de id.", |
18 | ); |
19 | |
20 | if ($id === null) |
21 | throw new ProblemDetails( |
22 | status: BAD_REQUEST, |
23 | title: "Id en blanco.", |
24 | type: "/error/idenblanco.html", |
25 | ); |
26 | |
27 | return $id; |
28 | } |
29 |
1 | <?php |
2 | |
3 | /** |
4 | * Recupera el texto de un parámetro enviado al |
5 | * servidor por medio de GET, POST o cookie. |
6 | * |
7 | * Si el parámetro no se recibe, devuelve false. |
8 | */ |
9 | function recuperaTexto(string $parametro): false|string |
10 | { |
11 | /* Si el parámetro está asignado en $_REQUEST, |
12 | * devuelve su valor; de lo contrario, devuelve false. |
13 | */ |
14 | $valor = isset($_REQUEST[$parametro]) |
15 | ? $_REQUEST[$parametro] |
16 | : false; |
17 | return $valor; |
18 | } |
19 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/fetchAll.php"; |
4 | require_once __DIR__ . "/calculaSqlDeAsignaciones.php"; |
5 | |
6 | function select( |
7 | PDO $pdo, |
8 | string $from, |
9 | array $where = [], |
10 | string $orderBy = "", |
11 | int $mode = PDO::FETCH_ASSOC, |
12 | $opcional = null |
13 | ) { |
14 | $sql = "SELECT * FROM $from"; |
15 | |
16 | if (sizeof($where) > 0) { |
17 | $sqlDeWhere = calculaSqlDeAsignaciones(" AND ", $where); |
18 | $sql .= " WHERE $sqlDeWhere"; |
19 | } |
20 | |
21 | if ($orderBy !== "") { |
22 | $sql .= " ORDER BY $orderBy"; |
23 | } |
24 | |
25 | if (sizeof($where) === 0) { |
26 | $statement = $pdo->query($sql); |
27 | return fetchAll($statement, [], $mode, $opcional); |
28 | } else { |
29 | $statement = $pdo->prepare($sql); |
30 | $parametros = calculaArregloDeParametros($where); |
31 | return fetchAll($statement, $parametros, $mode, $opcional); |
32 | } |
33 | } |
34 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/fetch.php"; |
4 | require_once __DIR__ . "/calculaArregloDeParametros.php"; |
5 | require_once __DIR__ . "/calculaSqlDeAsignaciones.php"; |
6 | |
7 | function selectFirst( |
8 | PDO $pdo, |
9 | string $from, |
10 | array $where = [], |
11 | string $orderBy = "", |
12 | int $mode = PDO::FETCH_ASSOC, |
13 | $opcional = null |
14 | ) { |
15 | $sql = "SELECT * FROM $from"; |
16 | |
17 | if (sizeof($where) > 0) { |
18 | $sqlDeWhere = calculaSqlDeAsignaciones(" AND ", $where); |
19 | $sql .= " WHERE $sqlDeWhere"; |
20 | } |
21 | |
22 | if ($orderBy !== "") { |
23 | $sql .= " ORDER BY $orderBy"; |
24 | } |
25 | |
26 | if (sizeof($where) === 0) { |
27 | $statement = $pdo->query($sql); |
28 | return fetch($statement, [], $mode, $opcional); |
29 | } else { |
30 | $statement = $pdo->prepare($sql); |
31 | $parametros = calculaArregloDeParametros($where); |
32 | return fetch($statement, $parametros, $mode, $opcional); |
33 | } |
34 | } |
35 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/calculaArregloDeParametros.php"; |
4 | require_once __DIR__ . "/calculaSqlDeAsignaciones.php"; |
5 | |
6 | |
7 | function update(PDO $pdo, string $table, array $set, array $where) |
8 | { |
9 | $sqlDeSet = calculaSqlDeAsignaciones(",", $set); |
10 | $sqlDeWhere = calculaSqlDeAsignaciones(" AND ", $where); |
11 | $sql = "UPDATE $table SET $sqlDeSet WHERE $sqlDeWhere"; |
12 | |
13 | $parametros = calculaArregloDeParametros($set); |
14 | foreach ($where as $nombreDeWhere => $valorDeWhere) { |
15 | $parametros[":$nombreDeWhere"] = $valorDeWhere; |
16 | } |
17 | $statement = $pdo->prepare($sql); |
18 | $statement->execute($parametros); |
19 | } |
20 |
1 | <?php |
2 | |
3 | require_once __DIR__ . "/BAD_REQUEST.php"; |
4 | require_once __DIR__ . "/ProblemDetails.php"; |
5 | |
6 | function validaNombre(false|string $nombre) |
7 | { |
8 | |
9 | if ($nombre === false) |
10 | throw new ProblemDetails( |
11 | status: BAD_REQUEST, |
12 | title: "Falta el nombre.", |
13 | type: "/error/faltanombre.html", |
14 | detail: "La solicitud no tiene el valor de nombre." |
15 | ); |
16 | |
17 | $trimNombre = trim($nombre); |
18 | |
19 | if ($trimNombre === "") |
20 | throw new ProblemDetails( |
21 | status: BAD_REQUEST, |
22 | title: "Nombre en blanco.", |
23 | type: "/error/nombreenblanco.html", |
24 | detail: "Pon texto en el campo nombre.", |
25 | ); |
26 | |
27 | return $trimNombre; |
28 | } |
29 |