Files
oficial/app/src/Repository/DatosPersona.php

87 lines
3.7 KiB
PHP
Raw Normal View History

2024-06-18 22:41:03 -04:00
<?php
namespace Incoviba\Repository;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
class DatosPersona extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Persona $personaRepository,
protected Direccion $direccionRepository)
{
parent::__construct($connection);
$this->setTable('datos_personas');
}
public function create(?array $data = null): Model\DatosPersona
{
$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))
->register('profesion', (new Implement\Repository\Mapper())->setFunction(function($data) {
return $data['profesion'];
})->setDefault(null));
return $this->parseData(new Model\DatosPersona(), $data, $map);
}
public function save(Define\Model $model): Model\DatosPersona
{
$this->saveNew([
'persona_rut', 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil',
'nacionalidad', 'profesion'
], [
$model->persona->rut, $model->direccion?->id, $model->telefono, $model->email, $model->fechaNacimiento,
$model->sexo, $model->estadoCivil, $model->nacionalidad, $model->profesion
]);
return $model;
}
public function edit(Define\Model $model, array $new_data): Model\DatosPersona
{
return $this->update($model, [
'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil',
'nacionalidad', 'profesion'
], $new_data);
}
public function fetchByPersona(int $persona_rut): Model\DatosPersona
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('persona_rut = ?');
return $this->fetchOne($query, [$persona_rut]);
}
protected function getKey(): string
{
return 'persona_rut';
}
}