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

127 lines
5.3 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 PDOException;
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) {
2025-08-12 19:17:32 -04:00
if ($data['direccion_id'] === null) {
return null;
}
2024-06-18 22:41:03 -04:00
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
{
2025-09-09 16:24:48 -03:00
$model->id = $this->saveNew([
2024-06-18 22:41:03 -04:00
'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]);
}
/**
* @param array $data
* @return void
* @throws PDOException
*/
public function saveMissing(array $data): void
{
$dataQuery = array_fill(0, count($data), ['?', '?', '?', '?', '?', '?', '?', '?', '?']);
$query = $this->connection->getQueryBuilder()
->insert()
->into($this->getTable())
->columns([
'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil',
'nacionalidad', 'ocupacion'
])
->values($dataQuery);
$flattened = [];
foreach ($data as $col => $value) {
$row = [
$value['direccion_id'] ?? null, $value['telefono'] ?? null, $value['email'] ?? null, $value['fecha_nacimiento'] ?? null,
$value['sexo'] ?? null, $value['estado_civil'] ?? null, $value['nacionalidad'] ?? null, $value['ocupacion'] ?? null
];
$flattened = array_merge($flattened, $row);
}
$this->connection->execute($query, $flattened);
}
2025-09-09 16:24:48 -03:00
public function filterData(array $data): array
{
$fields = ['persona_rut', 'direccion_id', 'telefono', 'email', 'fecha_nacimiento', 'sexo', 'estado_civil',
'nacionalidad', 'ocupacion'];
return array_intersect_key($data, array_flip($fields));
}
2024-06-18 22:41:03 -04:00
}