C. Carpeta « srv / modelo »

Versión para imprimir.

1. srv / modelo / Pasatiempo.php

1<?php
2
3class Pasatiempo
4{
5 public string $uuid;
6 public string $nombre;
7 public int $modificacion;
8 public bool $eliminado;
9
10 public function __construct(
11 string $nombre = "",
12 string $uuid = "",
13 int $modificacion = 0,
14 bool $eliminado = true
15 ) {
16 $this->nombre = $nombre;
17 $this->uuid = $uuid;
18 $this->modificacion = $modificacion;
19 $this->eliminado = $eliminado;
20 }
21
22 public function valida()
23 {
24 if ($this->uuid === "")
25 throw new Exception("Falta el uuid.");
26 if ($this->nombre === "")
27 throw new Exception("Falta el nombre.");
28 }
29}
30

2. srv / modelo / recuperaPasatiempo.php

1<?php
2
3require_once __DIR__ . "/../../lib/php/BAD_REQUEST.php";
4require_once __DIR__ . "/../../lib/php/validaJson.php";
5require_once __DIR__ . "/../../lib/php/ProblemDetails.php";
6require_once __DIR__ . "/Pasatiempo.php";
7
8function recuperaPasatiempo($objeto)
9{
10
11 $objeto = validaJson($objeto);
12
13 if (!isset($objeto->nombre) || !is_string($objeto->nombre))
14 throw new ProblemDetails(
15 status: BAD_REQUEST,
16 title: "El nombre debe ser texto.",
17 type: "/error/nombreincorrecto.html",
18 );
19
20 if (!isset($objeto->uuid) || !is_string($objeto->uuid))
21 throw new ProblemDetails(
22 status: BAD_REQUEST,
23 title: "El uuid debe ser texto.",
24 type: "/error/uuidincorrecto.html",
25 );
26
27 if (!isset($objeto->eliminado) || !is_bool($objeto->eliminado))
28 throw new ProblemDetails(
29 status: BAD_REQUEST,
30 title: "El campo eliminado debe ser booleano.",
31 type: "/error/eliminadoincorrecto.html",
32 );
33
34 if (!isset($objeto->modificacion) || !is_int($objeto->modificacion))
35 throw new ProblemDetails(
36 status: BAD_REQUEST,
37 title: "La modificacion debe ser número.",
38 type: "/error/modificacionincorrecta.html",
39 );
40
41 return new Pasatiempo(
42 uuid: $objeto->uuid,
43 nombre: $objeto->nombre,
44 modificacion: $objeto->modificacion,
45 eliminado: $objeto->eliminado
46 );
47}
48