A. Carpeta « srv / modelo »

Versión para imprimir.

1. srv / modelo / DetalleDeVenta.php

1<?php
2
3require_once __DIR__ . "/../../lib/php/ProblemDetails.php";
4require_once __DIR__ . "/Venta.php";
5require_once __DIR__ . "/Producto.php";
6
7class DetalleDeVenta
8{
9
10 public ?Venta $venta;
11 public ?Producto $producto;
12 public float $cantidad;
13 public float $precio;
14
15 public function __construct(
16 float $cantidad = NAN,
17 float $precio = NAN,
18 ?Producto $producto = null,
19 ?Venta $venta = null
20 ) {
21 $this->cantidad = $cantidad;
22 $this->precio = $precio;
23 $this->producto = $producto;
24 $this->venta = $venta;
25 }
26
27 public function valida()
28 {
29 if ($this->producto === null)
30 throw new Exception("Detalle de venta sin producto.");
31 if (is_nan($this->cantidad))
32 throw new ProblemDetails(
33 status: ProblemDetails::BadRequest,
34 type: "/error/cantidadincorrecta.html",
35 title: "La cantidad no puede ser NAN.",
36 );
37 if (is_nan($this->precio))
38 throw new ProblemDetails(
39 status: ProblemDetails::BadRequest,
40 type: "/error/precioincorrecto.html",
41 title: "El precio no puede ser NAN.",
42 );
43 }
44}
45

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

3. srv / modelo / Venta.php

1<?php
2
3require_once __DIR__ . "/../../lib/php/ProblemDetails.php";
4require_once __DIR__ . "/DetalleDeVenta.php";
5
6class Venta
7{
8
9 public int $id;
10 public bool $enCaptura;
11 /** @var DetalleDeVenta[] */
12 public array $detalles;
13
14 public function __construct(
15 bool $enCaptura = false,
16 array $detalles = [],
17 int $id = 0
18 ) {
19 $this->id = $id;
20 $this->enCaptura = $enCaptura;
21 $this->detalles = $detalles;
22 }
23
24 public function valida()
25 {
26 foreach ($this->detalles as $detalle) {
27 if (!($detalle instanceof DetalleDeVenta))
28 throw new ProblemDetails(
29 status: ProblemDetails::BadRequest,
30 type: "/error/detalledeventaincorrecto.html",
31 title: "Tipo incorrecto para un etalle de venta.",
32 );
33 $detalle->valida();
34 }
35 }
36}
37