From f3e15b34a8afc99839053738dcb65fd00999e212 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Wed, 7 May 2025 19:24:33 -0400 Subject: [PATCH] Persona conectada con Propietario --- app/src/Model/Persona.php | 15 +++++++++++++++ app/src/Service/Persona.php | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/app/src/Model/Persona.php b/app/src/Model/Persona.php index dd28c05..35be13d 100644 --- a/app/src/Model/Persona.php +++ b/app/src/Model/Persona.php @@ -1,6 +1,7 @@ datos; } + public function __get(string $name): mixed + { + if (property_exists($this, $name)) { + return $this->{$name}; + } + if ($name === 'datos') { + return $this->datos(); + } + if ($name === 'dv') { + return $this->digito; + } + throw new InvalidArgumentException("Property {$name} is not found in " . __CLASS__); + } + public function jsonSerialize(): mixed { return [ diff --git a/app/src/Service/Persona.php b/app/src/Service/Persona.php index c2647e4..5e443b5 100644 --- a/app/src/Service/Persona.php +++ b/app/src/Service/Persona.php @@ -1,12 +1,14 @@ process($this->personaRepository->fetchById($rut)); } catch (Implement\Exception\EmptyResult $exception) { - throw new Read(__CLASS__, $exception); + try { + $this->propietarioRepository->fetchById($rut); + return $this->add(compact('rut')); + } catch (Implement\Exception\EmptyResult $exception) { + throw new Read(__CLASS__, $exception); + } } } + + /** + * @param array $data + * @return Model\Persona + * @throws Create + */ public function add(array $data): Model\Persona { try { @@ -42,9 +55,19 @@ class Persona extends Ideal\Service $data['apellido_paterno'] = $propietario->apellidos['paterno']; $data['apellido_materno'] = $propietario->apellidos['materno']; $data['direccion_id'] = $propietario->datos->direccion->id; + if (isset($propietario->datos?->email)) { + $data['email'] = $propietario->datos->email; + } + if (isset($propietario->datos?->telefono)) { + $data['telefono'] = $propietario->datos->telefono; + } } catch (Implement\Exception\EmptyResult) {} $persona = $this->personaRepository->create($data); - $persona = $this->personaRepository->save($persona); + try { + $persona = $this->personaRepository->save($persona); + } catch (PDOException $exception) { + throw new Create(__CLASS__, $exception); + } } if (isset($data['direccion_id']) or isset($data['email']) or isset($data['telefono'])) { $datosData = ['persona_rut' => $persona->rut]; @@ -62,7 +85,11 @@ class Persona extends Ideal\Service $this->datosPersonaRepository->edit($datos, $data); } catch (Implement\Exception\EmptyResult) { $datos = $this->datosPersonaRepository->create($datosData); - $this->datosPersonaRepository->save($datos); + try { + $this->datosPersonaRepository->save($datos); + } catch (PDOException $exception) { + throw new Create(__CLASS__, $exception); + } } } return $this->process($persona);