2. srv / modelo / Producto.php

1<?php
2
3require_once __DIR__ . "/../../lib/php/ProblemDetails.php";
4require_once __DIR__ . "/../../lib/php/validaNombre.php";
5
6class Producto
7{
8
9 public int $id;
10 public string $nombre;
11 public float $existencias;
12 public float $precio;
13
14 public function __construct(
15 string $nombre = "",
16 float $existencias = NAN,
17 float $precio = NAN,
18 int $id = 0
19 ) {
20 $this->id = $id;
21 $this->nombre = $nombre;
22 $this->existencias = $existencias;
23 $this->precio = $precio;
24 }
25
26 public function valida()
27 {
28 validaNombre($this->nombre);
29 if (is_nan($this->existencias))
30 throw new ProblemDetails(
31 status: ProblemDetails::BadRequest,
32 type: "/error/existenciasincorrectas.html",
33 title: "Las existencias no pueden ser NAN.",
34 );
35 if (is_nan($this->precio))
36 throw new ProblemDetails(
37 status: ProblemDetails::BadRequest,
38 type: "/error/precioincorrecto.html",
39 title: "El precio no puede ser NAN.",
40 );
41 }
42}
43
skip_previous skip_next