personaRepository->fetchMissing(); $this->addMissingPropietarios($ruts); } catch (Implement\Exception\EmptyResult | Read | Create) {} return array_map([$this, 'process'], $this->personaRepository->fetchAll($orderBy)); } catch (Implement\Exception\EmptyResult) { return []; } } /** * @param int $rut * @return Model\Persona * @throws Read|Create */ public function getById(int $rut): Model\Persona { try { return $this->process($this->personaRepository->fetchById($rut)); } catch (Implement\Exception\EmptyResult) { 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 { $persona = $this->personaRepository->fetchById($data['rut']); } catch (Implement\Exception\EmptyResult) { try { $propietario = $this->propietarioRepository->fetchById($data['rut']); } catch (Implement\Exception\EmptyResult $exception) { throw new Create(__CLASS__, $exception); } $data['rut'] = $propietario->rut; $data['digito'] = $propietario->dv; $data['nombres'] = $propietario->nombres; $data['apellido_paterno'] = $propietario->apellidos['paterno']; $data['apellido_materno'] = $propietario->apellidos['materno'] ?? ''; $persona = $this->personaRepository->create($data); 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]; if (isset($data['direccion_id'])) { $datosData['direccion_id'] = $data['direccion_id']; } if (isset($data['email'])) { $datosData['email'] = $data['email']; } if (isset($data['telefono'])) { $datosData['telefono'] = $data['telefono']; } try { $datos = $this->datosPersonaRepository->fetchByPersona($persona->rut); $this->datosPersonaRepository->edit($datos, $data); } catch (Implement\Exception\EmptyResult) { $datos = $this->datosPersonaRepository->create($datosData); try { $this->datosPersonaRepository->save($datos); } catch (PDOException $exception) { throw new Create(__CLASS__, $exception); } } } return $this->process($persona); } /** * @param array $ruts * @param bool $load * @return array|null * @throws Create * @throws Read */ public function addMissingPropietarios(array $ruts, bool $load = false): ?array { try { $propietarios = $this->propietarioRepository->fetchByRuts($ruts); } catch (Implement\Exception\EmptyResult $exception) { throw new Read(__CLASS__, $exception); } $data = []; $datos = []; foreach ($propietarios as $propietario) { $data []= [ 'rut' => $propietario->rut, 'digito' => $propietario->dv, 'nombres' => $propietario->nombres, 'apellido_paterno' => $propietario->apellidos['paterno'], 'apellido_materno' => $propietario->apellidos['materno'] ?? '', ]; $datos []= [ 'persona_rut' => $propietario->rut, 'direccion_id' => $propietario->datos?->direccion_id, 'email' => $propietario->datos?->email, 'telefono' => $propietario->datos?->telefono, ]; } try { $personas = $this->personaRepository->saveMissing($data); } catch (PDOException|Implement\Exception\EmptyResult $exception) { throw new Create(__CLASS__, $exception); } $personasRuts = array_map(function(Model\Persona $persona) { return $persona->rut; }, $personas); $datos = array_filter($datos, function($row) use ($personasRuts) { return in_array($row['persona_rut'], $personasRuts); }); try { $this->datosPersonaRepository->saveMissing($datos); } catch (PDOException $exception) { throw new Create(__CLASS__, $exception); } if ($load) { return array_map([$this, 'process'], $personas); } return null; } public function edit(Model\Persona $persona, array $data): Model\Persona { $filteredData = $this->personaRepository->filterData($data); try { $datosData = $this->datosPersonaRepository->filterData($data); $this->datosPersonaRepository->edit($persona->datos(), $datosData); return $this->personaRepository->edit($persona, $filteredData); } catch (Implement\Exception\EmptyResult) { return $persona; } } protected function process(Model\Persona $persona): Model\Persona { $persona->addFactory('datos', (new Implement\Repository\Factory()) ->setCallable([$this->datosPersonaRepository, 'fetchByPersona']) ->setArgs(['persona_rut' => $persona->rut])); return $persona; } }