Files
oficial/app/src/Repository/Persona/Datos.php

89 lines
3.8 KiB
PHP
Raw Normal View History

2024-06-18 22:41:03 -04:00
<?php
2024-12-03 16:45:20 -03:00
namespace Incoviba\Repository\Persona;
2024-06-18 22:41:03 -04:00
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
2024-12-03 16:45:20 -03:00
use Incoviba\Repository\Direccion;
use Incoviba\Repository\Persona;
2024-06-18 22:41:03 -04:00
2024-12-03 16:45:20 -03:00
class Datos extends Ideal\Repository
2024-06-18 22:41:03 -04:00
{
public function __construct(Define\Connection $connection, protected Persona $personaRepository,
protected Direccion $direccionRepository)
{
parent::__construct($connection);
$this->setTable('datos_personas');
}
2024-12-03 16:45:20 -03:00
public function create(?array $data = null): Model\Persona\Datos
2024-06-18 22:41:03 -04:00
{
$map = (new Implement\Repository\MapperParser())
->register('persona_rut', (new Implement\Repository\Mapper())
->setProperty('persona')
->setFunction(function($data) {
return $this->personaRepository->fetchById($data['persona_rut']);
}))
->register('direccion_id', (new Implement\Repository\Mapper())
->setProperty('direccion')
->setFunction(function($data) {
return $this->direccionRepository->fetchById($data['direccion_id']);
})->setDefault(null))
->register('telefono', (new Implement\Repository\Mapper())->setFunction(function($data) {
return $data['telefono'];
})->setDefault(null))
->register('email', (new Implement\Repository\Mapper())->setFunction(function($data) {
return $data['email'];
})->setDefault(null))
->register('fecha_nacimiento', (new Implement\Repository\Mapper\DateTime('fecha_nacimiento'))
->setDefault(null)
->setProperty('fechaNacimiento'))
->register('sexo', (new Implement\Repository\Mapper())->setFunction(function($data) {
return $data['sexo'];
})->setDefault(null))
->register('estado_civil', (new Implement\Repository\Mapper())->setFunction(function($data) {
return $data['estado_civil'];
})->setDefault(null)->setProperty('estadoCivil'))
->register('nacionalidad', (new Implement\Repository\Mapper())->setFunction(function($data) {
return $data['nacionalidad'];
})->setDefault(null))
2024-12-03 16:45:20 -03:00
->register('ocupacion', (new Implement\Repository\Mapper())->setFunction(function($data) {
return $data['ocupacion'];
2024-06-18 22:41:03 -04:00
})->setDefault(null));
2024-12-03 16:45:20 -03:00
return $this->parseData(new Model\Persona\Datos(), $data, $map);
2024-06-18 22:41:03 -04:00
}
2024-12-03 16:45:20 -03:00
public function save(Define\Model $model): Model\Persona\Datos
2024-06-18 22:41:03 -04:00
{
$this->saveNew([
'persona_rut', 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil',
2024-12-03 16:45:20 -03:00
'nacionalidad', 'ocupacion'
2024-06-18 22:41:03 -04:00
], [
$model->persona->rut, $model->direccion?->id, $model->telefono, $model->email, $model->fechaNacimiento,
2024-11-28 11:41:03 -03:00
$model->sexo, $model->estadoCivil, $model->nacionalidad, $model->ocupacion
2024-06-18 22:41:03 -04:00
]);
return $model;
}
2024-12-03 16:45:20 -03:00
public function edit(Define\Model $model, array $new_data): Model\Persona\Datos
2024-06-18 22:41:03 -04:00
{
return $this->update($model, [
'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil',
2024-12-03 16:45:20 -03:00
'nacionalidad', 'ocupacion'
2024-06-18 22:41:03 -04:00
], $new_data);
}
2025-03-03 14:57:22 -03:00
/**
* @param int $persona_rut
* @return Model\Persona\Datos
* @throws Implement\Exception\EmptyResult
*/
2024-12-03 16:45:20 -03:00
public function fetchByPersona(int $persona_rut): Model\Persona\Datos
2024-06-18 22:41:03 -04:00
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('persona_rut = ?');
return $this->fetchOne($query, [$persona_rut]);
}
}